Recherche avancée

Médias (0)

Mot : - Tags -/navigation

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

Autres articles (77)

  • 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 (...)

  • Automated installation script of MediaSPIP

    25 avril 2011, par

    To overcome the difficulties mainly due to the installation of server side software dependencies, an "all-in-one" installation script written in bash was created to facilitate this step on a server with a compatible Linux distribution.
    You must have access to your server via SSH and a root account to use it, which will install the dependencies. Contact your provider if you do not have that.
    The documentation of the use of this installation script is available here.
    The code of this (...)

  • Contribute to translation

    13 avril 2011

    You can help us to improve the language used in the software interface to make MediaSPIP more accessible and user-friendly. You can also translate the interface into any language that allows it to spread to new linguistic communities.
    To do this, we use the translation interface of SPIP where the all the language modules of MediaSPIP are available. Just subscribe to the mailing list and request further informantion on translation.
    MediaSPIP is currently available in French and English (...)

Sur d’autres sites (7315)

  • C++ ffmpeg - export to wav error : Invalid PCM packet, data has size 2 but at least a size of 4 was expected

    9 septembre 2024, par Chris P

    C++ code :

    


    AudioSegment AudioSegment::from_file(const std::string&amp; file_path, const std::string&amp; format, const std::string&amp; codec,&#xA;    const std::map&amp; parameters, int start_second, int duration) {&#xA;&#xA;    avformat_network_init();&#xA;    av_log_set_level(AV_LOG_ERROR); // Adjust logging level as needed&#xA;&#xA;    AVFormatContext* format_ctx = nullptr;&#xA;    if (avformat_open_input(&amp;format_ctx, file_path.c_str(), nullptr, nullptr) != 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not open audio file." &lt;&lt; std::endl;&#xA;        return AudioSegment();  // Return an empty AudioSegment on failure&#xA;    }&#xA;&#xA;    if (avformat_find_stream_info(format_ctx, nullptr) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not find stream information." &lt;&lt; std::endl;&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    int audio_stream_index = -1;&#xA;    for (unsigned int i = 0; i &lt; format_ctx->nb_streams; i&#x2B;&#x2B;) {&#xA;        if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {&#xA;            audio_stream_index = i;&#xA;            break;&#xA;        }&#xA;    }&#xA;&#xA;    if (audio_stream_index == -1) {&#xA;        std::cerr &lt;&lt; "Error: Could not find audio stream." &lt;&lt; std::endl;&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    AVCodecParameters* codec_par = format_ctx->streams[audio_stream_index]->codecpar;&#xA;    const AVCodec* my_codec = avcodec_find_decoder(codec_par->codec_id);&#xA;    AVCodecContext* codec_ctx = avcodec_alloc_context3(my_codec);&#xA;&#xA;    if (!codec_ctx) {&#xA;        std::cerr &lt;&lt; "Error: Could not allocate codec context." &lt;&lt; std::endl;&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    if (avcodec_parameters_to_context(codec_ctx, codec_par) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not initialize codec context." &lt;&lt; std::endl;&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    if (avcodec_open2(codec_ctx, my_codec, nullptr) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not open codec." &lt;&lt; std::endl;&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    SwrContext* swr_ctx = swr_alloc();&#xA;    if (!swr_ctx) {&#xA;        std::cerr &lt;&lt; "Error: Could not allocate SwrContext." &lt;&lt; std::endl;&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;    codec_ctx->sample_rate = 44100;&#xA;    // Set up resampling context to convert to S16 format with 2 bytes per sample&#xA;    av_opt_set_chlayout(swr_ctx, "in_chlayout", &amp;codec_ctx->ch_layout, 0);&#xA;    av_opt_set_int(swr_ctx, "in_sample_rate", codec_ctx->sample_rate, 0);&#xA;    av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", codec_ctx->sample_fmt, 0);&#xA;&#xA;    AVChannelLayout dst_ch_layout;&#xA;    av_channel_layout_copy(&amp;dst_ch_layout, &amp;codec_ctx->ch_layout);&#xA;    av_channel_layout_uninit(&amp;dst_ch_layout);&#xA;    av_channel_layout_default(&amp;dst_ch_layout, 2);&#xA;&#xA;    av_opt_set_chlayout(swr_ctx, "out_chlayout", &amp;dst_ch_layout, 0);&#xA;    av_opt_set_int(swr_ctx, "out_sample_rate", codec_ctx->sample_rate, 0);  // Match input sample rate&#xA;    av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);  // Force S16 format&#xA;&#xA;    if (swr_init(swr_ctx) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Failed to initialize the resampling context" &lt;&lt; std::endl;&#xA;        swr_free(&amp;swr_ctx);&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    AVPacket packet;&#xA;    AVFrame* frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        std::cerr &lt;&lt; "Error: Could not allocate frame." &lt;&lt; std::endl;&#xA;        swr_free(&amp;swr_ctx);&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    std::vector<char> output;&#xA;    while (av_read_frame(format_ctx, &amp;packet) >= 0) {&#xA;        if (packet.stream_index == audio_stream_index) {&#xA;            if (avcodec_send_packet(codec_ctx, &amp;packet) == 0) {&#xA;                while (avcodec_receive_frame(codec_ctx, frame) == 0) {&#xA;                    if (frame->pts != AV_NOPTS_VALUE) {&#xA;                        frame->pts = av_rescale_q(frame->pts, codec_ctx->time_base, format_ctx->streams[audio_stream_index]->time_base);&#xA;                    }&#xA;&#xA;                    uint8_t* output_buffer;&#xA;                    int output_samples = av_rescale_rnd(&#xA;                        swr_get_delay(swr_ctx, codec_ctx->sample_rate) &#x2B; frame->nb_samples,&#xA;                        codec_ctx->sample_rate, codec_ctx->sample_rate, AV_ROUND_UP);&#xA;&#xA;                    int output_buffer_size = av_samples_get_buffer_size(&#xA;                        nullptr, 2, output_samples, AV_SAMPLE_FMT_S16, 1);&#xA;&#xA;                    output_buffer = (uint8_t*)av_malloc(output_buffer_size);&#xA;&#xA;                    if (output_buffer) {&#xA;                        memset(output_buffer, 0, output_buffer_size); // Zero padding to avoid random noise&#xA;                        int converted_samples = swr_convert(swr_ctx, &amp;output_buffer, output_samples,&#xA;                            (const uint8_t**)frame->extended_data, frame->nb_samples);&#xA;&#xA;                        if (converted_samples >= 0) {&#xA;                            output.insert(output.end(), output_buffer, output_buffer &#x2B; output_buffer_size);&#xA;                        }&#xA;                        else {&#xA;                            std::cerr &lt;&lt; "Error: Failed to convert audio samples." &lt;&lt; std::endl;&#xA;                        }&#xA;                        // Make sure output_buffer is valid before freeing&#xA;                        if (output_buffer != nullptr) {&#xA;                            av_free(output_buffer);&#xA;                            output_buffer = nullptr; // Prevent double-free&#xA;                        }&#xA;                    }&#xA;                    else {&#xA;                        std::cerr &lt;&lt; "Error: Could not allocate output buffer." &lt;&lt; std::endl;&#xA;                    }&#xA;                }&#xA;            }&#xA;            else {&#xA;                std::cerr &lt;&lt; "Error: Failed to send packet to codec context." &lt;&lt; std::endl;&#xA;            }&#xA;        }&#xA;        av_packet_unref(&amp;packet);&#xA;    }&#xA;&#xA;    int frame_width = av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * 2;  // Use 2 bytes per sample and 2 channels&#xA;&#xA;    std::map metadata = {&#xA;        {"sample_width", 2},  // S16 format has 2 bytes per sample&#xA;        {"frame_rate", codec_ctx->sample_rate},  // Use the input sample rate&#xA;        {"channels", 2},  // Assuming stereo output&#xA;        {"frame_width", frame_width}&#xA;    };&#xA;&#xA;    av_frame_free(&amp;frame);&#xA;    swr_free(&amp;swr_ctx);&#xA;    avcodec_free_context(&amp;codec_ctx);&#xA;    avformat_close_input(&amp;format_ctx);&#xA;&#xA;    return AudioSegment(static_cast<const>(output.data()), output.size(), metadata);&#xA;}&#xA;&#xA;std::ofstream AudioSegment::export_segment_to_wav_file(const std::string&amp; out_f) {&#xA;    std::cout &lt;&lt; this->get_channels() &lt;&lt; std::endl;&#xA;    av_log_set_level(AV_LOG_ERROR);&#xA;    AVCodecContext* codec_ctx = nullptr;&#xA;    AVFormatContext* format_ctx = nullptr;&#xA;    AVStream* stream = nullptr;&#xA;    AVFrame* frame = nullptr;&#xA;    AVPacket* pkt = nullptr;&#xA;    int ret;&#xA;&#xA;    // Initialize format context for WAV&#xA;    if (avformat_alloc_output_context2(&amp;format_ctx, nullptr, "wav", out_f.c_str()) &lt; 0) {&#xA;        throw std::runtime_error("Could not allocate format context.");&#xA;    }&#xA;&#xA;    // Find encoder for PCM&#xA;    const AVCodec* codec_ptr = avcodec_find_encoder(AV_CODEC_ID_PCM_S16LE);&#xA;    if (!codec_ptr) {&#xA;        throw std::runtime_error("PCM encoder not found.");&#xA;    }&#xA;&#xA;    // Add stream&#xA;    stream = avformat_new_stream(format_ctx, codec_ptr);&#xA;    if (!stream) {&#xA;        throw std::runtime_error("Failed to create new stream.");&#xA;    }&#xA;&#xA;    // Allocate codec context&#xA;    codec_ctx = avcodec_alloc_context3(codec_ptr);&#xA;    if (!codec_ctx) {&#xA;        throw std::runtime_error("Could not allocate audio codec context.");&#xA;    }&#xA;&#xA;    // Set codec parameters for PCM&#xA;    codec_ctx->bit_rate = 128000;  // Bitrate&#xA;    codec_ctx->sample_rate = this->get_frame_rate();  // Use correct sample rate&#xA;    codec_ctx->ch_layout.nb_channels = this->get_channels();  // Set the correct channel count&#xA;&#xA;    // Set the channel layout: stereo or mono&#xA;    if (this->get_channels() == 2) {&#xA;        av_channel_layout_default(&amp;codec_ctx->ch_layout, 2);  // Stereo layout&#xA;    }&#xA;    else {&#xA;        av_channel_layout_default(&amp;codec_ctx->ch_layout, 1);  // Mono layout&#xA;    }&#xA;&#xA;    codec_ctx->sample_fmt = AV_SAMPLE_FMT_S16;  // PCM 16-bit format&#xA;&#xA;    // Open codec&#xA;    if (avcodec_open2(codec_ctx, codec_ptr, nullptr) &lt; 0) {&#xA;        throw std::runtime_error("Could not open codec.");&#xA;    }&#xA;&#xA;    // Set codec parameters to the stream&#xA;    if (avcodec_parameters_from_context(stream->codecpar, codec_ctx) &lt; 0) {&#xA;        throw std::runtime_error("Could not initialize stream codec parameters.");&#xA;    }&#xA;&#xA;    // Open output file&#xA;    std::ofstream out_file(out_f, std::ios::binary);&#xA;    if (!out_file) {&#xA;        throw std::runtime_error("Failed to open output file.");&#xA;    }&#xA;&#xA;    if (!(format_ctx->oformat->flags &amp; AVFMT_NOFILE)) {&#xA;        if (avio_open(&amp;format_ctx->pb, out_f.c_str(), AVIO_FLAG_WRITE) &lt; 0) {&#xA;            throw std::runtime_error("Could not open output file.");&#xA;        }&#xA;    }&#xA;&#xA;    // Write file header&#xA;    if (avformat_write_header(format_ctx, nullptr) &lt; 0) {&#xA;        throw std::runtime_error("Error occurred when writing file header.");&#xA;    }&#xA;&#xA;    // Initialize packet&#xA;    pkt = av_packet_alloc();&#xA;    if (!pkt) {&#xA;        throw std::runtime_error("Could not allocate AVPacket.");&#xA;    }&#xA;&#xA;    // Initialize frame&#xA;    frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        throw std::runtime_error("Could not allocate AVFrame.");&#xA;    }&#xA;&#xA;    // Set the frame properties&#xA;    frame->format = codec_ctx->sample_fmt;&#xA;    frame->ch_layout = codec_ctx->ch_layout;&#xA;&#xA;    // Number of audio samples available in the data buffer&#xA;    int total_samples = data_.size() / (av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);&#xA;    int samples_read = 0;&#xA;&#xA;    // Set the number of samples per frame dynamically based on the input data&#xA;    while (samples_read &lt; total_samples) {&#xA;        // Determine how many samples to read in this iteration (don&#x27;t exceed the total sample count)&#xA;        int num_samples = std::min(codec_ctx->frame_size, total_samples - samples_read);&#xA;        if (num_samples == 0) {&#xA;            num_samples = 1024;&#xA;            codec_ctx->frame_size = 1024;&#xA;        }&#xA;        // Ensure num_samples is not zero&#xA;        if (num_samples &lt;= 0) {&#xA;            throw std::runtime_error("Invalid number of samples in frame.");&#xA;        }&#xA;&#xA;        // Set the number of samples in the frame&#xA;        frame->nb_samples = num_samples;&#xA;&#xA;        // Allocate the frame buffer based on the number of samples&#xA;        ret = av_frame_get_buffer(frame, 0);&#xA;        if (ret &lt; 0) {&#xA;            std::cerr &lt;&lt; "Error allocating frame buffer: " &lt;&lt; ret &lt;&lt; std::endl;&#xA;            throw std::runtime_error("Could not allocate audio data buffers.");&#xA;        }&#xA;&#xA;        // Copy the audio data into the frame&#x27;s buffer (interleaving if necessary)&#xA;        /*if (codec_ctx->ch_layout.nb_channels == 2) {&#xA;            // If stereo, interleave planar data into packed format&#xA;            for (int i = 0; i &lt; num_samples; &#x2B;&#x2B;i) {&#xA;                ((int16_t*)frame->data[0])[2 * i] = ((int16_t*)data_.data())[i];                // Left channel&#xA;                ((int16_t*)frame->data[0])[2 * i &#x2B; 1] = ((int16_t*)data_.data())[total_samples &#x2B; i]; // Right channel&#xA;            }&#xA;        }&#xA;        else {&#xA;            // For mono or packed data, directly copy the samples&#xA;            std::memcpy(frame->data[0], data_.data() &#x2B; samples_read * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels,&#xA;                num_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);&#xA;        }&#xA;        */&#xA;        std::memcpy(frame->data[0], data_.data() &#x2B; samples_read * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels,&#xA;            num_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);&#xA;&#xA;        // Send the frame for encoding&#xA;        ret = avcodec_send_frame(codec_ctx, frame);&#xA;        if (ret &lt; 0) {&#xA;            std::cerr &lt;&lt; "Error sending frame for encoding: " &lt;&lt; ret &lt;&lt; std::endl;&#xA;            throw std::runtime_error("Error sending frame for encoding.");&#xA;        }&#xA;&#xA;        // Receive and write encoded packets&#xA;        while (ret >= 0) {&#xA;            ret = avcodec_receive_packet(codec_ctx, pkt);&#xA;            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {&#xA;                break;&#xA;            }&#xA;            else if (ret &lt; 0) {&#xA;                throw std::runtime_error("Error during encoding.");&#xA;            }&#xA;&#xA;            out_file.write(reinterpret_cast(pkt->data), pkt->size);&#xA;            av_packet_unref(pkt);&#xA;        }&#xA;&#xA;        samples_read &#x2B;= num_samples;&#xA;    }&#xA;&#xA;    // Flush the encoder&#xA;    if (avcodec_send_frame(codec_ctx, nullptr) &lt; 0) {&#xA;        throw std::runtime_error("Error flushing the encoder.");&#xA;    }&#xA;&#xA;    while (avcodec_receive_packet(codec_ctx, pkt) >= 0) {&#xA;        out_file.write(reinterpret_cast(pkt->data), pkt->size);&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;&#xA;    // Write file trailer&#xA;    av_write_trailer(format_ctx);&#xA;&#xA;    // Cleanup&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;    avcodec_free_context(&amp;codec_ctx);&#xA;&#xA;    if (!(format_ctx->oformat->flags &amp; AVFMT_NOFILE)) {&#xA;        avio_closep(&amp;format_ctx->pb);&#xA;    }&#xA;    avformat_free_context(format_ctx);&#xA;&#xA;    out_file.close();&#xA;    return out_file;&#xA;}&#xA;&#xA;</const></char>

    &#xA;

    Run code :

    &#xA;

    #include "audio_segment.h"&#xA;#include "effects.h"&#xA;#include "playback.h"&#xA;#include "cppaudioop.h"&#xA;#include "exceptions.h"&#xA;#include "generators.h"&#xA;#include "silence.h"&#xA;#include "utils.h"&#xA;&#xA;#include <iostream>&#xA;#include <filesystem>&#xA;&#xA;using namespace cppdub;&#xA;&#xA;int main() {&#xA;    try {&#xA;        // Load the source audio file&#xA;        AudioSegment seg_1 = AudioSegment::from_file("../data/test10.mp3");&#xA;        std::string out_file_name = "ah-ah-ah.wav";&#xA;&#xA;        // Export the audio segment to a new file with specified settings&#xA;        //seg_1.export_segment(out_file_name, "mp3");&#xA;        seg_1.export_segment_to_wav_file(out_file_name);&#xA;&#xA;&#xA;        // Optionally play the audio segment to verify&#xA;        // play(seg_1);&#xA;&#xA;        // Load the exported audio file&#xA;        AudioSegment seg_2 = AudioSegment::from_file(out_file_name);&#xA;&#xA;        // Play segments&#xA;        //play(seg_1);&#xA;        play(seg_2);&#xA;    }&#xA;    catch (const std::exception&amp; e) {&#xA;        std::cerr &lt;&lt; "An error occurred: " &lt;&lt; e.what() &lt;&lt; std::endl;&#xA;    }&#xA;&#xA;    return 0;&#xA;}&#xA;</filesystem></iostream>

    &#xA;

    Error in second call of from_file function :

    &#xA;

    [pcm_s16le @ 000002d82ca5bfc0] Invalid PCM packet, data has size 2 but at least a size of 4 was expected

    &#xA;

    The process continue, i call hear the seg_2 with play(seg_2) call, but i can't directly play seg_2 export wav file (from windows explorer).

    &#xA;

    I had a guess that error may be because packed vs plannar formats missmatch but i am not quit sure. Maybe a swr_convert is necessary.

    &#xA;

  • encoding with ffmpeg libx265 -pix_fmt gray gives unplayable vid

    9 juin 2017, par netjiro

    What am I missing ?
    I encode an old black and white film clip with ffmpeg libx265 passing -pix_fmt gray. The output is unplayable in both vlc and mplayer (linux), so I assume I’m missing something...

    encoding :

    ffmpeg -i clip.mkv \
       -c:v libx265 -preset slow -x265-params "crf=24" -pix_fmt gray \
       -c:a libopus -b:a 64k \
       -c:s copy \
       out.mkv

    vlc errors :

    [00007f8a3ddfe328] blend blend error: no matching alpha blending routine (chroma: RGBA -> GREY)
    [00007f8a3ddfe328] core blend error: blending RGBA to GREY failed
    ... repeated ...

    mplayer errors :

    Unexpected decoder output format Planar Y800
    ... repeated ...

    ffmpeg encoding output :

    ffmpeg version 3.2.4 Copyright (c) 2000-2017 the FFmpeg developers
     built with gcc 4.9.4 (Gentoo 4.9.4 p1.0, pie-0.6.4)
     configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --docdir=/usr/share/doc/ffmpeg-3.2.4/html --mandir=/usr/share/man --enable-shared --cc=x86_64-pc-linux-gnu-gcc --cxx=x86_64-pc-linux-gnu-g++ --ar=x86_64-pc-linux-gnu-ar --optflags='-march=native -O2 -pipe' --disable-static --enable-avfilter --enable-avresample --disable-stripping --enable-nonfree --enable-version3 --disable-indev=oss --disable-indev=jack --disable-outdev=oss --enable-version3 --enable-bzlib --disable-runtime-cpudetect --disable-debug --disable-gcrypt --disable-gnutls --disable-gmp --enable-gpl --enable-hardcoded-tables --enable-iconv --enable-lzma --enable-network --enable-openssl --enable-postproc --disable-libsmbclient --enable-ffplay --enable-sdl2 --enable-vaapi --enable-vdpau --enable-xlib --enable-libxcb --enable-libxcb-shm --enable-libxcb-xfixes --enable-zlib --enable-libcdio --disable-libiec61883 --disable-libdc1394 --disable-libcaca --enable-openal --enable-opengl --enable-libv4l2 --disable-libpulse --enable-libopencore-amrwb --enable-libopencore-amrnb --disable-libfdk-aac --enable-libopenjpeg --enable-libbluray --enable-libcelt --disable-libgme --disable-libgsm --disable-mmal --enable-libmodplug --enable-libopus --disable-libilbc --disable-librtmp --enable-libssh --enable-libschroedinger --enable-libspeex --enable-libvorbis --enable-libvpx --disable-libzvbi --disable-libbs2b --disable-chromaprint --disable-libebur128 --disable-libflite --disable-frei0r --disable-libfribidi --enable-fontconfig --disable-ladspa --disable-libass --enable-libfreetype --disable-librubberband --disable-libzimg --enable-libsoxr --enable-pthreads --enable-libvo-amrwbenc --enable-libmp3lame --disable-libkvazaar --disable-nvenc --disable-libopenh264 --enable-libsnappy --enable-libtheora --enable-libtwolame --enable-libwavpack --disable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --disable-amd3dnow --disable-amd3dnowext --disable-fma4 --disable-xop --cpu=host --disable-doc --disable-htmlpages --enable-manpages
     libavutil      55. 34.101 / 55. 34.101
     libavcodec     57. 64.101 / 57. 64.101
     libavformat    57. 56.101 / 57. 56.101
     libavdevice    57.  1.100 / 57.  1.100
     libavfilter     6. 65.100 /  6. 65.100
     libavresample   3.  1.  0 /  3.  1.  0
     libswscale      4.  2.100 /  4.  2.100
     libswresample   2.  3.100 /  2.  3.100
     libpostproc    54.  1.100 / 54.  1.100
    x265 [info]: HEVC encoder version 2.2
    x265 [info]: build info [Linux][GCC 4.9.4][64 bit] 8bit+10bit+12bit
    x265 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX AVX2 FMA3 LZCNT BMI2
    x265 [info]: Unknown profile, Level-3.1 (Main tier)
    x265 [warning]: No thread pool allocated, --wpp disabled
    x265 [warning]: No thread pool allocated, --lookahead-slices disabled
    x265 [info]: Slices                              : 1
    x265 [info]: frame threads / pool features       : 3 / none
    x265 [info]: Coding QT: max CU size, min CU size : 64 / 8
    x265 [info]: Residual QT: max TU size, max depth : 32 / 1 inter / 1 intra
    x265 [info]: ME / range / subpel / merge         : star / 57 / 3 / 3
    x265 [info]: Keyframe min / max / scenecut / bias: 23 / 250 / 40 / 5.00
    x265 [info]: Lookahead / bframes / badapt        : 25 / 4 / 2
    x265 [info]: b-pyramid / weightp / weightb       : 1 / 1 / 0
    x265 [info]: References / ref-limit  cu / depth  : 4 / on / on
    x265 [info]: AQ: mode / str / qg-size / cu-tree  : 1 / 1.0 / 32 / 1
    x265 [info]: Rate Control / qCompress            : CRF-24.0 / 0.60
    x265 [info]: tools: rect limit-modes rd=4 psy-rd=2.00 rdoq=2 psy-rdoq=1.00
    x265 [info]: tools: rskip signhide tmvp strong-intra-smoothing deblock sao
    Output #0, matroska, to 'out.mkv':
     Metadata:
       encoder         : Lavf57.56.101
       Metadata:
       Stream #0:0(eng): Video: hevc (libx265), gray, 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 23.98 fps, 1k tbn, 23.98 tbc (default)
       Metadata:
         encoder         : Lavc57.64.101 libx265
       Stream #0:1(eng): Audio: opus (libopus) ([255][255][255][255] / 0xFFFFFFFF), 48000 Hz, stereo, flt, 64 kb/s (default)
       Metadata:
         encoder         : Lavc57.64.101 libopus
       Stream #0:2(eng): Subtitle: subrip (default)
    Stream mapping:
     Stream #0:0 -> #0:0 (h264 (native) -> hevc (libx265))
     Stream #0:1 -> #0:1 (eac3 (native) -> opus (libopus))
     Stream #0:3 -> #0:2 (copy)
    Press [q] to stop, [?] for help
    frame= 1439 fps=7.0 q=-0.0 Lsize=    5356kB time=00:01:00.01 bitrate= 731.1kbits/s speed=0.294x    
    video:4940kB audio:382kB subtitle:1kB other streams:0kB global headers:2kB muxing overhead: 0.629434%
    x265 [info]: frame I:      9, Avg QP:22.27  kb/s: 6064.82
    x265 [info]: frame P:    340, Avg QP:23.62  kb/s: 1950.21
    x265 [info]: frame B:   1090, Avg QP:29.65  kb/s: 230.75  
    x265 [info]: Weighted P-Frames: Y:0.9% UV:0.0%
    x265 [info]: consecutive B-frames: 2.9% 0.3% 1.4% 72.5% 22.9%
  • Read existing MP4 File and write into a new MP4 file using libavcodec

    4 octobre 2023, par Tahfimul

    I am new to the libavcodec space.

    &#xA;

    I am trying to read video and audio streams from an existing mp4 file and take the data from the two streams and then mux the two streams and write the muxed data into a new mp4 file using libavcodec in C++. Essentially, I am aiming to split the original (existing) mp4 file into small chunks of 1 second clips that then can be played back using a video player. I would like to preserve the original mp4 video's video stream (i.e. preserve its color, resolution and etc.) and preserve the mp4 video's audio stream (i.e. preserve its bit rate and etc.). I am trying to achieve this using libavcodec in C++. But there does not seem to be any tutorial or documentation online that points me to that direction.

    &#xA;

    So far, I have looked at and tried to implement a solution using this tutorial (tutorial#1) : https://github.com/leandromoreira/ffmpeg-libav-tutorial/blob/master/0_hello_world.c

    &#xA;

    However, tutorial#1 aimed to save each video frame from the existing (original) mp4 video stream into individual .pgm files, which meant that the .pgm files would store a grayscale image of each video frame.

    &#xA;

    Since, I want to preserve the colors of the original (existing) mp4 file, I looked at this tutorial (tutorial#2) that aimed to convert the grayscale video frame into color using the swscale library : https://www.youtube.com/watch?v=Y7SUm7Xf1sc&ab_channel=Bartholomew&#xA;However, in tutorial#2, they exported the output from swscale library to a GUI library to be viewed in a GUI application and did not show hwo to write the output data into a new mp4 file that can be played back by a video player.

    &#xA;

    So then, I looked at this tutorial(tutorial#3) which showed how to create an MP4 file using libavcodec : C++ FFmpeg create mp4 file&#xA;However, the problem with that solution is that I was not able to take a video frame from the original mp4 video and store it into another mp4 file. I kept getting errors when attempting to do so and I did not succeed in taking the data from the original(existing) mp4 file and storing it into a new mp4 file.

    &#xA;

    Here is the code that I have written so far :

    &#xA;

    #include<fstream>&#xA;#include &#xA;#include &#xA;#include &#xA;extern "C"&#xA;{&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>mathematics.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;#include <libavfilter></libavfilter>buffersrc.h>&#xA;#include <libavfilter></libavfilter>buffersink.h>&#xA;#include <libavutil></libavutil>time.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;}&#xA;#pragma comment(lib, "avfilter.lib")&#xA;#ifdef av_err2str&#xA;#undef av_err2str&#xA;#include <string>&#xA;av_always_inline std::string av_err2string(int errnum) {&#xA;    char str[AV_ERROR_MAX_STRING_SIZE];&#xA;    return av_make_error_string(str, AV_ERROR_MAX_STRING_SIZE, errnum);&#xA;}&#xA;#define av_err2str(err) av_err2string(err).c_str()&#xA;#endif  // av_err2str&#xA;&#xA;#include <chrono>&#xA;#include <thread>&#xA;&#xA;&#xA;// decode packets into frames&#xA;static int decode_packet(AVPacket *pPacket, AVCodecContext *pCodecContext, AVFrame *pFrame);&#xA;&#xA;static void pushFrame(AVFrame* frame, AVCodecContext* outputCodecContext, AVPacket * outputPacket, AVFormatContext* outputFormatContext, AVCodec *outputCodec) {&#xA;    &#xA;    std::cout&lt;&lt;"outputCodecContext: "&lt;format = AV_PIX_FMT_YUV420P;&#xA;        frame->width = 800;&#xA;        frame->height = 800;&#xA;        if ((err = av_frame_get_buffer(frame, 32)) &lt; 0) {&#xA;            std::cout &lt;&lt; "Failed to allocate picture" &lt;&lt; err &lt;&lt; std::endl;&#xA;            return;&#xA;        }&#xA;    }&#xA;    SwsContext* swsCtx = nullptr;&#xA;    if (!swsCtx) {&#xA;        swsCtx = sws_getContext(800, 800, AV_PIX_FMT_RGB24, 800, &#xA;            800, AV_PIX_FMT_YUV420P, SWS_BICUBIC, 0, 0, 0);&#xA;    }&#xA;    int inLinesize[1] = { 3 * 800 };&#xA;    // From RGB to YUV&#xA;    // sws_scale(swsCtx, (const uint8_t* const*)&amp;data, inLinesize, 0, 800, &#xA;        // frame->data, frame->linesize);&#xA;    std::cout&lt;&lt;"frame "&lt;pts = (1.0 / 30.0) * 90000 * (1);&#xA;    // std::cout &lt;&lt; videoFrame->pts &lt;&lt; " " &lt;&lt; cctx->time_base.num &lt;&lt; " " &lt;&lt; &#xA;    //     cctx->time_base.den &lt;&lt; " " &lt;&lt; 1 &lt;&lt; std::endl;&#xA;    if ((err = avcodec_send_frame(outputCodecContext, frame)) &lt; 0) {&#xA;        std::cout &lt;&lt; "Failed to send frame" &lt;&lt; err &lt;&lt; std::endl;&#xA;        return;&#xA;    }&#xA;    AV_TIME_BASE;&#xA;    AVPacket pkt;&#xA;    av_init_packet(&amp;pkt);&#xA;    pkt.data = NULL;&#xA;    pkt.size = 0;&#xA;    pkt.flags |= AV_PKT_FLAG_KEY;&#xA;    std::cout&lt;&lt;"here\n";&#xA;    if (avcodec_receive_packet(outputCodecContext, outputPacket) == 0) {&#xA;        static int counter = 0;&#xA;        if (counter == 0) {&#xA;            FILE* fp = fopen("dump_first_frame1.dat", "wb");&#xA;            fwrite(outputPacket->data, outputPacket->size, 1, fp);&#xA;            fclose(fp);&#xA;        }&#xA;        // std::cout &lt;&lt; "pkt key: " &lt;&lt; (pkt.flags &amp; AV_PKT_FLAG_KEY) &lt;&lt; " " &lt;&lt; &#xA;        //     pkt.size &lt;&lt; " " &lt;&lt; (counter&#x2B;&#x2B;) &lt;&lt; std::endl;&#xA;        // uint8_t* size = ((uint8_t*)pkt.data);&#xA;        // std::cout &lt;&lt; "first: " &lt;&lt; (int)size[0] &lt;&lt; " " &lt;&lt; (int)size[1] &lt;&lt; &#xA;        //     " " &lt;&lt; (int)size[2] &lt;&lt; " " &lt;&lt; (int)size[3] &lt;&lt; " " &lt;&lt; (int)size[4] &lt;&lt; &#xA;        //     " " &lt;&lt; (int)size[5] &lt;&lt; " " &lt;&lt; (int)size[6] &lt;&lt; " " &lt;&lt; (int)size[7] &lt;&lt; &#xA;        //     std::endl;&#xA;        av_interleaved_write_frame(outputFormatContext, outputPacket);&#xA;        av_packet_unref(outputPacket);&#xA;    }&#xA;}&#xA;&#xA;int main()&#xA;{&#xA;&#xA;    char* filename = "c&#x2B;&#x2B;.mp4";&#xA;&#xA;    AVFormatContext *pFormatContext = avformat_alloc_context();&#xA;&#xA;    AVOutputFormat* outputFormat = NULL;&#xA;&#xA;    AVFormatContext* outputFormatContext = nullptr;&#xA;&#xA;    AVCodecContext* outputCodecContext = nullptr;&#xA;&#xA;    if (!pFormatContext) {&#xA;        std::cerr&lt;&lt;"ERROR could not allocate memory for Format Context\n";&#xA;        return -1;&#xA;    }&#xA;&#xA;    if (avformat_open_input(&amp;pFormatContext, filename , NULL, NULL) != 0) {&#xA;        std::cerr&lt;&lt;"ERROR could not open the file\n";&#xA;            return -1;&#xA;    }&#xA;&#xA;    std::cout&lt;&lt;"format: "&lt;iformat->name&lt;&lt;" , duration:"&lt;&lt;(double)(pFormatContext->duration/AV_TIME_BASE)&lt;&lt;"seconds, bit_rate:"&lt;bit_rate&lt;video_codec);&#xA;&#xA;    &#xA;    if (!outputCodec)&#xA;    {&#xA;        std::cout &lt;&lt; "can&#x27;t create output codec" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }   &#xA;    &#xA;&#xA;    AVStream* outputStream = avformat_new_stream(outputFormatContext, outputCodec);&#xA;&#xA;    if (!outputStream)&#xA;    {&#xA;        std::cout &lt;&lt; "can&#x27;t find output format" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    outputCodecContext = avcodec_alloc_context3(outputCodec);&#xA;&#xA;    if (!outputCodecContext)&#xA;    {&#xA;        std::cout &lt;&lt; "can&#x27;t create output codec context" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    AVCodec *pCodec = NULL;&#xA;&#xA;    AVCodecParameters *pCodecParameters =  NULL;&#xA;&#xA;    int video_stream_index = -1;&#xA;&#xA;    AVStream* stream = NULL;&#xA;    &#xA;    // loop though all the streams and print its main information&#xA;    for (int i = 0; i &lt; pFormatContext->nb_streams; i&#x2B;&#x2B;)&#xA;     {&#xA;        &#xA;        AVCodecParameters *pLocalCodecParameters =  NULL;&#xA;        pLocalCodecParameters = pFormatContext->streams[i]->codecpar;&#xA;&#xA;        AVCodec *pLocalCodec = NULL;&#xA;        pLocalCodec = avcodec_find_decoder(pLocalCodecParameters->codec_id);&#xA;        if (pLocalCodec==NULL) {&#xA;            std::cerr&lt;&lt;"ERROR unsupported codec!\n";&#xA;                // In this example if the codec is not found we just skip it&#xA;                continue;&#xA;            }&#xA;&#xA;&#xA;        if (pLocalCodecParameters->codec_type == AVMEDIA_TYPE_VIDEO) {&#xA;                if (video_stream_index == -1) {&#xA;                    video_stream_index = i;&#xA;                    pCodec = pLocalCodec;&#xA;                    pCodecParameters = pLocalCodecParameters;&#xA;                    stream = pFormatContext->streams[i];&#xA;                    std::cout&lt;&lt;"codec id: "&lt;codecpar->codec_id&lt;codecpar->codec_type&lt;codecpar->width&lt;codecpar->height&lt;codecpar->format&lt;codecpar->bit_rate&lt;codecpar->codec_id = outputFormat->video_codec;&#xA;                    // outputStream->codecpar->codec_id = stream->codecpar->codec_id;&#xA;                    outputStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;                    outputStream->codecpar->width = stream->codecpar->width;&#xA;                    outputStream->codecpar->height = stream->codecpar->height;&#xA;                    outputStream->codecpar->format = AV_PIX_FMT_YUV420P;&#xA;                    outputStream->codecpar->bit_rate = stream->codecpar->bit_rate;&#xA;                    &#xA;                    avcodec_parameters_to_context(outputCodecContext, outputStream->codecpar);&#xA;                }       &#xA;&#xA;                std::cout&lt;&lt;"Video Codec: resolution " &lt;&lt; pLocalCodecParameters->width &lt;&lt; " x "&lt;height&lt;codec_type == AVMEDIA_TYPE_AUDIO) {&#xA;                std::cout&lt;&lt;"Audio Codec: "&lt;channels&lt;&lt;" channels, sample rate "&lt;sample_rate&lt;name &lt;&lt; " ID: " &lt;id&lt;&lt; " bit_rate: "&lt;bit_rate&lt;/ outputStream->codecpar->codec_id = outputFormat->video_codec;&#xA;    // outputStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    // outputStream->codecpar->width = 300;&#xA;    // outputStream->codecpar->height = 300;&#xA;    // outputStream->codecpar->format = AV_PIX_FMT_YUV420P;&#xA;    // outputStream->codecpar->bit_rate = 200 * 1000;&#xA;    outputCodecContext->time_base = (AVRational){ 1, 1 };&#xA;    outputCodecContext->max_b_frames = 2;&#xA;    outputCodecContext->gop_size = 12;&#xA;    outputCodecContext->framerate = (AVRational){ 30, 1 };&#xA;&#xA;    if (avcodec_parameters_to_context(pCodecContext, pCodecParameters) &lt; 0)&#xA;    {&#xA;        std::cerr&lt;&lt;"failed to copy codec params to codec context\n";&#xA;            return -1;&#xA;    }&#xA;&#xA;    // std::cout&lt;&lt;"pCodecContext->time_base: "&lt;time_base)&lt;/ outputCodecContext->time_base = pCodecContext->time_base;&#xA;    // outputCodecContext->max_b_frames = pCodecContext->max_b_frames;&#xA;    // outputCodecContext->gop_size = pCodecContext->gop_size;&#xA;    // outputCodecContext->framerate = pCodecContext->framerate;&#xA;&#xA;    if (outputStream->codecpar->codec_id == AV_CODEC_ID_H264) {&#xA;        // av_opt_set(pCodecContext, "preset", "ultrafast", 0);&#xA;        av_opt_set(outputCodecContext, "preset", "ultrafast", 0);&#xA;    }&#xA;    else if (outputStream->codecpar->codec_id == AV_CODEC_ID_H265)&#xA;    {&#xA;        // av_opt_set(pCodecContext, "preset", "ultrafast", 0);&#xA;        av_opt_set(outputCodecContext, "preset", "ultrafast", 0);&#xA;    }&#xA;&#xA;    // avcodec_parameters_from_context(stream->codecpar, pCodecContext);&#xA;    avcodec_parameters_from_context(outputStream->codecpar, outputCodecContext);&#xA;&#xA;    if (avcodec_open2(pCodecContext, pCodec, NULL) &lt; 0)&#xA;    {&#xA;        std::cerr&lt;&lt;"failed to open codec through avcodec_open2\n";&#xA;            return -1;&#xA;    }&#xA;&#xA;    if (avcodec_open2(outputCodecContext, outputCodec, NULL) &lt; 0)&#xA;    {&#xA;        std::cerr&lt;&lt;"failed to open output codec through avcodec_open2\n";&#xA;            return -1;&#xA;    }&#xA;&#xA;&#xA;    if (!(outputFormat->flags &amp; AVFMT_NOFILE)) {&#xA;        if (avio_open(&amp;outputFormatContext->pb, "test.mp4", AVIO_FLAG_WRITE) &lt; 0) {&#xA;            std::cout &lt;&lt; "Failed to open file" &lt;&lt; std::endl;&#xA;            return -1;&#xA;        }&#xA;    }&#xA;&#xA;    if (avformat_write_header(outputFormatContext, NULL) &lt; 0) {&#xA;        std::cout &lt;&lt; "Failed to write header" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    av_dump_format(outputFormatContext, 0, "test.mp4", 1);&#xA;&#xA;&#xA;    AVFrame *pFrame = av_frame_alloc();&#xA;    if (!pFrame)&#xA;    {&#xA;        std::cerr&lt;&lt;"failed to allocate memory for AVFrame\n";&#xA;            return -1;&#xA;    }&#xA;    &#xA;    // https://ffmpeg.org/doxygen/trunk/structAVPacket.html&#xA;    AVPacket *pPacket = av_packet_alloc();&#xA;    if (!pPacket)&#xA;    {&#xA;            std::cerr&lt;&lt;"failed to allocate memory for AVPacket\n";&#xA;            return -1;&#xA;    }&#xA;&#xA;    int response = 0;&#xA;    int how_many_packets_to_process = 300;&#xA;&#xA;    // fill the Packet with data from the Stream&#xA;    // https://ffmpeg.org/doxygen/trunk/group__lavf__decoding.html#ga4fdb3084415a82e3810de6ee60e46a61&#xA;    while (av_read_frame(pFormatContext, pPacket) >= 0)&#xA;    {&#xA;            // if it&#x27;s the video stream&#xA;            if (pPacket->stream_index == video_stream_index) {&#xA;            std::cout&lt;&lt;"AVPacket->pts "&lt;pts;&#xA;                // if(av_write_frame(outputFormatContext, pPacket)&lt;0)&#xA;                //  std::cout&lt;&lt;"error writing output frame\n";&#xA;                // pushFrame(pFrame, outputCodecContext, pPacket, outputFormatContext, outputCodec);&#xA;                response = decode_packet(pPacket, pCodecContext, pFrame);&#xA;                if (response &lt; 0)&#xA;                    break;&#xA;                // stop it, otherwise we&#x27;ll be saving hundreds of frames&#xA;                if (--how_many_packets_to_process &lt;= 0) break;&#xA;            }&#xA;            // https://ffmpeg.org/doxygen/trunk/group__lavc__packet.html#ga63d5a489b419bd5d45cfd09091cbcbc2&#xA;            av_packet_unref(pPacket);&#xA;    }   &#xA;&#xA;    if(av_write_trailer(outputFormatContext)&lt;0)&#xA;        std::cout &lt;&lt;"Error writing output trailer\n";&#xA;&#xA;&#xA;    return 0;&#xA;}&#xA;&#xA;int save_frame_as_mpeg(AVCodecContext* pCodecCtx, AVFrame* pFrame, int FrameNo) {&#xA;    int ret = 0;&#xA;&#xA;    const AVCodec* mpegCodec = avcodec_find_encoder(pCodecCtx->codec_id);&#xA;    if (!mpegCodec) {&#xA;        std::cout&lt;&lt;"failed to open mpegCodec\n";&#xA;        return -1;&#xA;    }&#xA;    AVCodecContext* mpegContext = avcodec_alloc_context3(mpegCodec);&#xA;    if (!mpegContext) {&#xA;        std::cout&lt;&lt;"failed to open mpegContext\n";&#xA;        return -1;&#xA;    }&#xA;&#xA;    mpegContext->pix_fmt = pCodecCtx->pix_fmt;&#xA;    mpegContext->height = pFrame->height;&#xA;    mpegContext->width = pFrame->width;&#xA;    mpegContext->time_base = AVRational{ 1,10 };&#xA;&#xA;    ret = avcodec_open2(mpegContext, mpegCodec, NULL);&#xA;    if (ret &lt; 0) {&#xA;        return ret;&#xA;    }&#xA;    FILE* MPEGFile;&#xA;    char MPEGFName[256];&#xA;&#xA;    AVPacket packet;&#xA;    packet.data = NULL;&#xA;    packet.size = 0;&#xA;    av_init_packet(&amp;packet);&#xA;&#xA;    int gotFrame;&#xA;&#xA;    ret = avcodec_send_frame(mpegContext, pFrame);&#xA;    if (ret &lt; 0) {&#xA;        std::cout&lt;&lt;"failed to send frame for mpegContext\n";&#xA;        return ret;&#xA;    }&#xA;&#xA;    ret = avcodec_receive_packet(mpegContext, &amp;packet);&#xA;    if (ret &lt; 0) {&#xA;        std::cout&lt;&lt;"failed to receive packet for mpegContext\terrocode: "&lt;pix_fmt = pCodecCtx->pix_fmt;&#xA;    jpegContext->height = pFrame->height;&#xA;    jpegContext->width = pFrame->width;&#xA;    jpegContext->time_base = AVRational{ 1,10 };&#xA;&#xA;    ret = avcodec_open2(jpegContext, jpegCodec, NULL);&#xA;    if (ret &lt; 0) {&#xA;        return ret;&#xA;    }&#xA;    FILE* JPEGFile;&#xA;    char JPEGFName[256];&#xA;&#xA;    AVPacket packet;&#xA;    packet.data = NULL;&#xA;    packet.size = 0;&#xA;    av_init_packet(&amp;packet);&#xA;&#xA;    int gotFrame;&#xA;&#xA;    ret = avcodec_send_frame(jpegContext, pFrame);&#xA;    if (ret &lt; 0) {&#xA;        return ret;&#xA;    }&#xA;&#xA;    ret = avcodec_receive_packet(jpegContext, &amp;packet);&#xA;    if (ret &lt; 0) {&#xA;        return ret;&#xA;    }&#xA;&#xA;    sprintf(JPEGFName, "c:\\folder\\dvr-%06d.jpg", FrameNo);&#xA;    JPEGFile = fopen(JPEGFName, "wb");&#xA;    fwrite(packet.data, 1, packet.size, JPEGFile);&#xA;    fclose(JPEGFile);&#xA;&#xA;    av_packet_unref(&amp;packet);&#xA;    avcodec_close(jpegContext);&#xA;    return 0;&#xA;}&#xA;&#xA;static int decode_packet(AVPacket *pPacket, AVCodecContext *pCodecContext, AVFrame *pFrame)&#xA;{&#xA;  // Supply raw packet data as input to a decoder&#xA;  // https://ffmpeg.org/doxygen/trunk/group__lavc__decoding.html#ga58bc4bf1e0ac59e27362597e467efff3&#xA;  int response = avcodec_send_packet(pCodecContext, pPacket);&#xA;  if (response &lt; 0) {&#xA;      std::cerr&lt;&lt;"Error while sending a packet to the decoder: "&lt;= 0)&#xA;  {&#xA;    // Return decoded output data (into a frame) from a decoder&#xA;    // https://ffmpeg.org/doxygen/trunk/group__lavc__decoding.html#ga11e6542c4e66d3028668788a1a74217c&#xA;    response = avcodec_receive_frame(pCodecContext, pFrame);&#xA;    if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {&#xA;      break;&#xA;    } else if (response &lt; 0) {&#xA;        std::cerr&lt;&lt;"Error while receiving a frame from the decoder: "&lt;= 0) {&#xA;&#xA;      response = save_frame_as_jpeg(pCodecContext, pFrame, pCodecContext->frame_number);&#xA;&#xA;      if(response&lt;0)&#xA;      {&#xA;        std::cerr&lt;&lt;"Failed to save frame as jpeg\n";&#xA;        return -1;&#xA;      }&#xA;&#xA;      response = save_frame_as_mpeg(pCodecContext, pFrame, pCodecContext->frame_number);&#xA;&#xA;      if(response&lt;0)&#xA;      {&#xA;        std::cerr&lt;&lt;"Failed to save frame as mpeg\n";&#xA;        return -1;&#xA;      }&#xA;&#xA;&#xA;     std::cout&lt;&lt;&#xA;          "Frame "&lt;frame_number&lt;&lt; "type= "&lt;pict_type)&lt;&lt;" size= "&lt;pkt_size&lt;&lt;" bytes, format= "&lt;format&lt;&lt;" "&lt;pts&lt;&lt;"pts key_frame "&lt;key_frame&lt;&lt; " [DTS"&lt;coded_picture_number&lt;&lt;" ]\n";&#xA;      &#xA;      char frame_filename[1024];&#xA;      snprintf(frame_filename, sizeof(frame_filename), "%s-%d.pgm", "frame", pCodecContext->frame_number);&#xA;      // Check if the frame is a planar YUV 4:2:0, 12bpp&#xA;      // That is the format of the provided .mp4 file&#xA;      // RGB formats will definitely not give a gray image&#xA;      // Other YUV image may do so, but untested, so give a warning&#xA;      if (pFrame->format != AV_PIX_FMT_YUV420P)&#xA;      {&#xA;          std::cout&lt;&lt;"Warning: the generated file may not be a grayscale image, but could e.g. be just the R component if the video format is RGB\n";&#xA;      }&#xA;   &#xA;    }&#xA;  }&#xA;  return 0;&#xA;}&#xA;</thread></chrono></string></fstream>

    &#xA;

    The question that I am seeking an answer to is How can I use libavcodec to split an mp4 file into 1 second chunk clips (those clips will be in mp4 format) ?

    &#xA;