Recherche avancée

Médias (0)

Mot : - Tags -/organisation

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

Autres articles (95)

  • Mediabox : ouvrir les images dans l’espace maximal pour l’utilisateur

    8 février 2011, par

    La visualisation des images est restreinte par la largeur accordée par le design du site (dépendant du thème utilisé). Elles sont donc visibles sous un format réduit. Afin de profiter de l’ensemble de la place disponible sur l’écran de l’utilisateur, il est possible d’ajouter une fonctionnalité d’affichage de l’image dans une boite multimedia apparaissant au dessus du reste du contenu.
    Pour ce faire il est nécessaire d’installer le plugin "Mediabox".
    Configuration de la boite multimédia
    Dès (...)

  • 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

  • Des sites réalisés avec MediaSPIP

    2 mai 2011, par

    Cette page présente quelques-uns des sites fonctionnant sous MediaSPIP.
    Vous pouvez bien entendu ajouter le votre grâce au formulaire en bas de page.

Sur d’autres sites (8562)

  • [ffmpeg]How to use libfdk_aac to encode pcm to constant bitrate

    27 décembre 2024, par qyt

    I have tried both libfdk_aac and aac, but the encoded PCM audio always has a variable bit rate. Why is this happening ? How can I make it encode with a constant bit rate
The code is as follows :

    


    #include &#xA;#include &#xA;#include &#xA;extern "C"&#xA;{&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavdevice></libavdevice>avdevice.h>&#xA;#include <libavfilter></libavfilter>avfilter.h>&#xA;#include <libavfilter></libavfilter>buffersrc.h>&#xA;#include <libavfilter></libavfilter>buffersink.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>avutil.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavutil></libavutil>channel_layout.h>   &#xA;#include <libswresample></libswresample>swresample.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;}&#xA;&#xA;int main(int argc, char** argv) {&#xA;    AVCodecContext* codec_context = NULL;&#xA;    const AVCodec* codec = NULL;&#xA;    AVFrame* frame = NULL;&#xA;    AVPacket* pkt = NULL;&#xA;    FILE* input_file = NULL;&#xA;    FILE* output_file = NULL;&#xA;    int ret;&#xA;&#xA;    // Open input file&#xA;    const char* input_filename = "D:\\audio\\b.pcm";&#xA;    const char* output_filename = "D:\\audio\\input.aac";&#xA;    input_file = fopen(input_filename, "rb");&#xA;    output_file = fopen(output_filename, "wb");&#xA;    if (!input_file || !output_file) {&#xA;        fprintf(stderr, "Could not open input or output file\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Find the AAC encoder&#xA;    codec = avcodec_find_encoder_by_name("libfdk_aac");&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    codec_context = avcodec_alloc_context3(codec);&#xA;    if (!codec_context) {&#xA;        fprintf(stderr, "Could not allocate audio codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Set codec parameters&#xA;    codec_context->sample_fmt = AV_SAMPLE_FMT_S16;&#xA;    codec_context->sample_rate = 44100;&#xA;    codec_context->bit_rate = 256000;&#xA;    codec_context->rc_buffer_size = codec_context->bit_rate; &#xA;    codec_context->rc_min_rate = codec_context->bit_rate;   &#xA;    codec_context->rc_max_rate = codec_context->bit_rate;  &#xA;    av_channel_layout_default(&amp;codec_context->ch_layout, 2);&#xA;    // Open codec&#xA;    if (avcodec_open2(codec_context, codec, &amp;opts) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Initialize packet&#xA;    pkt = av_packet_alloc();&#xA;    if (!pkt) {&#xA;        fprintf(stderr, "Could not allocate AVPacket\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Initialize frame&#xA;    frame = av_frame_alloc();&#xA;    frame->nb_samples = codec_context->frame_size;&#xA;    frame->format = codec_context->sample_fmt;&#xA;    frame->ch_layout.nb_channels = 2;&#xA;&#xA;    // Allocate the data buffers&#xA;    ret = av_frame_get_buffer(frame, 0);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate audio data buffers\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Main loop: read from the input file, encode, write to the output file&#xA;    while (fread(frame->data[0], 1, frame->linesize[0], input_file) == frame->linesize[0]) {&#xA;        // Send the frame to the encoder&#xA;        if (avcodec_send_frame(codec_context, frame) &lt; 0) {&#xA;            fprintf(stderr, "Error sending frame to codec\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        // Get the encoded packet&#xA;        while (avcodec_receive_packet(codec_context, pkt) == 0) {&#xA;            fwrite(pkt->data, 1, pkt->size, output_file);&#xA;            av_packet_unref(pkt);&#xA;        }&#xA;    }&#xA;&#xA;    // Flush the encoder&#xA;    avcodec_send_frame(codec_context, NULL);&#xA;    while (avcodec_receive_packet(codec_context, pkt) == 0) {&#xA;        fwrite(pkt->data, 1, pkt->size, output_file);&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;&#xA;    // Clean up&#xA;    fclose(input_file);&#xA;    fclose(output_file);&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;    avcodec_free_context(&amp;codec_context);&#xA;&#xA;    return 0;&#xA;}&#xA;&#xA;

    &#xA;

    I encoded the AAC and then used FFmpeg to write it to an MP4 file : ./ffmpeg -i input.aac -c copy output.mp4, and I checked it using MediaInfo

    &#xA;

    The PCM file is S16, 2 channels, 44100 Hz

    &#xA;

    I try to write mp4 directly and check the aac audio through mediainfo which is also a variable bitrate

    &#xA;

    #include &#xA;#include &#xA;#include &#xA;extern "C"&#xA;{&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavdevice></libavdevice>avdevice.h>&#xA;#include <libavfilter></libavfilter>avfilter.h>&#xA;#include <libavfilter></libavfilter>buffersrc.h>&#xA;#include <libavfilter></libavfilter>buffersink.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>avutil.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavutil></libavutil>channel_layout.h>   &#xA;#include <libswresample></libswresample>swresample.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;}&#xA;&#xA;int main(int argc, char** argv) {&#xA;    AVCodecContext* codec_context = NULL;&#xA;    const AVCodec* codec = NULL;&#xA;    AVFormatContext* format_context = NULL;&#xA;    AVStream* audio_stream = NULL;&#xA;    AVFrame* frame = NULL;&#xA;    AVPacket* pkt = NULL;&#xA;    FILE* input_file = NULL;&#xA;    int ret;&#xA;    int64_t next_pts = 0;&#xA;&#xA;    // Open input file&#xA;    const char* input_filename = "D:\\audio\\b.pcm";&#xA;    const char* output_filename = "D:\\audio\\input.mp4";&#xA;    input_file = fopen(input_filename, "rb");&#xA;    if (!input_file) {&#xA;        fprintf(stderr, "Could not open input file\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Find the AAC encoder&#xA;    codec = avcodec_find_encoder_by_name("libfdk_aac");&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Allocate AVFormatContext for MP4 output&#xA;    avformat_alloc_output_context2(&amp;format_context, NULL, NULL, output_filename);&#xA;    if (!format_context) {&#xA;        fprintf(stderr, "Could not allocate output context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Add audio stream to the output file&#xA;    audio_stream = avformat_new_stream(format_context, codec);&#xA;    if (!audio_stream) {&#xA;        fprintf(stderr, "Could not allocate stream\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    codec_context = avcodec_alloc_context3(codec);&#xA;    if (!codec_context) {&#xA;        fprintf(stderr, "Could not allocate audio codec context\n");&#xA;        exit(1);&#xA;    }&#xA;    audio_stream->codecpar->frame_size = codec_context->frame_size = 1024;&#xA;&#xA;    // Set codec parameters&#xA;    codec_context->sample_fmt = AV_SAMPLE_FMT_S16;&#xA;    codec_context->sample_rate = 44100;&#xA;    codec_context->bit_rate = 256000;&#xA;    codec_context->rc_buffer_size = codec_context->bit_rate;&#xA;    codec_context->rc_min_rate = codec_context->bit_rate;&#xA;    codec_context->rc_max_rate = codec_context->bit_rate;&#xA;    av_channel_layout_default(&amp;codec_context->ch_layout, 2);&#xA;&#xA;    // Copy settings to stream&#xA;    ret = avcodec_parameters_from_context(audio_stream->codecpar, codec_context);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Failed to copy codec parameters to stream\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Open codec&#xA;    if (avcodec_open2(codec_context, codec, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Open output file&#xA;    if (!(format_context->oformat->flags &amp; AVFMT_NOFILE)) {&#xA;        ret = avio_open(&amp;format_context->pb, output_filename, AVIO_FLAG_WRITE);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Could not open output file &#x27;%s&#x27;\n", output_filename);&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;&#xA;    // Write file header&#xA;    ret = avformat_write_header(format_context, NULL);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Error occurred when opening output file\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Initialize packet and frame&#xA;    pkt = av_packet_alloc();&#xA;    if (!pkt) {&#xA;        fprintf(stderr, "Could not allocate AVPacket\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    frame = av_frame_alloc();&#xA;    frame->nb_samples = codec_context->frame_size;&#xA;    frame->format = codec_context->sample_fmt;&#xA;    frame->ch_layout = codec_context->ch_layout;&#xA;&#xA;    // Allocate the data buffers&#xA;    ret = av_frame_get_buffer(frame, 0);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate audio data buffers\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Main loop: read from the input file, encode, write to the output file&#xA;    while (fread(frame->data[0], 1, frame->linesize[0], input_file) == frame->linesize[0]) {&#xA;        frame->pts = next_pts;  // Set PTS for the frame&#xA;        next_pts &#x2B;= frame->nb_samples;  // Increment the next PTS&#xA;        // Send the frame to the encoder&#xA;        if (avcodec_send_frame(codec_context, frame) &lt; 0) {&#xA;            fprintf(stderr, "Error sending frame to codec\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        // Get the encoded packet&#xA;        while (avcodec_receive_packet(codec_context, pkt) == 0) {&#xA;            pkt->pts = pkt->dts = frame->pts;&#xA;            av_packet_rescale_ts(pkt, codec_context->time_base, audio_stream->time_base);&#xA;            pkt->stream_index = audio_stream->index;&#xA;            av_interleaved_write_frame(format_context, pkt);&#xA;            av_packet_unref(pkt);&#xA;        }&#xA;    }&#xA;&#xA;    // Flush the encoder&#xA;    avcodec_send_frame(codec_context, NULL);&#xA;    while (avcodec_receive_packet(codec_context, pkt) == 0) {&#xA;        pkt->pts = pkt->dts = next_pts;&#xA;        av_packet_rescale_ts(pkt, codec_context->time_base, audio_stream->time_base);&#xA;        pkt->stream_index = audio_stream->index;&#xA;        av_interleaved_write_frame(format_context, pkt);&#xA;        av_packet_unref(pkt);&#xA;        next_pts &#x2B;= pkt->duration;&#xA;    }&#xA;&#xA;    // Write file trailer&#xA;    av_write_trailer(format_context);&#xA;&#xA;    // Clean up&#xA;    fclose(input_file);&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;    avcodec_free_context(&amp;codec_context);&#xA;    avio_closep(&amp;format_context->pb);&#xA;    avformat_free_context(format_context);&#xA;&#xA;    return 0;&#xA;}&#xA;

    &#xA;

  • FFmpeg VAAPI : GOP Size Setting Not Applied During Encoding

    17 octobre 2024, par sun tony

    I'm working on a video transcoding project using FFmpeg with VAAPI hardware acceleration, and I'm encountering an issue where the gop_size parameter is not being respected during encoding. Despite setting the gop_size to 150 in the encoder context, the output video does not have the expected GOP structure. This problem only occurs when using VAAPI for hardware acceleration—when I switch to software decoding or CUDA, the GOP size is applied correctly.

    &#xA;

    Here’s a simplified version of the relevant code where I set the gop_size :

    &#xA;

    encoder_ctx->gop_size = 150;&#xA;

    &#xA;

    I'm using FFmpeg's VAAPI for hardware-accelerated encoding, and everything else works as expected, except for this GOP size issue. Has anyone encountered this problem, or is there something specific to VAAPI that I'm missing ?

    &#xA;

    Environment :

    &#xA;

      &#xA;
    • FFmpeg version : (6.0)
    • &#xA;

    • VAAPI (Intel GPU)
    • &#xA;

    • Encoder : H.264
    • &#xA;

    &#xA;

    #include &#xA;#include &#xA;extern "C"&#xA;{&#xA;#include <libavutil></libavutil>hwcontext.h>&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;}&#xA;static AVFormatContext* ifmt_ctx = NULL, * ofmt_ctx = NULL;&#xA;static AVBufferRef* hw_device_ctx = NULL;&#xA;static AVCodecContext* decoder_ctx = NULL, * encoder_ctx = NULL;&#xA;static int video_stream = -1;&#xA;static AVStream* ost;&#xA;static int initialized = 0;&#xA;&#xA;static enum AVPixelFormat get_vaapi_format(AVCodecContext* ctx,&#xA;    const enum AVPixelFormat* pix_fmts)&#xA;{&#xA;    const enum AVPixelFormat* p;&#xA;&#xA;    for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p&#x2B;&#x2B;) {   &#xA;         if (*p == AV_PIX_FMT_VAAPI)&#xA;            return *p;&#xA;    }&#xA;&#xA;    fprintf(stderr, "Unable to decode this file using VA-API.\n");&#xA;    return AV_PIX_FMT_NONE;&#xA;}&#xA;&#xA;static int open_input_file(const char* filename)&#xA;{&#xA;    int ret;&#xA;    const AVCodec* decoder = NULL;&#xA;    AVStream* video = NULL;&#xA;    char err_buf[AV_ERROR_MAX_STRING_SIZE] = { 0, };&#xA;&#xA;    if ((ret = avformat_open_input(&amp;ifmt_ctx, filename, NULL, NULL)) &lt; 0) {&#xA;        av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Cannot open input file &#x27;%s&#x27;, Error code: %s\n", filename, err_buf);&#xA;        return ret;&#xA;    }&#xA;&#xA;    if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) &lt; 0) {&#xA;        av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Cannot find input stream information. Error code: %s\n", err_buf);&#xA;        return ret;&#xA;    }&#xA;&#xA;    ret = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &amp;decoder, 0);&#xA;    if (ret &lt; 0) {&#xA;        av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Cannot find a video stream in the input file. Error code: %s\n", err_buf);&#xA;        return ret;&#xA;    }&#xA;    video_stream = ret;&#xA;&#xA;    if (!(decoder_ctx = avcodec_alloc_context3(decoder)))&#xA;        return AVERROR(ENOMEM);&#xA;&#xA;    video = ifmt_ctx->streams[video_stream];&#xA;    if ((ret = avcodec_parameters_to_context(decoder_ctx, video->codecpar)) &lt; 0) {&#xA;&#xA;        av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "avcodec_parameters_to_context error. Error code: %s\n", err_buf);&#xA;        return ret;&#xA;    }&#xA;&#xA;    decoder_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);&#xA;    if (!decoder_ctx->hw_device_ctx) {&#xA;        fprintf(stderr, "A hardware device reference create failed.\n");&#xA;        return AVERROR(ENOMEM);&#xA;    }&#xA;    decoder_ctx->get_format = get_vaapi_format;&#xA;&#xA;    if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) &lt; 0){&#xA;    &#xA;        av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Failed to open codec for decoding.Error code: %s\n", err_buf);&#xA;    }&#xA;      &#xA;    return ret;&#xA;}&#xA;&#xA;static int encode_write(AVPacket* enc_pkt, AVFrame* frame)&#xA;{&#xA;    int ret = 0;&#xA;    char err_buf[AV_ERROR_MAX_STRING_SIZE] = { 0, };&#xA;    av_packet_unref(enc_pkt);&#xA;&#xA;    if ((ret = avcodec_send_frame(encoder_ctx, frame)) &lt; 0) {&#xA;        av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Error during encoding. Error code: %s\n", err_buf);&#xA;        goto end;&#xA;    }&#xA;    while (1) {&#xA;        ret = avcodec_receive_packet(encoder_ctx, enc_pkt);&#xA;        if (ret)&#xA;            break;&#xA;&#xA;        enc_pkt->stream_index = 0;&#xA;        av_packet_rescale_ts(enc_pkt, ifmt_ctx->streams[video_stream]->time_base,&#xA;            ofmt_ctx->streams[0]->time_base);&#xA;        ret = av_interleaved_write_frame(ofmt_ctx, enc_pkt);&#xA;        if (ret &lt; 0) {&#xA;            av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;            fprintf(stderr, "Error during writing data to output file. Error code: %s\n", err_buf);&#xA;            return -1;&#xA;        }&#xA;    }&#xA;&#xA;end:&#xA;    if (ret == AVERROR_EOF)&#xA;        return 0;&#xA;    ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);&#xA;    return ret;&#xA;}&#xA;&#xA;static int dec_enc(AVPacket* pkt, const AVCodec* enc_codec)&#xA;{&#xA;    AVFrame* frame;&#xA;    AVDictionary* opts = NULL;&#xA;&#xA;    int ret = 0;&#xA;    char err_buf[AV_ERROR_MAX_STRING_SIZE] = { 0, };&#xA;    ret = avcodec_send_packet(decoder_ctx, pkt);&#xA;    if (ret &lt; 0) {&#xA;        av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Error during decoding.Error code : %s\n", err_buf);&#xA;        return ret;&#xA;    }&#xA;&#xA;    while (ret >= 0) {&#xA;        if (!(frame = av_frame_alloc()))&#xA;            return AVERROR(ENOMEM);&#xA;&#xA;        ret = avcodec_receive_frame(decoder_ctx, frame);&#xA;        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {&#xA;            av_frame_free(&amp;frame);&#xA;            return 0;&#xA;        }&#xA;        else if (ret &lt; 0) {&#xA;            av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;            fprintf(stderr, "Error while decoding.Error code : %s\n", err_buf);&#xA;            goto fail;&#xA;        }&#xA;&#xA;        if (!initialized) {&#xA;            /* we need to ref hw_frames_ctx of decoder to initialize encoder&#x27;s codec.&#xA;               Only after we get a decoded frame, can we obtain its hw_frames_ctx */&#xA;            encoder_ctx->hw_frames_ctx = av_buffer_ref(decoder_ctx->hw_frames_ctx);&#xA;            if (!encoder_ctx->hw_frames_ctx) {&#xA;                ret = AVERROR(ENOMEM);&#xA;                goto fail;&#xA;            }&#xA;            /* set AVCodecContext Parameters for encoder, here we keep them stay&#xA;             * the same as decoder.&#xA;             * xxx: now the sample can&#x27;t handle resolution change case.&#xA;             */&#xA;            encoder_ctx->time_base = av_inv_q(decoder_ctx->framerate);&#xA;            encoder_ctx->pix_fmt = AV_PIX_FMT_VAAPI;&#xA;            encoder_ctx->width = decoder_ctx->width;&#xA;            encoder_ctx->height = decoder_ctx->height;&#xA;            encoder_ctx->gop_size = 150;&#xA;            av_dict_set(&amp;opts, "g", "150", 0); // gop_size 설정&#xA;&#xA;            if ((ret = avcodec_open2(encoder_ctx, enc_codec, &amp;opts)) &lt; 0) {&#xA;                av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;                fprintf(stderr, "Failed to open encode codec. Error code : %s\n", err_buf);&#xA;                goto fail;&#xA;            }&#xA;&#xA;            if (!(ost = avformat_new_stream(ofmt_ctx, enc_codec))) {&#xA;                fprintf(stderr, "Failed to allocate stream for output format.\n");&#xA;                ret = AVERROR(ENOMEM);&#xA;                goto fail;&#xA;            }&#xA;&#xA;            ost->time_base = encoder_ctx->time_base;&#xA;            ret = avcodec_parameters_from_context(ost->codecpar, encoder_ctx);&#xA;            if (ret &lt; 0) {&#xA;                av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;                fprintf(stderr, "Failed to copy the stream parameters. Error code : %s\n", err_buf);&#xA;                goto fail;&#xA;            }&#xA;&#xA;            /* write the stream header */&#xA;            if ((ret = avformat_write_header(ofmt_ctx, NULL)) &lt; 0) {&#xA;                av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;                fprintf(stderr, "Error while writing stream header. Error code : %s\n", err_buf);&#xA;                goto fail;&#xA;            }&#xA;&#xA;            initialized = 1;&#xA;        }&#xA;&#xA;        if ((ret = encode_write(pkt, frame)) &lt; 0)&#xA;            fprintf(stderr, "Error during encoding and writing.\n");&#xA;&#xA;    fail:&#xA;        av_frame_free(&amp;frame);&#xA;        if (ret &lt; 0)&#xA;            return ret;&#xA;    }&#xA;    return 0;&#xA;}&#xA;&#xA;int main(int argc, char** argv)&#xA;{&#xA;    const AVCodec* enc_codec;&#xA;    int ret = 0;&#xA;    AVPacket* dec_pkt;&#xA;    char err_buf[AV_ERROR_MAX_STRING_SIZE] = { 0, };&#xA;&#xA;    if (argc != 4) {&#xA;        fprintf(stderr, "Usage: %s <input file="file" /> <encode codec="codec"> <output file="file">\n"&#xA;            "The output format is guessed according to the file extension.\n"&#xA;            "\n", argv[0]);&#xA;        return -1;&#xA;    }&#xA;&#xA;    ret = av_hwdevice_ctx_create(&amp;hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, NULL, NULL, 0);&#xA;    if (ret &lt; 0) {&#xA;        av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Failed to create a VAAPI device. Error code : %s\n", err_buf);&#xA;        return -1;&#xA;    }&#xA;&#xA;    dec_pkt = av_packet_alloc();&#xA;    if (!dec_pkt) {&#xA;        fprintf(stderr, "Failed to allocate decode packet\n");&#xA;        goto end;&#xA;    }&#xA;&#xA;    if ((ret = open_input_file(argv[1])) &lt; 0)&#xA;        goto end;&#xA;&#xA;    if (!(enc_codec = avcodec_find_encoder_by_name(argv[2]))) {&#xA;        fprintf(stderr, "Could not find encoder &#x27;%s&#x27;\n", argv[2]);&#xA;        ret = -1;&#xA;        goto end;&#xA;    }&#xA;&#xA;    if ((ret = (avformat_alloc_output_context2(&amp;ofmt_ctx, NULL, NULL, argv[3]))) &lt; 0) {&#xA;        av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Failed to deduce output format from file extension. Error code : %s\n", err_buf);&#xA;        goto end;&#xA;    }&#xA;&#xA;    if (!(encoder_ctx = avcodec_alloc_context3(enc_codec))) {&#xA;        ret = AVERROR(ENOMEM);&#xA;        goto end;&#xA;    }&#xA;&#xA;    ret = avio_open(&amp;ofmt_ctx->pb, argv[3], AVIO_FLAG_WRITE);&#xA;    if (ret &lt; 0) {&#xA;        av_strerror(ret, err_buf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Cannot open output file. Error code : %s\n", err_buf);&#xA;        goto end;&#xA;    }&#xA;&#xA;    /* read all packets and only transcoding video */&#xA;    while (ret >= 0) {&#xA;        if ((ret = av_read_frame(ifmt_ctx, dec_pkt)) &lt; 0)&#xA;            break;&#xA;&#xA;        if (video_stream == dec_pkt->stream_index)&#xA;            ret = dec_enc(dec_pkt, enc_codec);&#xA;&#xA;        av_packet_unref(dec_pkt);&#xA;    }&#xA;&#xA;    /* flush decoder */&#xA;    av_packet_unref(dec_pkt);&#xA;    ret = dec_enc(dec_pkt, enc_codec);&#xA;&#xA;    /* flush encoder */&#xA;    ret = encode_write(dec_pkt, NULL);&#xA;&#xA;    /* write the trailer for output stream */&#xA;    av_write_trailer(ofmt_ctx);&#xA;&#xA;end:&#xA;    avformat_close_input(&amp;ifmt_ctx);&#xA;    avformat_close_input(&amp;ofmt_ctx);&#xA;    avcodec_free_context(&amp;decoder_ctx);&#xA;    avcodec_free_context(&amp;encoder_ctx);&#xA;    av_buffer_unref(&amp;hw_device_ctx);&#xA;    av_packet_free(&amp;dec_pkt);&#xA;    return ret;&#xA;}&#xA;</output></encode>

    &#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;