Recherche avancée

Médias (39)

Mot : - Tags -/audio

Autres articles (40)

  • Submit bugs and patches

    13 avril 2011

    Unfortunately a software is never perfect.
    If you think you have found a bug, report it using our ticket system. Please to help us to fix it by providing the following information : the browser you are using, including the exact version as precise an explanation as possible of the problem if possible, the steps taken resulting in the problem a link to the site / page in question
    If you think you have solved the bug, fill in a ticket and attach to it a corrective patch.
    You may also (...)

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

  • Initialisation de MediaSPIP (préconfiguration)

    20 février 2010, par

    Lors de l’installation de MediaSPIP, celui-ci est préconfiguré pour les usages les plus fréquents.
    Cette préconfiguration est réalisée par un plugin activé par défaut et non désactivable appelé MediaSPIP Init.
    Ce plugin sert à préconfigurer de manière correcte chaque instance de MediaSPIP. Il doit donc être placé dans le dossier plugins-dist/ du site ou de la ferme pour être installé par défaut avant de pouvoir utiliser le site.
    Dans un premier temps il active ou désactive des options de SPIP qui ne le (...)

Sur d’autres sites (9254)

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

    22 novembre 2023, par Clockman

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

    


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

    


    avformat-60.dll!avformat_write_header() C
avformat-60.dll!ff_write_chained()  C
avformat-60.dll!ff_write_chained()  C
avformat-60.dll!av_write_frame()    C
FfmpegPractice.exe!main() Line 215  C++


    


    Some things I have tried

    


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

    


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

    


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

    


    This is the code

    


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

    &#xA;

    Error/Exception

    &#xA;

    The exception is thrown when I call

    &#xA;

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

    &#xA;

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

    &#xA;

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

    &#xA;

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

    &#xA;

  • Bad src image ptrs converting YUV to RGB after H264 decoding with libav and c++

    31 octobre 2023, par Sebastian DELLING

    I am getting "bad src image ptrs" errors when trying to convert my frames to RGB with sws_scale after decoding frames from a H264 file and cannot figure out wht is going wrong.

    &#xA;

    I checked what is causing the error and found the check_image_pointers function in swscale.c which validates that the planes and line sizes needed for the pixel format (av_pix_fmt_desc_get) are present in the given data which seems not to be the case with my data.

    &#xA;

    The written pgm files look ok to me, also replaying the file works.

    &#xA;

    I printed the corresponding data of my frame. The problem seems that planes 1 and 2 have lines sizes of 0. All 3 of them seem to have data. Plane 0 line size is three times image width which is also confusing to me.

    &#xA;

    Here is my output :

    &#xA;

    Have videoStreamIndex 0 codec id: 27&#xA;saving frame 1 C:\\tmp\\output-frame-1.pgm colorspace 2 pix_fmt 0 w: 3840 h: 2160&#xA;Required:&#xA;plane 0 : 0&#xA;plane 1 : 1&#xA;plane 2 : 2&#xA;plane 3 : 0&#xA;Present:&#xA;Frame plane 0: 1 , 11520&#xA;Frame plane 1: 1 , 0&#xA;Frame plane 2: 1 , 0&#xA;Frame plane 3: 0 , 0&#xA;Frame plane 4: 0 , 0&#xA;Frame plane 5: 0 , 0&#xA;Frame plane 6: 0 , 0&#xA;Frame plane 7: 0 , 0&#xA;

    &#xA;

    Here the whole code of my application, the issues occurs in method decode :

    &#xA;

    #include <iostream>&#xA;#include <cstring>&#xA;#include <cstdio>&#xA;#include <cstdint>&#xA;#include <string>&#xA;#include <iostream>&#xA;#include <chrono>&#xA;&#xA;// #include <opencv2></opencv2>highgui.hpp>&#xA;// #include <opencv2></opencv2>opencv.hpp>&#xA;&#xA;extern "C"&#xA;{&#xA;&#xA;#include <libswscale></libswscale>swscale.h>&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavfilter></libavfilter>buffersink.h>&#xA;#include <libavfilter></libavfilter>buffersrc.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavutil></libavutil>pixdesc.h>&#xA;#include <libavutil></libavutil>display.h>&#xA;#include "libavutil/imgutils.h"&#xA;}&#xA;&#xA;#define INBUF_SIZE 4096&#xA;class H264Decoder&#xA;{&#xA;public:&#xA;    H264Decoder(const std::string &amp;inputFilename, const std::string &amp;outputFilenamePrefix)&#xA;    {&#xA;&#xA;        // Open input file&#xA;        if (avformat_open_input(&amp;formatContext, inputFilename.c_str(), nullptr, nullptr) != 0)&#xA;        {&#xA;            throw std::runtime_error("Could not open input file");&#xA;        }&#xA;&#xA;        if (avformat_find_stream_info(formatContext, nullptr) &lt; 0)&#xA;        {&#xA;            throw std::runtime_error("Could not find stream information");&#xA;        }&#xA;&#xA;        // Find H.264 video stream&#xA;        for (unsigned i = 0; i &lt; formatContext->nb_streams; i&#x2B;&#x2B;)&#xA;        {&#xA;            if (formatContext->streams[i]->codecpar->codec_id == AV_CODEC_ID_H264)&#xA;            {&#xA;                videoStreamIndex = i;&#xA;                std::cout &lt;&lt; "Have videoStreamIndex " &lt;&lt; videoStreamIndex &lt;&lt; " codec id: " &lt;&lt; formatContext->streams[i]->codecpar->codec_id &lt;&lt; std::endl;&#xA;                break;&#xA;            }&#xA;        }&#xA;&#xA;        if (videoStreamIndex == -1)&#xA;        {&#xA;            throw std::runtime_error("H.264 video stream not found");&#xA;        }&#xA;&#xA;        // Initialize codec and codec context&#xA;        const AVCodec *codec = avcodec_find_decoder(formatContext->streams[videoStreamIndex]->codecpar->codec_id);&#xA;        if (!codec)&#xA;        {&#xA;            throw std::runtime_error("Codec not found");&#xA;        }&#xA;&#xA;        parser = av_parser_init(codec->id);&#xA;        if (!parser)&#xA;        {&#xA;            throw std::runtime_error("parser not found");&#xA;        }&#xA;&#xA;        codecContext = avcodec_alloc_context3(codec);&#xA;        if (!codecContext)&#xA;        {&#xA;            throw std::runtime_error("Could not allocate codec context");&#xA;        }&#xA;&#xA;        if (avcodec_open2(codecContext, codec, nullptr) &lt; 0)&#xA;        {&#xA;            throw std::runtime_error("Could not open codec");&#xA;        }&#xA;&#xA;        // Initialize frame&#xA;        frame = av_frame_alloc();&#xA;        frame->format = AV_PIX_FMT_YUV420P;&#xA;        if (!frame)&#xA;        {&#xA;            throw std::runtime_error("Could not allocate frame");&#xA;        }&#xA;&#xA;        inputPacket = av_packet_alloc();&#xA;        if (!inputPacket)&#xA;        {&#xA;            throw std::runtime_error("Could not allocate packet");&#xA;        }&#xA;&#xA;        inputFilename_ = inputFilename;&#xA;        outputFilenamePrefix_ = outputFilenamePrefix;&#xA;    }&#xA;&#xA;    void decode()&#xA;    {&#xA;        char buf[1024];&#xA;        int ret;&#xA;&#xA;        ret = avcodec_send_packet(codecContext, inputPacket);&#xA;        if (ret &lt; 0)&#xA;        {&#xA;            fprintf(stderr, "Error sending a packet for decoding\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        while (ret >= 0)&#xA;        {&#xA;            ret = avcodec_receive_frame(codecContext, frame);&#xA;            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)&#xA;                return;&#xA;            else if (ret &lt; 0)&#xA;            {&#xA;                fprintf(stderr, "Error during decoding\n");&#xA;                exit(1);&#xA;            }&#xA;&#xA;            /* the picture is allocated by the decoder. no need to&#xA;               free it */&#xA;            snprintf(buf, sizeof(buf), "%s-%" PRId64 ".pgm", outputFilenamePrefix_.c_str(), codecContext->frame_num);&#xA;&#xA;            std::cout &lt;&lt; "saving frame " &lt;&lt; codecContext->frame_num &lt;&lt; " " &lt;&lt; buf &lt;&lt; " colorspace " &lt;&lt; frame->colorspace &lt;&lt; " pix_fmt " &lt;&lt; codecContext->pix_fmt &lt;&lt; " w: " &lt;&lt; frame->width &lt;&lt; " h: " &lt;&lt; frame->height &lt;&lt; std::endl;&#xA;&#xA;            SwsContext *sws_ctx = NULL;&#xA;&#xA;            sws_ctx = sws_getContext(codecContext->width,&#xA;                                     codecContext->height,&#xA;                                     codecContext->pix_fmt,&#xA;                                     codecContext->width,&#xA;                                     codecContext->height,&#xA;                                     AV_PIX_FMT_RGB24,&#xA;                                     SWS_BICUBIC,&#xA;                                     NULL,&#xA;                                     NULL,&#xA;                                     NULL);&#xA;&#xA;            AVFrame *frame2 = av_frame_alloc();&#xA;            int num_bytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, codecContext->width, codecContext->height, 32);&#xA;            uint8_t *frame2_buffer = (uint8_t *)av_malloc(num_bytes * sizeof(uint8_t));&#xA;            av_image_fill_arrays(frame2->data, frame->linesize, frame2_buffer, AV_PIX_FMT_RGB24, codecContext->width, codecContext->height, 32);&#xA;&#xA;            const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(codecContext->pix_fmt);&#xA;            std::cout &lt;&lt; "Required:" &lt;&lt; std::endl;&#xA;            for (int i = 0; i &lt; 4; i&#x2B;&#x2B;)&#xA;            {&#xA;                int plane = desc->comp[i].plane;&#xA;                std::cout &lt;&lt; "plane " &lt;&lt; i &lt;&lt; " : " &lt;&lt; plane &lt;&lt; std::endl;&#xA;            }&#xA;            std::cout &lt;&lt; "Present:" &lt;&lt; std::endl;&#xA;            for (int i = 0; i &lt; AV_NUM_DATA_POINTERS; &#x2B;&#x2B;i)&#xA;            {&#xA;                std::cout &lt;&lt; "Frame plane " &lt;&lt; i &lt;&lt; ": " &lt;&lt; static_cast<bool>(frame->data[i]) &lt;&lt; " , " &lt;&lt; frame->linesize[i] &lt;&lt; std::endl;&#xA;            }&#xA;&#xA;            sws_scale(sws_ctx, frame->data,&#xA;                      frame->linesize, 0, codecContext->height,&#xA;                      frame2->data, frame2->linesize);&#xA;&#xA;            // cv::Mat img(frame2->height, frame2->width, CV_8UC3, frame2->data[0]);&#xA;            // cv::imshow("Image", img);&#xA;&#xA;            pgm_save(frame->data[0], frame->linesize[0],&#xA;                     frame->width, frame->height, buf);&#xA;        }&#xA;    }&#xA;&#xA;    ~H264Decoder()&#xA;    {&#xA;        avformat_close_input(&amp;formatContext);&#xA;        avformat_free_context(formatContext);&#xA;        avcodec_free_context(&amp;codecContext);&#xA;        av_frame_free(&amp;frame);&#xA;        av_packet_free(&amp;inputPacket);&#xA;    }&#xA;&#xA;    void readAndDecode()&#xA;    {&#xA;        FILE *f;&#xA;        uint8_t inbuf[INBUF_SIZE &#x2B; AV_INPUT_BUFFER_PADDING_SIZE];&#xA;        uint8_t *data;&#xA;        size_t data_size;&#xA;        int ret;&#xA;        int eof;&#xA;        f = fopen(inputFilename_.c_str(), "rb");&#xA;        auto start = std::chrono::high_resolution_clock::now();&#xA;        do&#xA;        {&#xA;            /* read raw data from the input file */&#xA;            data_size = fread(inbuf, 1, INBUF_SIZE, f);&#xA;            if (ferror(f))&#xA;                break;&#xA;            eof = !data_size;&#xA;&#xA;            /* use the parser to split the data into frames */&#xA;            data = inbuf;&#xA;            while (data_size > 0 || eof)&#xA;            {&#xA;                ret = av_parser_parse2(parser, codecContext, &amp;inputPacket->data, &amp;inputPacket->size,&#xA;                                       data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);&#xA;                if (ret &lt; 0)&#xA;                {&#xA;                    fprintf(stderr, "Error while parsing\n");&#xA;                    exit(1);&#xA;                }&#xA;                data &#x2B;= ret;&#xA;                data_size -= ret;&#xA;&#xA;                if (inputPacket->size)&#xA;                {&#xA;                    decode();&#xA;                }&#xA;                else if (eof)&#xA;                {&#xA;                    break;&#xA;                }&#xA;            }&#xA;        } while (!eof);&#xA;        auto diff = std::chrono::high_resolution_clock::now() - start;&#xA;        std::cout &lt;&lt; "Decoded " &lt;&lt; codecContext->frame_num &lt;&lt; " frames in " &lt;&lt; std::chrono::duration_cast(diff).count() &lt;&lt; " ms" &lt;&lt; std::endl;&#xA;    }&#xA;&#xA;private:&#xA;    AVFormatContext *formatContext = nullptr;&#xA;    AVCodecContext *codecContext = nullptr;&#xA;    AVCodecParserContext *parser;&#xA;    AVFrame *frame = nullptr;&#xA;    AVFrame *frameRgb = nullptr;&#xA;    AVPacket *inputPacket = nullptr;&#xA;    int videoStreamIndex = -1;&#xA;    std::string inputFilename_;&#xA;    std::string outputFilenamePrefix_;&#xA;&#xA;    static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, const char *filename)&#xA;    {&#xA;        FILE *f = fopen(filename, "wb");&#xA;        if (!f)&#xA;        {&#xA;            std::cout &lt;&lt; "Error opening file for saving PGM" &lt;&lt; std::endl;&#xA;            exit(1);&#xA;        }&#xA;&#xA;        fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);&#xA;        for (int i = 0; i &lt; ysize; i&#x2B;&#x2B;)&#xA;            fwrite(buf &#x2B; i * wrap, 1, xsize, f);&#xA;&#xA;        fclose(f);&#xA;    }&#xA;};&#xA;&#xA;int main(int argc, char *argv[])&#xA;{&#xA;    if (argc &lt; 2)&#xA;    {&#xA;        std::cout &lt;&lt; "Please provide input file name as parameter" &lt;&lt; std::endl;&#xA;    }&#xA;&#xA;    std::string inputFilename = argv[1];&#xA;    std::string outputFilenamePrefix = "C:\\tmp\\output-frame";&#xA;&#xA;    try&#xA;    {&#xA;&#xA;        H264Decoder decoder(inputFilename, outputFilenamePrefix);&#xA;        decoder.readAndDecode();&#xA;    }&#xA;    catch (const std::exception &amp;e)&#xA;    {&#xA;        std::cout &lt;&lt; "Error: " &lt;&lt; e.what() &lt;&lt; std::endl;&#xA;        return 1;&#xA;    }&#xA;&#xA;    return 0;&#xA;}&#xA;</bool></chrono></iostream></string></cstdint></cstdio></cstring></iostream>

    &#xA;

  • Merge file without data loss using FFmpeg inside of WASM

    9 septembre 2023, par Deji

    Edit : I'm rewriting this entire question

    &#xA;

    Goal : To reconstruct a video from its pieces/chunks from a network stream inside of an @ffmpeg/ffmpeg worker

    &#xA;

    Problems :

    &#xA;

      &#xA;
    1. Video chunks/pieces which come after the first piece/chunk are reported by @ffmpeg/ffmpeg to have invalid data, as seen in the log below :
    2. &#xA;

    &#xA;

    {&#xA;  "type": "stderr",&#xA;  "message": "video-0_chunk-1.part: Invalid data found when processing input"&#xA;}&#xA;

    &#xA;

      &#xA;
    1. How would I merge these chunks/pieces to reconstruct the full video using @ffmpeg/ffmpeg (after solving the first issue above)
    2. &#xA;

    &#xA;

    My current code situation :

    &#xA;

      &#xA;
    1. For merging the video pieces
    2. &#xA;

    &#xA;

    const constructFile = async (chunks: Uint8Array[], queueId: number) => {&#xA;  await Promise.all(&#xA;    chunks.map(async (chunk, index) => {&#xA;      const chunkFile = `video-${queueId}_chunk-${index}`;&#xA;      await ffmpeg.writeFile(chunkFile, chunk);&#xA;&#xA;      // Return information about newly created file&#xA;      ffmpeg.exec(["-i", chunkFile]);&#xA;    })&#xA;  );&#xA;};&#xA;

    &#xA;

    I'm reading the logs/output for

    &#xA;

    ffmpeg.exec([&#x27;-i&#x27;, chunkFile])&#xA;

    &#xA;

    using

    &#xA;

    ffmpeg.on(&#x27;log&#x27;, (log) => console.log(log))&#xA;

    &#xA;

      &#xA;
    1. For fetching the videos using streams
    2. &#xA;

    &#xA;

    await useFetch(Capacitor.convertFileSrc(file.path), {&#xA;  responseType: "stream",&#xA;&#xA;  onResponse: async ({ response }) => {&#xA;    if (response.body) {&#xA;      const reader = response.body.getReader();&#xA;&#xA;      while (true) {&#xA;        const { done, value } = await reader.read();&#xA;&#xA;        if (done) break;&#xA;        file.chunks.push(value);&#xA;      }&#xA;      reader.releaseLock();&#xA;    }&#xA;  },&#xA;});&#xA;

    &#xA;

    Note : file.chunks is linked to a reactive value which is passed to constructFile() when initialized

    &#xA;

    These are the logs I get from the code currently above :

    &#xA;

    chunk-4OF65L5M.js:2710 <suspense> is an experimental feature and its API will likely change.&#xA;(index):298 native App.addListener (#25407936)&#xA;(index):298 native FilePicker.pickVideos (#25407937)&#xA;(index):272 result FilePicker.pickVideos (#25407937)&#xA;(index):298 native VideoEditor.thumbnail (#25407938)&#xA;(index):272 result VideoEditor.thumbnail (#25407938)&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;ffmpeg version 5.1.3 Copyright (c) 2000-2022 the FFmpeg developers&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  built with emcc (Emscripten gcc/clang-like repla…3.1.40 (5c27e79dd0a9c4e27ef2326841698cdd4f6b5784)&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  configuration: --target-os=none --arch=x86_32 --…e-libfreetype --enable-libfribidi --enable-libass&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavutil      57. 28.100 / 57. 28.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavcodec     59. 37.100 / 59. 37.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavformat    59. 27.100 / 59. 27.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavdevice    59.  7.100 / 59.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavfilter     8. 44.100 /  8. 44.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libswscale      6.  7.100 /  6.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libswresample   4.  7.100 /  4.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libpostproc    56.  6.100 / 56.  6.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: "Input #0, mov,mp4,m4a,3gp,3g2,mj2, from &#x27;video-0_chunk-0&#x27;:"}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  Metadata:&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;    major_brand     : mp42&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;    minor_version   : 0&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;    compatible_brands: isommp42&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;    creation_time   : 2022-11-29T14:46:32.000000Z&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  Duration: 00:00:51.50, start: 0.000000, bitrate: 81 kb/s&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  Stream #0:0[0x1](und): Video: h264 (High) (avc1 …6], 259 kb/s, 30 fps, 30 tbr, 15360 tbn (default)&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;    Metadata:&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;      creation_time   : 2022-11-29T14:46:32.000000Z&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;      handler_name    : ISO Media file produced by Google Inc. Created on: 11/29/2022.&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;      vendor_id       : [0][0][0][0]&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0…706D), 44100 Hz, stereo, fltp, 127 kb/s (default)&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;    Metadata:&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;      creation_time   : 2022-11-29T14:46:32.000000Z&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;      handler_name    : ISO Media file produced by Google Inc. Created on: 11/29/2022.&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;      vendor_id       : [0][0][0][0]&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;At least one output file must be specified&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;Aborted()&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;ffmpeg version 5.1.3 Copyright (c) 2000-2022 the FFmpeg developers&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  built with emcc (Emscripten gcc/clang-like repla…3.1.40 (5c27e79dd0a9c4e27ef2326841698cdd4f6b5784)&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  configuration: --target-os=none --arch=x86_32 --…e-libfreetype --enable-libfribidi --enable-libass&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavutil      57. 28.100 / 57. 28.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavcodec     59. 37.100 / 59. 37.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavformat    59. 27.100 / 59. 27.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavdevice    59.  7.100 / 59.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavfilter     8. 44.100 /  8. 44.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libswscale      6.  7.100 /  6.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libswresample   4.  7.100 /  4.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libpostproc    56.  6.100 / 56.  6.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;video-0_chunk-1: Invalid data found when processing input&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;Aborted()&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;ffmpeg version 5.1.3 Copyright (c) 2000-2022 the FFmpeg developers&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  built with emcc (Emscripten gcc/clang-like repla…3.1.40 (5c27e79dd0a9c4e27ef2326841698cdd4f6b5784)&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  configuration: --target-os=none --arch=x86_32 --…e-libfreetype --enable-libfribidi --enable-libass&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavutil      57. 28.100 / 57. 28.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavcodec     59. 37.100 / 59. 37.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavformat    59. 27.100 / 59. 27.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavdevice    59.  7.100 / 59.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavfilter     8. 44.100 /  8. 44.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libswscale      6.  7.100 /  6.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libswresample   4.  7.100 /  4.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libpostproc    56.  6.100 / 56.  6.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;video-0_chunk-2: Invalid data found when processing input&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;Aborted()&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;ffmpeg version 5.1.3 Copyright (c) 2000-2022 the FFmpeg developers&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  built with emcc (Emscripten gcc/clang-like repla…3.1.40 (5c27e79dd0a9c4e27ef2326841698cdd4f6b5784)&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  configuration: --target-os=none --arch=x86_32 --…e-libfreetype --enable-libfribidi --enable-libass&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavutil      57. 28.100 / 57. 28.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavcodec     59. 37.100 / 59. 37.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavformat    59. 27.100 / 59. 27.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavdevice    59.  7.100 / 59.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavfilter     8. 44.100 /  8. 44.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libswscale      6.  7.100 /  6.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libswresample   4.  7.100 /  4.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libpostproc    56.  6.100 / 56.  6.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;video-0_chunk-3: Invalid data found when processing input&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;Aborted()&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;ffmpeg version 5.1.3 Copyright (c) 2000-2022 the FFmpeg developers&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  built with emcc (Emscripten gcc/clang-like repla…3.1.40 (5c27e79dd0a9c4e27ef2326841698cdd4f6b5784)&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  configuration: --target-os=none --arch=x86_32 --…e-libfreetype --enable-libfribidi --enable-libass&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavutil      57. 28.100 / 57. 28.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavcodec     59. 37.100 / 59. 37.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavformat    59. 27.100 / 59. 27.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavdevice    59.  7.100 / 59.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libavfilter     8. 44.100 /  8. 44.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libswscale      6.  7.100 /  6.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libswresample   4.  7.100 /  4.  7.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;  libpostproc    56.  6.100 / 56.  6.100&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;video-0_chunk-4: Invalid data found when processing input&#x27;}&#xA;Processing.vue:135 {type: &#x27;stderr&#x27;, message: &#x27;Aborted()&#x27;}&#xA;</suspense>

    &#xA;

    Notes :

    &#xA;

      &#xA;
    1. The sections which start with Processing.vue come from the logging system I've setup.
    2. &#xA;

    3. The pieces/chunks gotten from the network where stored in exactly the same order in which they came
    4. &#xA;

    5. If you've seen the old question, the ReferenceError happens as a result of HMR by Vite&#xA;
        &#xA;
      1. Similar to this, some logs were repeated twice because I was actively changing some things and the component had to rerun from the start
      2. &#xA;

      &#xA;

    6. &#xA;

    &#xA;

    Summary : If my problem is still not clear, you could provide another way of fetching a large file (video) from a network, loading the file into memory and passing the file data to @ffmpeg/ffmpeg for further processing

    &#xA;