Recherche avancée

Médias (0)

Mot : - Tags -/protocoles

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

Autres articles (84)

  • Les formats acceptés

    28 janvier 2010, par

    Les commandes suivantes permettent d’avoir des informations sur les formats et codecs gérés par l’installation local de ffmpeg :
    ffmpeg -codecs ffmpeg -formats
    Les format videos acceptés en entrée
    Cette liste est non exhaustive, elle met en exergue les principaux formats utilisés : h264 : H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 m4v : raw MPEG-4 video format flv : Flash Video (FLV) / Sorenson Spark / Sorenson H.263 Theora wmv :
    Les formats vidéos de sortie possibles
    Dans un premier temps on (...)

  • Les autorisations surchargées par les plugins

    27 avril 2010, par

    Mediaspip core
    autoriser_auteur_modifier() afin que les visiteurs soient capables de modifier leurs informations sur la page d’auteurs

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

Sur d’autres sites (5210)

  • I need some help accurately trimming video using the FFmpeg C api

    20 mars 2015, par Justin Bradley

    I need some help accurately trimming video using the FFmpeg C api.
    What I’m seeing is when the input video stream has a time_base of 1/48000 it correctly selects the trimmed start and end times. However when the input video stream has a time_base other than 1/48000 the trim is incorrect.

    Time bases of 1/30000 and 1/24000 trim half the expected video stream length - 10s instead of 20s.
    1/25 trims almost nothing at all - the output file size is only a few kb.

    The audio stream appears to always be trimmed correctly.

    For example if I try to trim the first 20s of a video whose video stream time base is 1/30000, the output mp4’s length is 20s. It has first 10s of video and first 20s of audio.

    I think I’m incorrectly calculating the end_time but I’m not sure why it’s correct for 1/48000 time_base streams.

    record[i].start_time = av_rescale_q((int64_t)( start_time * AV_TIME_BASE ), default_timebase, in_stream->time_base);
    record[i].end_time = av_rescale_q((int64_t)( end_time   * AV_TIME_BASE ), default_timebase, in_stream->time_base);

    Here is a more complete sample of code :

    int num_of_streams = ifmt_ctx->nb_streams;
    if(num_of_streams > 0) {
       // keeps track of each stream's trimmed start and end times
       struct stream_pts record[num_of_streams];

       for (i = 0; i < num_of_streams; i++) {
           AVStream *in_stream = ifmt_ctx->streams[i];
           AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
           if (!out_stream) {
               LOGE("=> Failed allocating output stream");
               ret = AVERROR_UNKNOWN;
               return close_connection(ret);
           }

           ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
           if (ret < 0) {
               LOGE("=> Failed to copy context from input to output stream codec context");
               return close_connection(ret);
           }
           out_stream->codec->codec_tag = 0;
           if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
               out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;

           AVRational default_timebase;
           default_timebase.num = 1;
           default_timebase.den = AV_TIME_BASE;

           // determine start/end times for each stream
           record[i].index = i;
           record[i].start_time = av_rescale_q((int64_t)( start_time * AV_TIME_BASE ), default_timebase, in_stream->time_base);
           record[i].end_time = av_rescale_q((int64_t)( end_time   * AV_TIME_BASE ), default_timebase, in_stream->time_base);
       }

       av_dump_format(ofmt_ctx, 0, output_file, 1);

       if (!(ofmt->flags & AVFMT_NOFILE)) {
           ret = avio_open(&ofmt_ctx->pb, output_file, AVIO_FLAG_WRITE);
           if (ret < 0) {
               LOGE("=> Could not open output file '%s'", output_file);
               return close_connection(ret);
           }
       }

       ret = avformat_write_header(ofmt_ctx, NULL);
       if (ret < 0) {
           LOGE("=> Error occurred when opening output file");
           return close_connection(ret);
       }

       while (1) {
           AVStream *in_stream, *out_stream;

           ret = av_read_frame(ifmt_ctx, &pkt);
           if (ret < 0)
               break;

           in_stream  = ifmt_ctx->streams[pkt.stream_index];
           out_stream = ofmt_ctx->streams[pkt.stream_index];

           // copy packet
           pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
           pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
           pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
           pkt.pos = -1;

           // write the frames we're looking for
           if(pkt.pts >= record[pkt.stream_index].start_time && pkt.pts <= record[pkt.stream_index].end_time) {
               ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
               if (ret < 0) {
                   LOGE("=> Error muxing packet");
                   break;
               }
           }

           av_free_packet(&pkt);
       }
    }
  • C++ ffmpeg lib version 7.0 - noice in exported audio

    2 septembre 2024, par Chris P

    I want to make a C++ lib named cppdub which will mimic the python module pydub.

    


    One main function is to export the AudioSegment to a file with a specific format (example : mp3).

    


    The code is :

    


    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 (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;        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;&#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", 48000, 0);&#xA;    av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);&#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;                        48000, 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;&#xA;                        av_free(output_buffer);&#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;    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;    std::map metadata = {&#xA;        {"sample_width", 2},&#xA;        {"frame_rate", 48000},&#xA;        {"channels", 2},&#xA;        {"frame_width", 4}&#xA;    };&#xA;&#xA;    return AudioSegment(static_cast<const>(output.data()), output.size(), metadata);&#xA;}&#xA;&#xA;&#xA;std::ofstream AudioSegment::export_segment(std::string&amp; out_f,&#xA;    const std::string&amp; format,&#xA;    const std::string&amp; codec,&#xA;    const std::string&amp; bitrate,&#xA;    const std::vector&amp; parameters,&#xA;    const std::map&amp; tags,&#xA;    const std::string&amp; id3v2_version,&#xA;    const std::string&amp; cover) {&#xA;    av_log_set_level(AV_LOG_DEBUG);&#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;    // 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;    // Initialize format context&#xA;    avformat_alloc_output_context2(&amp;format_ctx, nullptr, format.c_str(), out_f.c_str());&#xA;    if (!format_ctx) {&#xA;        throw std::runtime_error("Could not allocate format context.");&#xA;    }&#xA;&#xA;    // Find encoder&#xA;    const AVCodec* codec_ptr = avcodec_find_encoder_by_name(codec.c_str());&#xA;    if (!codec_ptr) {&#xA;        throw std::runtime_error("Codec 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&#xA;    codec_ctx->bit_rate = std::stoi(bitrate);&#xA;    codec_ctx->sample_fmt = codec_ptr->sample_fmts ? codec_ptr->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;&#xA;    codec_ctx->sample_rate = frame_rate_;&#xA;    codec_ctx->ch_layout.nb_channels = this->get_channels();&#xA;    AVChannelLayout ch_layout_1;&#xA;    av_channel_layout_uninit(&amp;ch_layout_1);&#xA;    av_channel_layout_default(&amp;ch_layout_1, this->get_channels());&#xA;    codec_ctx->ch_layout = ch_layout_1;&#xA;&#xA;    // Open codec&#xA;    ret = avcodec_open2(codec_ctx, codec_ptr, nullptr);&#xA;    if (ret &lt; 0) {&#xA;        throw std::runtime_error("Could not open codec.");&#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;    frame->nb_samples = codec_ctx->frame_size;&#xA;    frame->format = codec_ctx->sample_fmt;&#xA;    frame->ch_layout = codec_ctx->ch_layout;&#xA;    frame->sample_rate = codec_ctx->sample_rate;&#xA;&#xA;    // Allocate data buffer&#xA;    ret = av_frame_get_buffer(frame, 0);&#xA;    if (ret &lt; 0) {&#xA;        throw std::runtime_error("Could not allocate audio data buffers.");&#xA;    }&#xA;&#xA;    // Encode frames&#xA;    int samples_read = 0;&#xA;    while (samples_read &lt; data_.size()) {&#xA;        ret = av_frame_make_writable(frame);&#xA;        if (ret &lt; 0) {&#xA;            throw std::runtime_error("Frame not writable.");&#xA;        }&#xA;&#xA;        // Determine the number of samples to copy into the frame&#xA;        int frame_size = std::min<int>(codec_ctx->frame_size, (data_.size() - samples_read) / frame_width_);&#xA;        int buffer_size = frame_size * frame_width_;&#xA;&#xA;        // Clear the frame data to avoid artifacts from previous data&#xA;        std::memset(frame->data[0], 0, codec_ctx->frame_size * frame_width_);&#xA;&#xA;        // Copy the actual audio data into the frame&#xA;        std::memcpy(frame->data[0], data_.data() &#x2B; samples_read, buffer_size);&#xA;        samples_read &#x2B;= buffer_size;&#xA;&#xA;        // If the frame is partially filled, pad the remaining part with zeros&#xA;        if (frame_size &lt; codec_ctx->frame_size) {&#xA;            std::memset(frame->data[0] &#x2B; buffer_size, 0, (codec_ctx->frame_size - frame_size) * frame_width_);&#xA;        }&#xA;&#xA;        // Send the frame for encoding&#xA;        ret = avcodec_send_frame(codec_ctx, frame);&#xA;        if (ret &lt; 0) {&#xA;            throw std::runtime_error("Error sending frame for encoding.");&#xA;        }&#xA;&#xA;        // Receive and write 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 encoding frame.");&#xA;            }&#xA;&#xA;            out_file.write(reinterpret_cast(pkt->data), pkt->size);&#xA;            av_packet_unref(pkt);&#xA;        }&#xA;    }&#xA;&#xA;    // **Explicitly flush the encoder**&#xA;    ret = avcodec_send_frame(codec_ctx, nullptr);&#xA;    if (ret &lt; 0) {&#xA;        throw std::runtime_error("Error flushing the encoder.");&#xA;    }&#xA;&#xA;    // Receive and write remaining packets after flushing&#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 encoding frame during flush.");&#xA;        }&#xA;&#xA;        out_file.write(reinterpret_cast(pkt->data), pkt->size);&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;&#xA;    // Cleanup&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;    avcodec_free_context(&amp;codec_ctx);&#xA;    avformat_free_context(format_ctx);&#xA;&#xA;    return out_file;&#xA;}&#xA;</int></const></char>

    &#xA;

    I have no run time error but i see this message in console :

    &#xA;

    [libmp3lame @ 000002d26b239ac0] Trying to remove 47 more samples than there are in the queue&#xA;

    &#xA;

    I can play the exported mp3 file but there is background noise.

    &#xA;

  • 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 :

    &#xA;

    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;