Newest 'ffmpeg' Questions - Stack Overflow

http://stackoverflow.com/questions/tagged/ffmpeg

Les articles publiés sur le site

  • Try to concatenate videos using FFmpeg Package. My videos concatenate correctly but those that i record from fornt camera rotate 90' in concatenate

    24 avril, par Ahmad Akram

    Here is my code where I pass a list of image paths that concatenate. I am facing an issue with the front camera video. When concatenated completely some videos rotate 90 degrees.

    Future mergeVideos(List videoPaths) async {
        VideoHelper.showInSnackBar('Videos merged Start', context);
        String outputPath = await VideoHelper.generateOutputPath();
        FlutterFFmpeg flutterFFmpeg = FlutterFFmpeg();
    
        // Create a text file containing the paths of the videos to concatenate
        String fileListPath =
            '${(await getTemporaryDirectory()).path}/fileList.txt';
        File fileList = File(fileListPath);
        await fileList
            .writeAsString(videoPaths.map((path) => 'file \'$path\'').join('\n'));
    
        // Run FFmpeg command to concatenate videos
        // String command = '-f concat -safe 0 -i $fileListPath -c copy $outputPath';
    
        String command =
            '-f concat -safe 0 -i $fileListPath -vf "transpose=1" -c:a copy $outputPath';
    
        VideoHelper.showInSnackBar('command Start', context);
        await flutterFFmpeg.execute(command).then((value) {
          if (value == 0) {
            print("Output Path : $outputPath");
            VideoHelper.showInSnackBar('Videos merged successfully', context);
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => VideoPlayerScreen(
                          videoFile: XFile(outputPath),
                        )));
          } else {
            VideoHelper.showInSnackBar(
                'Error merging videos  ::::: returnCode=== $value ', context);
          }
        });
      }
    
  • Why the source order matters on working with multiple media sources in a single AVFormatContext ?

    24 avril, par Mehmet YILMAZ

    avformat_open_input() deletes the AVFormatContext* and returns -6 when the source order changes.

    I am trying to open multiple media sources dynamically with different(mixed) formats and codecs in a single context (AVFormatContext).

    My media sources are a BlackMagic DeckLink Duo SDI input as first source and an mp4 file or rtsp stream as second.

    When I order to open (avformat_open_input()) the source 2 (RTSP or MP4 file) at first and then open the BlackMagic DeckLink Duo, proceed as expected.

    But when I change the order, and first open the DeckLink and then try to open RTSP stream or MP4 file, as I inspected in the step debugger; AVFormatContext* deleting in the av_open_input() function and it returns -6 as result.

    Please find the simple error reproduction code snappet below;

    AVFormatContext* context{avformat_alloc_context()};
    const char* url_source1{"DeckLink Duo (1)"};
    const AVInputFormat* format_source1{av_find_input_format("decklink")};
    
    const char* url_source2{"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"};
    
    // Open the first media input
    int result = avformat_open_input(&context, url_source1, format_source1, NULL);
    
    if(result < 0) {
      exit(1);
    }
    
    // Open the second media input
    // This function in current order deletes the context and returns -6
    result = avformat_open_input(&context, url_source2, NULL, NULL);
    if(result < 0) {
      exit(1);
    }
    
    // Since the context has been deleted in previous step, segmentation fault accours here!
    result = avformat_find_stream_info(context, NULL);
    if(result < 0) {
      exit(1);
    }
    
    std::cout << "Total number of streams: " << context->nb_streams << std::endl;
    
    

    But When I change the order and call the avformat_open_input() first for the mp4 file and then the DeckLink device as following it proceed as expected, no error.

    AVFormatContext* context{avformat_alloc_context()};
    const char* url_source1{"DeckLink Duo (1)"};
    const AVInputFormat* format_source1{av_find_input_format("decklink")};
    
    const char* url_source2{"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"};
    
    
    // Open the second media input
    int result = avformat_open_input(&context, url_source2, NULL, NULL);
    if(result < 0) {
      exit(1);
    }
    
    
    // Open the first media input
    result = avformat_open_input(&context, url_source1, format_source1, NULL);
    
    if(result < 0) {
      exit(1);
    }
    
    
    result = avformat_find_stream_info(context, NULL);
    if(result < 0) {
      exit(1);
    }
    
    std::cout << "Total number of streams: " << context->nb_streams << std::endl;
    
    
  • Decode AAC with FFmpeg in RTP packet

    24 avril, par Tran Toan

    I'm trying to decode AAC in RTP packet with FFmpeg. According to rfc document the RTP payload is audioMuxElement mapped directly. I try remove the RTP header and read the remaining bytes to AVPacket struct, but the avcodec_decode_audio4() returning error -1094995529. Here is the code:

    #include "stdafx.h"
    #include "stdio.h"
    #include "conio.h"
    
    
    extern "C" 
    {
    #ifndef __STDC_CONSTANT_MACROS
    #define __STDC_CONSTANT_MACROS
    #endif
    #include 
    #include 
    }
    
    
    // compatibility with newer API
    #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
    #define av_frame_alloc avcodec_alloc_frame
    #define av_frame_free avcodec_free_frame
    #endif
    
    #define AUDIO_INBUF_SIZE 20480
    #define AUDIO_REFILL_THRESH 4096
    #define SAMPLE_RATE 44100
    #define CHANNEL_NUM 2
    
    static void decode_packet();
    
    int main(int argc, char *argv[]) {
        decode_packet();
        getch();
        return 0;
    }
    
    
    
    static void decode_packet()
    {
        const char *filename = "D:\\NoRTP_AACPacket.dat";
        const char *outfilename = "D:\\test2.pcm";
    
        AVCodec *codec;
        AVFormatContext   *pFormatCtx = NULL;
        AVCodecContext * pCodecCtx= NULL;
        int len;
        FILE *f, *outfile;
        uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
        AVPacket avpkt;
        AVFrame *decoded_frame = NULL;
    
        av_register_all();
    
        av_init_packet(&avpkt);
    
        printf("Decode audio file %s to %s\n", filename, outfilename);
    
        // Find the decoder for the audio stream
        codec=avcodec_find_decoder(AV_CODEC_ID_AAC_LATM);
        if(codec==NULL) {
            fprintf(stderr, "Codec not found\n");
            return; // Codec not found
        }
    
        pCodecCtx = avcodec_alloc_context3(codec);
        if (!pCodecCtx) {
            fprintf(stderr, "Could not allocate audio codec context\n");
            return;
        }
    
        pCodecCtx->sample_rate = SAMPLE_RATE;
        pCodecCtx->channels = CHANNEL_NUM;
    
    
        /* open it */
        if (avcodec_open2(pCodecCtx, codec, NULL) < 0) {
            fprintf(stderr, "Could not open codec\n");
            return;
        }
    
        f = fopen(filename, "rb");
        if (!f) {
            fprintf(stderr, "Could not open %s\n", filename);
            return;
        }
        outfile = fopen(outfilename, "wb");
        if (!outfile) {
            av_free(pCodecCtx);
            return;
        }
    
        avpkt.data = inbuf;
        avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
        // supposed to do this but don't have AVFormatContext
        // int frReadStt = av_read_frame(pFormatCtx, &avpkt);   
    
         /* decode until eof */
        while (avpkt.size > 0) {
            int i, ch;
            int got_frame = 0;
    
            if (!decoded_frame) {
                if (!(decoded_frame = av_frame_alloc())) {
                    fprintf(stderr, "Could not allocate audio frame\n");
                    return;
                }
            }
    
            len = avcodec_decode_audio4(pCodecCtx, decoded_frame, &got_frame, &avpkt);
            if (len < 0) {
                fprintf(stderr, "Error while decoding. len = %d \n",len);
                return;
            }
            if (got_frame) {
                /* if a frame has been decoded, output it */
                int data_size = av_get_bytes_per_sample(pCodecCtx->sample_fmt);
                if (data_size < 0) {
                    /* This should not occur, checking just for paranoia */
                    fprintf(stderr, "Failed to calculate data size\n");
                    return;
                }
                for (i=0; i < decoded_frame->nb_samples; i++)
                    for (ch=0; ch < pCodecCtx->channels; ch++)
                        fwrite(decoded_frame->data[ch] + data_size*i, 1, data_size, outfile);
            }
    
            avpkt.size -= len;
            avpkt.data += len;
    
            avpkt.dts =
            avpkt.pts = AV_NOPTS_VALUE;
            // frReadStt = av_read_frame(pFormatCtx, &avpkt);
    
            if (avpkt.size < AUDIO_REFILL_THRESH) {
                /* Refill the input buffer, to avoid trying to decode
                 * incomplete frames. Instead of this, one could also use
                 * a parser, or use a proper container format through
                 * libavformat. */
                memmove(inbuf, avpkt.data, avpkt.size);
                avpkt.data = inbuf;
                len = fread(avpkt.data + avpkt.size, 1,
                            AUDIO_INBUF_SIZE - avpkt.size, f);
                if (len > 0)
                    avpkt.size += len;
            }
        }
    
        fclose(outfile);
        fclose(f);
    
        avcodec_close(pCodecCtx);
        av_free(pCodecCtx);
        av_frame_free(&decoded_frame);
    
        printf("Finish decode audio file %s to %s\n", filename, outfilename);
    }
    

    I learnt from this question that I should use av_read_frame() instead of fread but I only have RTP payload not a whole file. Is it right to directly map rtp payload to AVPacket struct? If not then how should I decode the RTP payload?

  • How to create video editor to edit the video as fast as possible irrespective of size ? [closed]

    24 avril, par Mayur

    we wanted to build a video editor where we wanted to give the option to add watermarks, change the bitrate, resize the video, add frames inside the video, etc. We are using NodeJS for the backend, I have tried to achieve the same thing with the help of ffmpeg package in NodeJS, but it is taking too much time to get the edited video. Is there any better approach that I can follow to get this done as quickly as possible? The video size could be up to 1 GB.

    Server Configuration 4 CPU 8 GB RAM

  • Slow execution of cuCtxDestroy causing Windows desktop freeze [closed]

    24 avril, par sun

    I’m encountering a problem with the cuCtxDestroy function that seems to be causing my system to freeze and result in a black screen. I’m running Windows 11 on my computer, and I would appreciate any assistance in troubleshooting this issue.

    When I call cuCtxDestroy, it takes more than 10 seconds to complete, during which both the GPU and CPU appear to be stuck. Eventually, the screen goes black, and in some cases, the system may recover, while in other cases, it requires a system reboot.

    Here is some information about my system:

    • Integrated Intel graphics card

    • NVIDIA GeForce GTX 1650

    • Driver Version: 546.65

    • CUDA Version: 12.3

    • Windows 11

    cuda profile image