Newest 'ffmpeg' Questions - Stack Overflow

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

Articles published on the website

  • ffmpeg fails when using a temporary file path for output

    1 February, by GROVER.

    I'm using the fluent-ffmpeg library in Node to automatically generate a single thumbnail at the halfway mark of a given video file.

    const screenshot = async (pathToFile: string) => {
        // Generate a temporary file path outside of the working directory with the extension .jpg
        const tempFileName = tmp.tmpNameSync({ postfix: ".jpg" });
    
        try{
            await new Promise((resolve, reject) => {
                ffmpeg(pathToFile)
                    .thumbnail({
                        // This works fine when NOT using tmpNameSync
                        filename: tempFileName, 
                        count: 1, 
                        timestamps: ["50%"]
                    })
                    .on("end", resolve)
                    .on("error", reject);
            });
        } catch(err){
            console.log(err);
            return null;
        }
        
        return tempFileName;
    };
    

    This implementation works very well when I'm using a "non-temporary" output path, such as /path/to/thumbnail.jpg. But, when I use a library such as tmp to generate a temporary file name outside of the working directory, ffmpeg throws an error.

    Error: ffmpeg exited with code 1: av_interleaved_write_frame(): Input/output error
    frame=    1 fps=0.0 q=7.8 size=N/A time=00:00:00.04 bitrate=N/A speed=0.152x    
    frame=    1 fps=0.0 q=7.8 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.141x    
    video:119kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
    Conversion failed!
    

    I cannot seem to find anything about ffmpeg struggling with accessing temporary directories online, and using the command directly in the terminal works as expected, so I don't believe this to be a permissions issue. Although, I may be going about this incorrectly.

    This is the full ffmpeg command that fluent-ffmpeg generates (reduced filenames so it doesn't look horrible):

    ffmpeg -ss 14.118271 -i /var/folders/__/XYZ/T/tmp-XYZ/tmp-XYZ -y -filter_complex scale=w=trunc(oh*a/2)*2:h=720[size0];[size0]split=1[screen0] -vframes 1 -map [screen0] var/folders/__/XYZ/T/tmp-XYZ.jpg
    
  • ffmpeg stdin "output file is empty, nothing was encoded"

    1 February, by brock

    Just trying to stdin and stdout a simple CAF to MP3 conversion. Output looks exactly the same except using stdin does not encode anything. Windows 10. I'm going bananas here. Please advise.

    Using - (stdin)...

    >type test.caf | ffmpeg -i - -f mp3 - > test.mp3
    ffmpeg version 4.2.1 Copyright (c) 2000-2019 the FFmpeg developers
      built with gcc 9.1.1 (GCC) 20190807
      configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
      libavutil      56. 31.100 / 56. 31.100
      libavcodec     58. 54.100 / 58. 54.100
      libavformat    58. 29.100 / 58. 29.100
      libavdevice    58.  8.100 / 58.  8.100
      libavfilter     7. 57.100 /  7. 57.100
      libswscale      5.  5.100 /  5.  5.100
      libswresample   3.  5.100 /  3.  5.100
      libpostproc    55.  5.100 / 55.  5.100
    Guessed Channel Layout for Input Stream #0.0 : stereo
    Input #0, caf, from 'pipe:':
      Metadata:
        approximate duration in seconds: 3.1
        source bit depth: I16
      Duration: N/A, start: 0.000000, bitrate: N/A
        Stream #0:0: Audio: adpcm_ima_qt (ima4 / 0x34616D69), 48000 Hz, stereo, s16p, 384 kb/s
    Stream mapping:
      Stream #0:0 -> #0:0 (adpcm_ima_qt (native) -> mp3 (libmp3lame))
    Output #0, mp3, to 'pipe:':
      Metadata:
        approximate duration in seconds: 3.1
        source bit depth: I16
        TSSE            : Lavf58.29.100
        Stream #0:0: Audio: mp3 (libmp3lame), 48000 Hz, stereo, s16p
        Metadata:
          encoder         : Lavc58.54.100 libmp3lame
    size=       0kB time=00:00:00.00 bitrate=N/A speed=   0x
    video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
    Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)
    

    Using -i...

    >ffmpeg -i test.caf -f mp3 - > test.mp3
    ffmpeg version 4.2.1 Copyright (c) 2000-2019 the FFmpeg developers
      built with gcc 9.1.1 (GCC) 20190807
      configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
      libavutil      56. 31.100 / 56. 31.100
      libavcodec     58. 54.100 / 58. 54.100
      libavformat    58. 29.100 / 58. 29.100
      libavdevice    58.  8.100 / 58.  8.100
      libavfilter     7. 57.100 /  7. 57.100
      libswscale      5.  5.100 /  5.  5.100
      libswresample   3.  5.100 /  3.  5.100
      libpostproc    55.  5.100 / 55.  5.100
    Guessed Channel Layout for Input Stream #0.0 : stereo
    Input #0, caf, from 'test.caf':
      Metadata:
        approximate duration in seconds: 3.1
        source bit depth: I16
      Duration: N/A, start: 0.000000, bitrate: N/A
        Stream #0:0: Audio: adpcm_ima_qt (ima4 / 0x34616D69), 48000 Hz, stereo, s16p, 384 kb/s
    Stream mapping:
      Stream #0:0 -> #0:0 (adpcm_ima_qt (native) -> mp3 (libmp3lame))
    Press [q] to stop, [?] for help
    Output #0, mp3, to 'pipe:':
      Metadata:
        approximate duration in seconds: 3.1
        source bit depth: I16
        TSSE            : Lavf58.29.100
        Stream #0:0: Audio: mp3 (libmp3lame), 48000 Hz, stereo, s16p
        Metadata:
          encoder         : Lavc58.54.100 libmp3lame
    size=      49kB time=00:00:03.12 bitrate= 129.3kbits/s speed=34.4x
    video:0kB audio:49kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.246501%
    
  • Why this ffmpeg mp4/mkv/wmv/flv/avi to webm command line with cuda is as slow as with CPU?

    1 February, by uchi

    im playing with my video files, I want to convert them from mp4, mkv, wmv, avi, flv, mov... to webm.

    So I have written this:

    for file in *
        do   
    if [ -f "$file" ]
        then     filename="${file%.*}"
        ../ffmpeg -hwaccel nvdec -hwaccel_device 0 -hwaccel_output_format cuda -i "$file" -c:v vp9 -b:v 0 -crf 31 -c:a libvorbis "$filename".webm
    fi
    done
    

    As shown, I have compiler my own ffmpeg binary, with those options:

    ./configure --enable-gpl --enable-libx264 --enable-nonfree --enable-cuda-nvcc --enable-libvpx --enable-vaapi --enable-libvorbis  --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
    

    It works, but, with CPU I got speed 0,1x and with cuda... almost the same.

    My CPU? 11th Gen Intel(R) Core(TM) i7-11800H

    My GPU? GeForce RTX™ 3060 mobile

    Filesize? 300-3000mb (same speed with any)

    Is there anything I could do? (I also tried vp9_vaapi, getting

    Impossible to convert between the formats supported by the filter 'Parsed_null_0' and the filter 'auto_scale_0'
    Error reinitializing filters!
    Failed to inject frame into filter network: Function not implemented
    

    Any clue? Or do I have to give up?

    Thanks

  • ffmpeg created video receiving error message on Youtube

    1 February, by David Ruan

    I am using this command to generate a video with a cover page and an audio.

    ffmpeg -loop 1 -r 1 -i c:/temp/coverImage.jpeg -i C:/temp/out/input.mp3 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -c:a copy -shortest -c:v libx264 C:/temp/VideoOut/output.mp4

    But the generated video got an error message from Youtube after it is uploaded.

    "Your video has an unsupported aspect ratio"

    Any idea how to solve this issue?

  • Trying to decode and encode audio files with the FFMPEG C API

    1 February, by Giulio Iacomino

    My ultimate goal will be to split multi channel WAV files into single mono ones, after few days of experiments my plan is the sequence:

    1. Decode audio file into a frame.
    2. Convert interleaved frame into a planar one. (in order to separate the data buffer into multiple ones)
    3. Grab the planar frame buffers and encode each of them into a new file.

    So far I'm stuck trying to convert a wav file from interleaved to a planar one, and reprint the wav file.

    edit: I've turned on guard malloc and apparently the error is within the convert function

    Here's the code:

    AVCodecContext* initializeAndOpenCodecContext(AVFormatContext* formatContext, AVStream* stream){
         // grab our stream, most audio files only have one anyway
        const AVCodec* decoder = avcodec_find_decoder(stream->codecpar->codec_id);
        if (!decoder){
            std::cout << "no decoder, can't go ahead!\n";
            return nullptr;
        }
        AVCodecContext* codecContext = avcodec_alloc_context3(decoder);
        avcodec_parameters_to_context(codecContext, stream->codecpar);
        int err = avcodec_open2(codecContext, decoder, nullptr);
        if (err < 0){
            std::cout << "couldn't open codex!\n";
        }
        return codecContext;
    }
    
    void initialiseResampler(SwrContext* resampler, AVFrame* inputFrame, AVFrame* outputFrame){
        av_opt_set_chlayout(resampler, "in_channel_layout", &inputFrame->ch_layout, 0);
        av_opt_set_chlayout(resampler, "out_channel_layout", &outputFrame->ch_layout, 0);
        av_opt_set_int(resampler, "in_sample_fmt", inputFrame->format, 0);
        av_opt_set_int(resampler, "out_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
        av_opt_set_int(resampler, "in_sample_rate", inputFrame->sample_rate, 0);
        av_opt_set_int(resampler, "out_sample_rate", outputFrame->sample_rate, 0);
    }
    
    AVFrame* initialisePlanarFrame(AVFrame* frameToInit, AVFrame* inputFrame){
        //AVFrame *planar_frame = av_frame_alloc();
        frameToInit->nb_samples = inputFrame->nb_samples;
        frameToInit->ch_layout = inputFrame->ch_layout;
        frameToInit->format = AV_SAMPLE_FMT_FLTP;
        frameToInit->sample_rate = inputFrame->sample_rate;
        return nullptr;
    }
    
    int main() {
        AVCodecContext *codingContext= NULL;
        const AVCodec *codec;
        codec = avcodec_find_encoder(AV_CODEC_ID_PCM_F32LE);
        codingContext = avcodec_alloc_context3(codec);
        codingContext->bit_rate = 16000;
        codingContext->sample_fmt = AV_SAMPLE_FMT_FLT;
        codingContext->sample_rate = 48000;
        codingContext->ch_layout.nb_channels = 2;
        codingContext->ch_layout.order = (AVChannelOrder)0;
        uint8_t **buffer_ = NULL;
        AVFrame* planar_frame = NULL;
        
        // open input
        AVFormatContext* formatContext = nullptr;
        int err = avformat_open_input(&formatContext, "/Users/tonytorm/Desktop/drum kits/DECAP - Drums That Knock Vol. 9/Kicks/Brash Full Metal Kick.wav", nullptr, nullptr);
        if (err < 0){
            fprintf(stderr, "Unable to open file!\n");
            return;
        }
    
        // find audio stream
        err = avformat_find_stream_info(formatContext, nullptr);
        if (err > 0){
            fprintf(stderr, "Unable to retrieve stream info!\n");
            return;
        }
        
        int index = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
        if (index < 0){
            std::cout<<  "coudn't find audio stream in this file" << '\n';
        }
        AVStream* stream = formatContext->streams[index];
        
        auto fileName = "/Users/tonytorm/Desktop/newFile.wav";
        FILE* newFile = fopen(fileName, "w+");
        
        // find right codec and open it
        if (auto openCodecContext = initializeAndOpenCodecContext(formatContext, stream)){
            AVPacket* packet = av_packet_alloc();
            AVFrame* frame = av_frame_alloc();
            AVFrame* planar_frame = av_frame_alloc();
            SwrContext *avr = swr_alloc();  //audio resampling context
            AVChannelLayout monoChannelLayout{(AVChannelOrder)0};
            monoChannelLayout.nb_channels = 2;
            
    
            while (!av_read_frame(formatContext, packet)){
                if (packet->stream_index != stream->index) continue;  // we only care about audio
                int ret = avcodec_send_packet(openCodecContext, packet);
                if ( ret < 0) {
                    if (ret != AVERROR(EAGAIN)){   // if error is actual error not EAGAIN
                        std::cout << "can't do shit\n";
                        return;
                    }
                }
                while (int bret = avcodec_receive_frame(openCodecContext, frame) == 0){
                    initialisePlanarFrame(planar_frame, frame);
                    
       
                    
                    int buffer_size_in = av_samples_get_buffer_size(nullptr,
                                                                    frame->ch_layout.nb_channels,
                                                                    frame->nb_samples,
                                                                    (AVSampleFormat)frame->format,
                                                                    0);
                    int buffer_size_out = buffer_size_in/frame->ch_layout.nb_channels;
    
                    //planar_frame->linesize[0] = buffer_size_out;
                    
                    int ret = av_samples_alloc(planar_frame->data,
                                               NULL,
                                               planar_frame->ch_layout.nb_channels,
                                               planar_frame->nb_samples,
                                               AV_SAMPLE_FMT_FLTP,
                                               0);
                    
                    initialiseResampler(avr, frame, planar_frame);
                    if (int errRet = swr_init(avr) < 0) {
                        fprintf(stderr, "Failed to initialize the resampling context\n");
                    }
    
                    if (ret < 0){
                        char error_message[AV_ERROR_MAX_STRING_SIZE];
                        av_strerror(ret, error_message, AV_ERROR_MAX_STRING_SIZE);
                        fprintf(stderr, "Error allocating sample buffer: %s\n", error_message);
                        return -1;
                    }
                    
                    int samples_converted = swr_convert(avr,
                                                        planar_frame->data,
                                                        buffer_size_out,
                                                        (const uint8_t **)frame->data,
                                                        buffer_size_in);
                    if (samples_converted < 0) {
                        // handle error
                        std::cout << "error in conversion\n";
                        return;
                    }
                    if (avcodec_open2(codingContext, codec, NULL) < 0) {
                        std::cout << "can't encode!\n";
                        return;
                    }
                    AVPacket* nu_packet = av_packet_alloc();
                    while (int copy = avcodec_send_frame(codingContext, planar_frame) != 0){
                        if (copy == AVERROR(EAGAIN) || copy == AVERROR_EOF){
                            std::cout << "can't encode file\n";
                            return;
                        }
                        if (avcodec_receive_packet(codingContext, nu_packet) >=0){
                            fwrite(nu_packet->data, 4, nu_packet->size, newFile);
                            //av_write_frame(avc, nu_packet);
                        }
                    }
                    av_freep(planar_frame->data);
                    av_frame_unref(frame);
                    av_frame_unref(planar_frame);
                }
    //            av_packet_free(&packet);
    //            av_packet_free(&nu_packet);
            }
            swr_free(&avr);
            avcodec_free_context(&codingContext);
            
        }
        fclose(newFile);
    }
    

    I know i should write a header to the new wave file but for now I'm just trying to write the raw audio data. I'm getting always the same error but in different parts of the code (randomly), sometimes the code even compiles (writing the raw audio data, but filling it with some rubbish as well, i end up with a data file that is thrice the original one, sometimes i end up with a slightly smaller file - i guess the raw audio without the headers), results are basically random.

    Here are some of the functions that trigger the error:

    int ret = av_samples_alloc(); //(this the most common one)
    swr_convert()
    av_freep();
    

    the error is:

    main(64155,0x101b5d5c0) malloc: Incorrect checksum for freed object 0x106802600: probably modified after being freed.
    Corrupt value: 0x0
    main(64155,0x101b5d5c0) malloc: *** set a breakpoint in malloc_error_break to debug */