Recherche avancée

Médias (0)

Mot : - Tags -/alertes

Aucun média correspondant à vos critères n’est disponible sur le site.

Autres articles (105)

  • Encoding and processing into web-friendly formats

    13 avril 2011, par

    MediaSPIP automatically converts uploaded files to internet-compatible formats.
    Video files are encoded in MP4, Ogv and WebM (supported by HTML5) and MP4 (supported by Flash).
    Audio files are encoded in MP3 and Ogg (supported by HTML5) and MP3 (supported by Flash).
    Where possible, text is analyzed in order to retrieve the data needed for search engine detection, and then exported as a series of image files.
    All uploaded files are stored online in their original format, so you can (...)

  • List of compatible distributions

    26 avril 2011, par

    The table below is the list of Linux distributions compatible with the automated installation script of MediaSPIP. Distribution nameVersion nameVersion number Debian Squeeze 6.x.x Debian Weezy 7.x.x Debian Jessie 8.x.x Ubuntu The Precise Pangolin 12.04 LTS Ubuntu The Trusty Tahr 14.04
    If you want to help us improve this list, you can provide us access to a machine whose distribution is not mentioned above or send the necessary fixes to add (...)

  • Soumettre bugs et patchs

    10 avril 2011

    Un logiciel n’est malheureusement jamais parfait...
    Si vous pensez avoir mis la main sur un bug, reportez le dans notre système de tickets en prenant bien soin de nous remonter certaines informations pertinentes : le type de navigateur et sa version exacte avec lequel vous avez l’anomalie ; une explication la plus précise possible du problème rencontré ; si possibles les étapes pour reproduire le problème ; un lien vers le site / la page en question ;
    Si vous pensez avoir résolu vous même le bug (...)

Sur d’autres sites (12961)

  • Using libav to encode RGBA frames into MP4 but the output is a mess

    5 octobre 2019, par Cu2S

    I’m trying to decode a video into RGB frames, and then postprocess the frames, finally encode the frames into a video. But the output video is completely a mess :
    Screenshot from potplayer

    I wrote a minimal example to illustrate my idea. First, I read some information from some source video :

       AVFormatContext* inputFormatCtx = nullptr;
       int ret = avformat_open_input(&inputFormatCtx, inputParamsVideo, nullptr, nullptr);
       assert(ret >= 0);
       ret = avformat_find_stream_info(inputFormatCtx, NULL);
       av_dump_format(inputFormatCtx, 0, inputParamsVideo, 0);

       assert(ret >= 0);
       AVStream* inputVideoStream = nullptr;
       for (int i = 0; i < inputFormatCtx->nb_streams; i++)
       {
           const auto inputStream = inputFormatCtx->streams[i];
           if (inputStream->codec->codec_type == AVMEDIA_TYPE_VIDEO)
           {
               inputVideoStream = inputStream;
               break;
           }
       }

       assert(inputVideoStream != nullptr);
       AVCodecParameters* inputParams = inputVideoStream->codecpar;
       AVRational framerate = inputVideoStream->codec->framerate;
       auto gop_size = inputVideoStream->codec->gop_size;
       auto maxBFrames = inputVideoStream->codec->max_b_frames;

    Then I assign the information to the output stream :

    AVFormatContext *outputAVFormat = nullptr;
    avformat_alloc_output_context2(&outputAVFormat, nullptr, nullptr, kOutputPath);
    assert(outputAVFormat);
    AVCodec* codec = avcodec_find_encoder(outputAVFormat->oformat->video_codec);
    assert(codec);
    AVCodecContext* encodingCtx = avcodec_alloc_context3(codec);
    avcodec_parameters_to_context(encodingCtx, inputParams);
    encodingCtx->time_base = av_inv_q(framerate);
    encodingCtx->max_b_frames = maxBFrames;
    encodingCtx->gop_size = gop_size;


    if (outputAVFormat->oformat->flags & AVFMT_GLOBALHEADER)
       encodingCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    AVStream* outStream = avformat_new_stream(outputAVFormat, nullptr);
    assert(outStream != nullptr);
    ret = avcodec_parameters_from_context(outStream->codecpar, encodingCtx);
    assert(ret >= 0);
    outStream->time_base = encodingCtx->time_base;

    Then I convert RGBA frames(which is read from files) into YUV420P via sws_scale, and encoding :

       ret = avcodec_open2(encodingCtx, codec, nullptr);
       assert(ret >= 0);
       av_dump_format(outputAVFormat, 0, kOutputPath, 1);

       ret = avio_open(&outputAVFormat->pb, kOutputPath, AVIO_FLAG_WRITE);
       assert(ret >= 0);
       ret = avformat_write_header(outputAVFormat, nullptr);
       assert(ret >= 0);

       AVFrame* frame = av_frame_alloc();
       frame->width = inputParams->width;
       frame->height = inputParams->height;
       frame->format = inputParams->format;
       frame->pts = 0;
       assert(ret >= 0);

       ret = av_frame_get_buffer(frame, 32);
       int frameCount = 0;
       assert(ret >= 0);
       ret = av_frame_make_writable(frame);
       assert(ret >= 0);
       SwsContext* swsContext = sws_getContext(inputParams->width, inputParams->height,
           AV_PIX_FMT_RGBA, frame->width,
           frame->height, static_cast<avpixelformat>(inputParams->format),
           SWS_BILINEAR, NULL, NULL, NULL);


       for (auto inputPicPath : std::filesystem::directory_iterator(kInputDir))
       {
           int width, height, comp;
           unsigned char* data = stbi_load(inputPicPath.path().string().c_str(), &amp;width, &amp;height, &amp;comp, 4);
           int srcStrides[1] = { 4 * width };
           int ret = sws_scale(swsContext, &amp;data, srcStrides, 0, height, frame->data,
               frame->linesize);
           assert(ret >= 0);
           frame->pts = frameCount;
           //frame->pict_type = AV_PICTURE_TYPE_I;
           frameCount += 1;
           encode(encodingCtx, frame, 0, outputAVFormat);

           stbi_image_free(data);
       }

       while (encode(encodingCtx, nullptr, 0, outputAVFormat))
       {
           ;
       }

       static bool encode(AVCodecContext* enc_ctx, AVFrame* frame, std::uint32_t streamIndex,
           AVFormatContext * formatCtx)
       {
           int ret;
           int got_output = 0;
           AVPacket packet = {};
           av_init_packet(&amp;packet);
           ret = avcodec_encode_video2(enc_ctx, &amp;packet, frame, &amp;got_output);
           assert(ret >= 0);
           if (got_output) {
               packet.stream_index = streamIndex;
               av_packet_rescale_ts(&amp;packet, enc_ctx->time_base, formatCtx->streams[streamIndex]->time_base);
               ret = av_interleaved_write_frame(formatCtx, &amp;packet);
               assert(ret >= 0);
               return true;
           }
           else {
               return false;
           }
       }
    </avpixelformat>

    Finally I cleaned up stuff :

       av_write_trailer(outputAVFormat);
       sws_freeContext(swsContext);
       avcodec_free_context(&amp;encodingCtx);
       avio_closep(&amp;outputAVFormat->pb);
       avformat_free_context(outputAVFormat);
       av_frame_free(&amp;frame);

    I dumped my input format and my output format :

    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'H:\Me.MP4':
     Metadata:
       major_brand     : mp42
       minor_version   : 1
       compatible_brands: mp41mp42isom
       creation_time   : 2019-04-03T05:44:22.000000Z
     Duration: 00:00:06.90, start: 0.000000, bitrate: 1268 kb/s
       Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 540x960, 1238 kb/s, 29.86 fps, 30 tbr, 600 tbn, 1200 tbc (default)
       Metadata:
         creation_time   : 2019-04-03T05:44:22.000000Z
         handler_name    : Core Media Video
       Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 8000 Hz, stereo, fltp, 24 kb/s (default)
       Metadata:
         creation_time   : 2019-04-03T05:44:22.000000Z
         handler_name    : Core Media Audio
    [libx264 @ 000002126F90C1C0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
    [libx264 @ 000002126F90C1C0] profile High, level 3.1, 4:2:0, 8-bit
    [libx264 @ 000002126F90C1C0] 264 - core 157 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=2 keyint=12 keyint_min=1 scenecut=40 intra_refresh=0 rc_lookahead=12 rc=abr mbtree=1 bitrate=1238 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
    Output #0, mp4, to './output.mp4':
       Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 540x960, q=2-31, 1238 kb/s, 29.86 tbn

    Update :

    After I deleted

    encodingCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

    the output video is right. Also, outputting avi works, too.

  • ffmpeg piped output producing incorrect metadata frame count with Python

    6 décembre 2024, par Xorgon

    Using Python, I am attempting to use ffmpeg to compress videos and put them in a PowerPoint. This works great, however, the video files themselves have incorrect frame counts which can cause issues when I read from those videos in other code.

    &#xA;

    Edit for clarification : by "frame count" I mean the metadata frame count. The actual number of frames contained in the video is correct, but querying the metadata gives an incorrect frame count.

    &#xA;

    Having eliminated the PowerPoint aspect of the code, I've narrowed this down to the following minimal reproducing example of saving an output from an ffmpeg pipe :

    &#xA;

    from subprocess import Popen, PIPE&#xA;&#xA;video_path = &#x27;test_mp4.mp4&#x27;&#xA;&#xA;ffmpeg_pipe = Popen([&#x27;ffmpeg&#x27;,&#xA;                     &#x27;-y&#x27;,  # Overwrite files&#xA;                     &#x27;-i&#x27;, f&#x27;{video_path}&#x27;,  # Input from file&#xA;                     &#x27;-f&#x27;, &#x27;avi&#x27;,  # Output format&#xA;                     &#x27;-c:v&#x27;, &#x27;libx264&#x27;,  # Codec&#xA;                     &#x27;-&#x27;],  # Output to pipe&#xA;                    stdout=PIPE)&#xA;&#xA;new_path = "piped_video.avi"&#xA;vid_file = open(new_path, "wb")&#xA;vid_file.write(ffmpeg_pipe.stdout.read())&#xA;vid_file.close()&#xA;

    &#xA;

    I've tested several different videos. One small example video that I've tested can be found here.

    &#xA;

    I've tried a few different codecs with avi format and tried libvpx with webm format. For the avi outputs, the frame count usually reads as 1073741824 (2^30). Weirdly, for the webm format, the frame count read as -276701161105643264.

    &#xA;

    This is a snippet I used to read the frame count, but one could also see the error by opening the video details in Windows Explorer and seeing the total time as something like 9942 hours, 3 minutes, and 14 seconds.

    &#xA;

    import cv2&#xA;&#xA;video_path = &#x27;test_mp4.mp4&#x27;&#xA;new_path = "piped_video.webm"&#xA;&#xA;cap = cv2.VideoCapture(video_path)&#xA;print(f"Original video frame count: = {int(cap.get(cv2.CAP_PROP_FRAME_COUNT)):d}")&#xA;cap.release()&#xA;&#xA;cap = cv2.VideoCapture(new_path)&#xA;print(f"Piped video frame count: = {int(cap.get(cv2.CAP_PROP_FRAME_COUNT)):d}")&#xA;cap.release()&#xA;

    &#xA;

    For completeness, here is the ffmpeg output :

    &#xA;

    ffmpeg version 2023-06-11-git-09621fd7d9-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers&#xA;  built with gcc 12.2.0 (Rev10, Built by MSYS2 project)&#xA;  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint&#xA;  libavutil      58. 13.100 / 58. 13.100&#xA;  libavcodec     60. 17.100 / 60. 17.100&#xA;  libavformat    60.  6.100 / 60.  6.100&#xA;  libavdevice    60.  2.100 / 60.  2.100&#xA;  libavfilter     9.  8.101 /  9.  8.101&#xA;  libswscale      7.  3.100 /  7.  3.100&#xA;  libswresample   4. 11.100 /  4. 11.100&#xA;  libpostproc    57.  2.100 / 57.  2.100&#xA;Input #0, mov,mp4,m4a,3gp,3g2,mj2, from &#x27;test_mp4.mp4&#x27;:&#xA;  Metadata:&#xA;    major_brand     : mp42&#xA;    minor_version   : 0&#xA;    compatible_brands: isommp42&#xA;    creation_time   : 2022-08-10T12:54:09.000000Z&#xA;  Duration: 00:00:06.67, start: 0.000000, bitrate: 567 kb/s&#xA;  Stream #0:0[0x1](eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 384x264 [SAR 1:1 DAR 16:11], 563 kb/s, 30 fps, 30 tbr, 30k tbn (default)&#xA;    Metadata:&#xA;      creation_time   : 2022-08-10T12:54:09.000000Z&#xA;      handler_name    : Mainconcept MP4 Video Media Handler&#xA;      vendor_id       : [0][0][0][0]&#xA;      encoder         : AVC Coding&#xA;Stream mapping:&#xA;  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))&#xA;Press [q] to stop, [?] for help&#xA;[libx264 @ 0000018c68c8b9c0] using SAR=1/1&#xA;[libx264 @ 0000018c68c8b9c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2&#xA;[libx264 @ 0000018c68c8b9c0] profile High, level 2.1, 4:2:0, 8-bit&#xA;Output #0, avi, to &#x27;pipe:&#x27;:&#xA;  Metadata:&#xA;    major_brand     : mp42&#xA;    minor_version   : 0&#xA;    compatible_brands: isommp42&#xA;    ISFT            : Lavf60.6.100&#xA;  Stream #0:0(eng): Video: h264 (H264 / 0x34363248), yuv420p(progressive), 384x264 [SAR 1:1 DAR 16:11], q=2-31, 30 fps, 30 tbn (default)&#xA;    Metadata:&#xA;      creation_time   : 2022-08-10T12:54:09.000000Z&#xA;      handler_name    : Mainconcept MP4 Video Media Handler&#xA;      vendor_id       : [0][0][0][0]&#xA;      encoder         : Lavc60.17.100 libx264&#xA;    Side data:&#xA;      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A&#xA;[out#0/avi @ 0000018c687f47c0] video:82kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.631060%&#xA;frame=  200 fps=0.0 q=-1.0 Lsize=      85kB time=00:00:06.56 bitrate= 106.5kbits/s speed=76.2x    &#xA;[libx264 @ 0000018c68c8b9c0] frame I:1     Avg QP:16.12  size:  3659&#xA;[libx264 @ 0000018c68c8b9c0] frame P:80    Avg QP:21.31  size:   647&#xA;[libx264 @ 0000018c68c8b9c0] frame B:119   Avg QP:26.74  size:   243&#xA;[libx264 @ 0000018c68c8b9c0] consecutive B-frames:  3.0% 53.0%  0.0% 44.0%&#xA;[libx264 @ 0000018c68c8b9c0] mb I  I16..4: 17.6% 70.6% 11.8%&#xA;[libx264 @ 0000018c68c8b9c0] mb P  I16..4:  0.8%  1.7%  0.6%  P16..4: 17.6%  4.6%  3.3%  0.0%  0.0%    skip:71.4%&#xA;[libx264 @ 0000018c68c8b9c0] mb B  I16..4:  0.1%  0.3%  0.2%  B16..8: 11.7%  1.4%  0.4%  direct: 0.6%  skip:85.4%  L0:32.0% L1:59.7% BI: 8.3%&#xA;[libx264 @ 0000018c68c8b9c0] 8x8 transform intra:59.6% inter:62.4%&#xA;[libx264 @ 0000018c68c8b9c0] coded y,uvDC,uvAC intra: 48.5% 0.0% 0.0% inter: 3.5% 0.0% 0.0%&#xA;[libx264 @ 0000018c68c8b9c0] i16 v,h,dc,p: 19% 39% 25% 17%&#xA;[libx264 @ 0000018c68c8b9c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 21% 25% 30%  3%  3%  4%  4%  4%  5%&#xA;[libx264 @ 0000018c68c8b9c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 22% 20% 16%  6%  8%  8%  8%  5%  6%&#xA;[libx264 @ 0000018c68c8b9c0] i8c dc,h,v,p: 100%  0%  0%  0%&#xA;[libx264 @ 0000018c68c8b9c0] Weighted P-Frames: Y:0.0% UV:0.0%&#xA;[libx264 @ 0000018c68c8b9c0] ref P L0: 76.2%  7.9% 11.2%  4.7%&#xA;[libx264 @ 0000018c68c8b9c0] ref B L0: 85.6% 12.9%  1.5%&#xA;[libx264 @ 0000018c68c8b9c0] ref B L1: 97.7%  2.3%&#xA;[libx264 @ 0000018c68c8b9c0] kb/s:101.19&#xA;

    &#xA;

    So the question is : why does this happen, and how can one avoid it ?

    &#xA;

  • I want to take any Audio from a file and encode it as PCM_ALAW. My Example is a .m4a file to .wav file

    22 novembre 2023, par Clockman

    I have been working on this for a while now while am generally new to ffmpeg library, I have studied it a bit. The challenge I have that at the point of witting to file I get the following exception.

    &#xA;

    "Exception thrown at 0x00007FFACA8305B3 (avformat-60.dll) in FfmpegPractice.exe : 0xC0000005 : Access violation writing location 0x0000000000000000.". I understand this means am writing to an uninitialized buffer am unable to discover why this is happening. The exception call stack shows the following

    &#xA;

    avformat-60.dll!avformat_write_header() C&#xA;avformat-60.dll!ff_write_chained()  C&#xA;avformat-60.dll!ff_write_chained()  C&#xA;avformat-60.dll!av_write_frame()    C&#xA;FfmpegPractice.exe!main() Line 215  C&#x2B;&#x2B;&#xA;

    &#xA;

    Some things I have tried

    &#xA;

    This code is part of a larger project built with CMake but for some reason I could no step into ffmpeg library while debugging, So I recompiled ffmpeg ensured debugging was enabled so I could drill down to the root cause but I still could not step into the ffmpeg library.

    &#xA;

    I then created a minimal project using visual studio c++ console project and I still could not step into the code.

    &#xA;

    I have read through many ffmpeg docs and some I could find on the internet and I still could not solve it.

    &#xA;

    This is the code

    &#xA;

    #include <iostream>&#xA;&#xA;extern "C" {&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libswresample></libswresample>swresample.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavutil></libavutil>audio_fifo.h>&#xA;}&#xA;&#xA;using namespace std;&#xA;&#xA;//in audio file&#xA;string filename{ "rapid_caller_test.m4a" };&#xA;AVFormatContext* pFormatCtx{};&#xA;AVCodecContext* pCodecCtx{};&#xA;AVStream* pStream{};&#xA;&#xA;//out audio file&#xA;string outFilename{ "output.wav" };&#xA;AVFormatContext* pOutFormatCtx{ nullptr };&#xA;AVCodecContext* pOutCodecCtx{ nullptr };&#xA;AVIOContext* pOutIoContext{ nullptr };&#xA;const AVCodec* pOutCodec{ nullptr };&#xA;AVStream* pOutStream{ nullptr };&#xA;const int OUTPUT_CHANNELS = 1;&#xA;const int SAMPLE_RATE = 8000;&#xA;const int OUT_BIT_RATE = 64000;&#xA;uint8_t** convertedSamplesBuffer{ nullptr };&#xA;int64_t dstNmbrSamples{ 0 };&#xA;int dstLineSize{ 0 };&#xA;static int64_t pts{ 0 };&#xA;&#xA;//conversion context;&#xA;SwrContext* swr{};&#xA;&#xA;uint32_t i{ 0 };&#xA;int audiostream{ -1 };&#xA;&#xA;&#xA;void cleanUp() &#xA;{&#xA;  avcodec_free_context(&amp;pOutCodecCtx);;&#xA;  avio_closep(&amp;(pOutFormatCtx)->pb);&#xA;  avformat_free_context(pOutFormatCtx);&#xA;  pOutFormatCtx = nullptr;&#xA;}&#xA;&#xA;int main()&#xA;{&#xA;&#xA;/*&#xA;* section to setup input file&#xA;*/&#xA;if (avformat_open_input(&amp;pFormatCtx, filename.data(), nullptr, nullptr) != 0) {&#xA;  cout &lt;&lt; "could not open file " &lt;&lt; filename &lt;&lt; endl;&#xA;  return -1;&#xA;}&#xA;if (avformat_find_stream_info(pFormatCtx, nullptr) &lt; 0) {&#xA;  cout &lt;&lt; "Could not retrieve stream information from file " &lt;&lt; filename &lt;&lt; endl;&#xA;  return -1;&#xA;}&#xA;av_dump_format(pFormatCtx, 0, filename.c_str(), 0);&#xA;&#xA;for (i = 0; i &lt; pFormatCtx->nb_streams; i&#x2B;&#x2B;) {&#xA;  if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {&#xA;    audiostream = i;&#xA;    break;&#xA;  }&#xA;}&#xA;if (audiostream == -1) {&#xA;  cout &lt;&lt; "did not find audio stream" &lt;&lt; endl;&#xA;  return -1;&#xA;}&#xA;&#xA;pStream = pFormatCtx->streams[audiostream];&#xA;const AVCodec* pCodec{ avcodec_find_decoder(pStream->codecpar->codec_id) };&#xA;pCodecCtx = avcodec_alloc_context3(pCodec);&#xA;avcodec_parameters_to_context(pCodecCtx, pStream->codecpar);&#xA;if (avcodec_open2(pCodecCtx, pCodec, nullptr)) {&#xA;  cout &lt;&lt; "could not open codec" &lt;&lt; endl;&#xA;  return -1;&#xA;}&#xA;&#xA;/*&#xA;* section to set up output file which is a G711 audio&#xA;*/&#xA;if (avio_open(&amp;pOutIoContext, outFilename.data(), AVIO_FLAG_WRITE)) {&#xA;  cout &lt;&lt; "could not open out put file" &lt;&lt; endl;&#xA;  return -1;&#xA;}&#xA;if (!(pOutFormatCtx = avformat_alloc_context())) {&#xA;  cout &lt;&lt; "could not create format conext" &lt;&lt; endl;&#xA;  cleanUp();&#xA;  return -1;&#xA;}&#xA;pOutFormatCtx->pb = pOutIoContext;&#xA;if (!(pOutFormatCtx->oformat = av_guess_format(nullptr, outFilename.data(), nullptr))) {&#xA;  cout &lt;&lt; "could not find output file format" &lt;&lt; endl;&#xA;  cleanUp();&#xA;  return -1;&#xA;}&#xA;if (!(pOutFormatCtx->url = av_strdup(outFilename.data()))) {&#xA;  cout &lt;&lt; "could not allocate file name" &lt;&lt; endl;&#xA;  cleanUp();&#xA;  return -1;&#xA;}&#xA;if (!(pOutCodec = avcodec_find_encoder(AV_CODEC_ID_PCM_ALAW))) {&#xA;  cout &lt;&lt; "codec not found" &lt;&lt; endl;&#xA;  cleanUp();&#xA;  return -1;&#xA;}&#xA;if (!(pOutStream = avformat_new_stream(pOutFormatCtx, nullptr))) {&#xA;  cout &lt;&lt; "could not create new stream" &lt;&lt; endl;&#xA;  cleanUp();&#xA;  return -1;&#xA;}&#xA;if (!(pOutCodecCtx = avcodec_alloc_context3(pOutCodec))) {&#xA;  cout &lt;&lt; "could not allocate codec context" &lt;&lt; endl;&#xA;  return -1;&#xA;}&#xA;av_channel_layout_default(&amp;pOutCodecCtx->ch_layout, OUTPUT_CHANNELS);&#xA;pOutCodecCtx->sample_rate = SAMPLE_RATE;&#xA;pOutCodecCtx->sample_fmt = pOutCodec->sample_fmts[0];&#xA;pOutCodecCtx->bit_rate = OUT_BIT_RATE;&#xA;&#xA;//setting sample rate for the container&#xA;pOutStream->time_base.den = SAMPLE_RATE;&#xA;pOutStream->time_base.num = 1;&#xA;if (pOutFormatCtx->oformat->flags &amp; AVFMT_GLOBALHEADER)&#xA;  pOutCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;&#xA;&#xA;if (avcodec_open2(pOutCodecCtx, pOutCodec, nullptr)) {&#xA;  cout &lt;&lt; "could not open output codec" &lt;&lt; endl;&#xA;  cleanUp();&#xA;  return -1;&#xA;}&#xA;if ((avcodec_parameters_from_context(pOutStream->codecpar, pOutCodecCtx)) &lt; 0) {&#xA;  cout &lt;&lt; "could not initialize stream parameters" &lt;&lt; endl;&#xA;}   &#xA;&#xA;AVPacket* packet = av_packet_alloc();&#xA;&#xA;swr = swr_alloc();&#xA;swr_alloc_set_opts2(&amp;swr, &amp;pOutCodecCtx->ch_layout, pOutCodecCtx->sample_fmt, pOutCodecCtx->sample_rate,&amp;pCodecCtx->ch_layout, pCodecCtx->sample_fmt, pCodecCtx->sample_rate, 0, nullptr);&#xA;swr_init(swr);&#xA;&#xA;int ret{};&#xA;int bSize{};&#xA;while (av_read_frame(pFormatCtx, packet) >= 0) {&#xA;  AVFrame* pFrame = av_frame_alloc();&#xA;  AVFrame* pOutFrame = av_frame_alloc();&#xA;  if (packet->stream_index == audiostream) {&#xA;    ret = avcodec_send_packet(pCodecCtx, packet);&#xA;    while (ret >= 0) {&#xA;    ret = avcodec_receive_frame(pCodecCtx, pFrame);&#xA;    if (ret == AVERROR(EAGAIN))&#xA;    continue;&#xA;    else if (ret == AVERROR_EOF)&#xA;    break;&#xA;    dstNmbrSamples = av_rescale_rnd(swr_get_delay(swr, pCodecCtx->sample_rate) &#x2B; pFrame->nb_samples, pOutCodecCtx->sample_rate, pCodecCtx->sample_rate, AV_ROUND_UP);&#xA;    if ((av_samples_alloc_array_and_samples(&amp;convertedSamplesBuffer, &amp;dstLineSize, pOutCodecCtx->ch_layout.nb_channels,dstNmbrSamples, pOutCodecCtx->sample_fmt, 0)) &lt; 0) {&#xA;    cout &lt;&lt; "coult not allocate samples array and buffer" &lt;&lt; endl;&#xA;    }&#xA;    int channel_samples_count{ 0 };&#xA;    channel_samples_count = swr_convert(swr, convertedSamplesBuffer, dstNmbrSamples, (const uint8_t**)pFrame->data, pFrame->nb_samples);&#xA;    bSize = av_samples_get_buffer_size(&amp;dstLineSize, pOutCodecCtx->ch_layout.nb_channels, channel_samples_count, pOutCodecCtx->sample_fmt, 0);&#xA;    cout &lt;&lt; "no of samples is " &lt;&lt; channel_samples_count &lt;&lt; " the buffer size " &lt;&lt; bSize &lt;&lt; endl;&#xA;    pOutFrame->nb_samples = channel_samples_count;&#xA;    av_channel_layout_copy(&amp;pOutFrame->ch_layout, &amp;pOutCodecCtx->ch_layout);&#xA;    pOutFrame->format = pOutCodecCtx->sample_fmt;&#xA;    pOutFrame->sample_rate = pOutCodecCtx->sample_rate;&#xA;    if ((av_frame_get_buffer(pOutFrame, 0)) &lt; 0) {&#xA;    cout &lt;&lt; "could not allocate output frame samples " &lt;&lt; endl;&#xA;    av_frame_free(&amp;pOutFrame);&#xA;  }&#xA;                &#xA;    //populate out frame buffer&#xA;    av_frame_make_writable(pOutFrame);&#xA;    for (int i{ 0 }; i &lt; bSize; i&#x2B;&#x2B;) {&#xA;    pOutFrame->data[0][i] = convertedSamplesBuffer[0][i];&#xA;    cout &lt;&lt; pOutFrame->data[0][i];&#xA;   }&#xA;   if (pOutFrame) {&#xA;   pOutFrame->pts = pts;&#xA;   pts &#x2B;= pOutFrame->nb_samples;&#xA;  }&#xA;   int res = avcodec_send_frame(pOutCodecCtx, pOutFrame);&#xA;    if (res &lt; 0) {&#xA;    cout &lt;&lt; "error sending frame to encoder" &lt;&lt; endl;&#xA;    cleanUp();&#xA;    return -1;&#xA;   }&#xA;   //int er = avformat_write_header(pOutFormatCtx,nullptr);&#xA;   AVPacket* pOutPacket = av_packet_alloc();&#xA;   pOutPacket->time_base.num = 1;&#xA;   pOutPacket->time_base.den = 8000;&#xA;   if (pOutPacket == nullptr) {&#xA;    cout &lt;&lt; "unable to allocate packet" &lt;&lt; endl;&#xA;  }&#xA;  while (res >= 0) {&#xA;   res = avcodec_receive_packet(pOutCodecCtx, pOutPacket);&#xA;   if (res == AVERROR(EAGAIN))&#xA;    continue;&#xA;   else if (ret == AVERROR_EOF)&#xA;    break;&#xA;   av_packet_rescale_ts(pOutPacket, pOutCodecCtx->time_base, pOutFormatCtx->streams[0]->time_base);&#xA;   //av_dump_format(pOutFormatCtx, 0, outFilename.c_str(), 1);&#xA;   if (av_write_frame(pOutFormatCtx, pOutPacket) &lt; 0) {&#xA;    cout &lt;&lt; "could not write frame" &lt;&lt; endl;&#xA;    }&#xA;   }&#xA;  }&#xA;}&#xA; av_frame_free(&amp;pFrame);&#xA; av_frame_free(&amp;pOutFrame);&#xA;}&#xA;if (av_write_trailer(pOutFormatCtx) &lt; 0) {&#xA; cout &lt;&lt; "could not write file trailer" &lt;&lt; endl;&#xA;}&#xA;swr_free(&amp;swr);&#xA;avcodec_free_context(&amp;pOutCodecCtx);&#xA;av_packet_free(&amp;packet);&#xA;}&#xA;</iostream>

    &#xA;

    Error/Exception

    &#xA;

    The exception is thrown when I call

    &#xA;

    if (av_write_frame(pOutFormatCtx, pOutPacket) &lt; 0)  {   cout &lt;&lt; "could not write frame" &lt;&lt; endl; } &#xA;I also called this line

    &#xA;

    //int er = avformat_write_header(pOutFormatCtx,nullptr);

    &#xA;

    to see if I will get an exception but it did not throw any exception.

    &#xA;

    I have spent weeks on this issue with no success.&#xA;My goal is to take any audio from a file an be able to resample it if need be, and transcode it to PCM_ALAW.&#xA;I will appreciate any help I can get.

    &#xA;