Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • concat ffmpeg filter outputs wrong timebase, xfade cannot be used

    27 mai, par Navarro

    I'm trying to join several videos (removed for testing purposes) and images using xfade. In some cases, I don't want to use any transition, so instead of using xfade filter I'm using concat filter. This is the command:

    ffmpeg \
    -loop 1 -t 3 -framerate 30 -i f44096fb9e5ecff463aeedc56c5f4795 \
    -loop 1 -t 3 -framerate 30 -i 3a5ec4f224614a17ff4c7c77fe853233 \
    -loop 1 -t 3 -framerate 30 -i 3a5ec4f224614a17ff4c7c77fe853233 \
    -filter_complex " \
    [0:v]settb=AVTB,setpts=PTS-STARTPTS,fps=30,scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:trunc((ow-iw)/2):trunc((oh-ih)/2):black,setsar=1[scale0]; \
    [1:v]settb=AVTB,setpts=PTS-STARTPTS,fps=30,scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:trunc((ow-iw)/2):trunc((oh-ih)/2):black,setsar=1[scale1]; \
    [2:v]settb=AVTB,setpts=PTS-STARTPTS,fps=30,scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:trunc((ow-iw)/2):trunc((oh-ih)/2):black,setsar=1[scale2]; \
    [scale0][scale1]concat=n=2:v=1:a=0[vconcat1]; \
    [vconcat1][scale2]xfade=transition=wipeleft:duration=1:offset=5,setpts=PTS-STARTPTS[xfade2]" \
    -map "[xfade2]" -f mp4 -q:v 0 -r 30 -vcodec libx264 -pix_fmt yuv420p -shortest output.mp4
    

    However I get the following error:

    [Parsed_xfade_19 @ 0x6703a80] First input link main timebase (1/1000000) do not match the corresponding second input link xfade timebase (1/30)
    [Parsed_xfade_19 @ 0x6703a80] Failed to configure output pad on Parsed_xfade_19
    Error reinitializing filters!
    Failed to inject frame into filter network: Invalid argument
    Error while processing the decoded data for stream #2:0
    

    Why am I getting this? Input videos are using 30 fps and default timebase as per previous filters. Why concat filter is outputting a timebase of 1/1000000 and where is it specified? How can I change that?

    I also get this warning, but I think it's unrelated:

    [swscaler @ 0x6963e80] deprecated pixel format used, make sure you did set range correctly
    

    By the way, can I use anything else rather than concat filter so that videos which don't use xfade are not re-encoded? I cannot use null filter because it takes only 1 input, not 2.

    Thanks!

  • FFmpeg : About filter_complex command

    26 mai, par HJ Kim

    I use this command.

    ffmpeg -i Input.mp4 -i logo.png -c:v h264_nvenc -filter_complex "[0:v]scale=-1:720[video];[1:v][video]scale2ref=(iw/ih)*ih/8/sar:ih/8[wm][base];[base][wm]overlay=10:10" output.mp4

    But what does this mean?

    scale2ref=(iw/ih)*ih/8/sar:ih/8

  • FFmpeg zoompan filter always arcs when panning — how to get a straight‐line pan to a focus rectangle center ?

    26 mai, par Mykyta Manuilenko

    I’m trying to generate a 10s video from a single PNG image with FFmpeg’s zoompan filter, where the crop window zooms in from the image center and simultaneously pans in a perfectly straight line to the center of a predefined focus rectangle.

    My input parameters:

    "zoompan": {
      "timings": {
        "entry": 0.5, // show full frame
        "zoom": 1, // zoom-in/zoom-out timing
        "outro": 0.5 // show full frame in the end
      },
      "focusRect": {
        "x": 1086.36,
        "y": 641.87,
        "width": 612.44,
        "height": 344.86
      }
    }
    

    My input/output values:

    • fps: 25
    • image input dimensions: 1920 × 1080
    • output video dimensions: 1920 × 1080

    My calculations:

        // Width of the bounding box to zoom into
        const bboxWidth = focusRect.width;
    
        // Height of the bounding box to zoom into
        const bboxHeight = focusRect.height;
    
        // X coordinate (center of the bounding box)
        const bboxX = focusRect.x + focusRect.width / 2;
    
        // Y coordinate (center of the bounding box)
        const bboxY = focusRect.y + focusRect.height / 2;
    
        // Time (in seconds) to wait before starting the zoom-in
        const preWaitSec = timings.entry;
    
        // Duration (in seconds) of the zoom-in/out animation
        const zoomSec = timings.zoom;
    
        // Time (in seconds) to wait on the last frame after zoom-out
        const postWaitSec = timings.outro;
    
        // Frame counts
        const preWaitF = Math.round(preWaitSec * fps);
        const zoomInF = Math.round(zoomSec * fps);
        const zoomOutF = Math.round(zoomSec * fps);
        const postWaitF = Math.round(postWaitSec * fps);
    
        // Calculate total frames and holdF
        const totalF = Math.round(duration * fps);
    
        // Zoom target so that bbox fills the output
        const zoomTarget = Math.max(
          inputWidth / bboxWidth,
          inputHeight / bboxHeight,
        );
    
        // Calculate when zoom-out should start (totalF - zoomOutF - postWaitF)
        const zoomOutStartF = totalF - zoomOutF - postWaitF;
    
        // Zoom expression (simple linear in/out)
        const zoomExpr = [
          // Pre-wait (hold at 1)
          `if(lte(on,${preWaitF}),1,`,
          // Zoom in (linear)
          `if(lte(on,${preWaitF + zoomInF}),1+(${zoomTarget}-1)*((on-${preWaitF})/${zoomInF}),`,
          // Hold zoomed
          `if(lte(on,${zoomOutStartF}),${zoomTarget},`,
          // Zoom out (linear)
          `if(lte(on,${zoomOutStartF + zoomOutF}),${zoomTarget}-((${zoomTarget}-1)*((on-${zoomOutStartF})/${zoomOutF})),`,
          // End
          `1))))`,
        ].join('');
    
        // Center bbox for any zoom
        const xExpr = `${bboxX} - (${outputWidth}/zoom)/2`;
        const yExpr = `${bboxY} - (${outputHeight}/zoom)/2`;
    
        // Build the filter string
        const zoomPanFilter = [
          `zoompan=`,
          `s=${outputWidth}x${outputHeight}`,
          `:fps=${fps}`,
          `:d=${totalF}`,
          `:z='${zoomExpr}'`,
          `:x='${xExpr}'`,
          `:y='${yExpr}'`,
          `,gblur=sigma=0.5`,
          `,minterpolate=mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=${fps}`,
        ].join('');
    

    So, my FFmpeg command looks like:

    ffmpeg -t 10 -framerate 25 -loop 1 -i input.png -y -filter_complex "[0:v]zoompan=s=1920x1080:fps=25:d=250:z='if(lte(on,13),1,if(lte(on,38),1+(3.1350009796878058-1)*((on-13)/25),if(lte(on,212),3.1350009796878058,if(lte(on,237),3.1350009796878058-((3.1350009796878058-1)*((on-212)/25)),1))))':x='1392.58 - (1920/zoom)/2':y='814.3 - (1080/zoom)/2',gblur=sigma=0.5,minterpolate=mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=25,format=yuv420p,pad=ceil(iw/2)*2:ceil(ih/2)*2" -vcodec libx264 -f mp4 -t 10 -an -crf 23 -preset medium -copyts output.mp4
    

    Actual behavior:

    The pan starts at the image center, but follows a curved (arc-like) trajectory before it settles on the focus‐rect center (first it goes to the right bottom corner and then to the focus‐rect center).

    Expected behavior:

    The pan should move the crop window’s center in a perfectly straight line from (iw/2, ih/2) to (1392.58, 814.3) over the 25-frame zoom‐in (similar to pinch-zooming on a smartphone).

    Questions:

    • How can I express a truly linear interpolation of the crop window center inside zoompan so that the pan path is a straight line in source coordinates?

    • Is there a better way (perhaps using different FFmpeg filters or scripting) to achieve this effect?

  • How to quote a file name with a single quote in ffmpeg movie= filter notation ? [closed]

    26 mai, par PieterV

    I am trying to run ffmpeg using a file that contains a single quote ' in the filename.

    I tried to follow the docs that say I should replace a ' with '\''.
    And a ticket that says I should replace a ' with \\\\\'.

    I've tried both, and can't get get it working.

    E.g. docs format:

    ./ffprobe -loglevel error -read_intervals %00:30 -select_streams s:0 -f lavfi -i "movie='D\:\\Test\\Interlaced - Dragons'\'' Den - S14E02 - Episode 2.mkv'[out0+subcc]" -show_packets -print_format json
    
    {
    [Parsed_movie_0 @ 00000222a2f82200] Failed to avformat_open_input 'D:\Test\Interlaced - Dragons Den - S14E02 - Episode 2.mkv'
    [AVFilterGraph @ 00000222a2f76ec0] Error processing filtergraph: No such file or directory
    movie='D\:\\Test\\Interlaced - Dragons'\'' Den - S14E02 - Episode 2.mkv'[out0+subcc]: No such file or directory
    

    E.g. ticket format:

    ./ffprobe -loglevel error -read_intervals %00:30 -select_streams s:0 -f lavfi -i "movie='D\:\\Test\\Interlaced - Dragons\\\\\' Den - S14E02 - Episode 2.mkv'[out0+subcc]" -show_packets -print_format json
    
    {
    [Parsed_movie_0 @ 00000158613d2080] Failed to avformat_open_input 'D:\Test\Interlaced - Dragons\\ Den - S14E02 - Episode 2.mkv[out0+subcc]'
    [AVFilterGraph @ 00000158613c6ec0] Error processing filtergraph: No such file or directory
    movie='D\:\\Test\\Interlaced - Dragons\\\\\' Den - S14E02 - Episode 2.mkv'[out0+subcc]: No such file or directory
    
    > dir "D:\Test\Interlaced - Dragons' Den - S14E02 - Episode 2.mkv"
    
        Directory: D:\Test
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a---           4/20/2025 11:38 AM       18059051 Interlaced - Dragons' Den - S14E02 - Episode 2.mkv
    

    This is on Win11 using FFmpeg7.
    Any ideas?

    [Update]
    I found a doc on escape filtergraph strings, did not help, I tried 0 to 7 \.

    I also found and tried the ffescape utility, the output it produces just uses a single \' and does not work.

    > echo "D:\Test\Interlaced - Dragons' Den - S14E02 - Episode 2.mkv" | ./ffescape.exe
    => D:\\Test\\Interlaced - Dragons\' Den - S14E02 - Episode 2.mkv\
    
    > ./ffprobe -loglevel error -read_intervals %00:30 -select_streams s:0 -f lavfi -i "movie='D:\\Test\\Interlaced - Dragons\' Den - S14E02 - Episode 2.mkv\'[out0+subcc]" -show_packets -print_format json
    {
    [Parsed_movie_0 @ 0000021348f12200] Failed to avformat_open_input 'D'
    [AVFilterGraph @ 0000021348f06ec0] Error processing filtergraph: No such file or directory
    movie='D:\\Test\\Interlaced - Dragons\' Den - S14E02 - Episode 2.mkv\'[out0+subcc]: No such file or directory
    

    [Update]
    I found docs for ffmpeg filter script where I can place commands in a file.

    I tried ./ffprobe -loglevel error -read_intervals %00:01 -select_streams s:0 -f lavfi -/i "d:\filtergraph.txt" -show_packets -print_format json, and it load the script.

    Works: movie=test.mkv[out0+subcc]\ if test.mkv is in ffprobe dir. Works: movie=test\'.mkv[out0+subcc]\ if test'.mkv is in ffprobe dir.
    Not: movie=D:\test.mkv[out0+subcc]
    Not: movie=D\:\\test.mkv[out0+subcc]
    Not: movie=test space.mkv[out0+subcc]
    Not: movie='test space.mkv[out0+subcc]'
    Not: movie="test space.mkv[out0+subcc]"
    Not: 'movie=test space.mkv[out0+subcc]'
    Not: "movie=test space.mkv[out0+subcc]"\

    :(

    Update with working answer

  • avcodec_find_encoder(AV_CODEC_ID_H264) returns null

    25 mai, par Monjura Rumi

    I am building an android application which will encode image captured from camera preview and later decode it. I am using ffmpeg library to encode and decode. To build static library with x264 I have used this tutorial. http://dl.dropbox.com/u/22605641/ffmpeg_android/main.html. As a source code of ffmpeg if I use the one downloaded from the link given in tutorial I can built it but can't build library if i use source code downloaded from here git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg. I have built library in ubuntu and using it in windows 7 in Eclipse. As I need only h264 encoder and decoder I have used following code for ffmpeg, slightly modified from tutorial.

    #!/bin/bash
    
    NDK=~/Documents/android-ndk-r8e
    PLATFORM=$NDK/platforms/android-8/arch-arm
    PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86
    PREFIX=/home/android-ffmpeg
    
    function build_one
    {
        ./configure --target-os=linux --prefix=$PREFIX \
        --enable-cross-compile \
        --enable-runtime-cpudetect \
        --disable-asm \
        --arch=arm \
        --cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
        --cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
        --disable-stripping \
        --nm=$PREBUILT/bin/arm-linux-androideabi-nm \
        --sysroot=$PLATFORM \
        --enable-nonfree \
        --enable-version3 \
        --disable-everything \
        --enable-gpl \
        --disable-doc \
        --enable-avresample \
        --disable-ffplay \
        --disable-ffserver \
        --enable-ffmpeg \
        --disable-ffprobe \
        --enable-avcodec \
        --enable-libx264 \
        --enable-encoder=libx264 \
        --enable-encoder=libx264rgb \
        --enable-decoder=h263 \
        --enable-decoder=h264 \
        --enable-decoder=svq3 \   
        --enable-zlib \
        --enable-gpl \
        --enable-pic \
        --disable-devices \
        --disable-avdevice \
        --extra-cflags="-I/home/android-ffmpeg/include -fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=armv7-a" \
        --extra-ldflags="-L/home/android-ffmpeg/lib"
    make -j4 install
    $PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o
    $PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -L$PREFIX/lib  -soname libffmpeg.so -shared -nostdlib  -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavfilter/libavfilter.a libavresample/libavresample.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog -lx264 --warn-once --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a
    }
    
    build_one
    

    After building library I have been able to build android ndk. A little part of my JNI code is here.

    JNIEXPORT jint JNICALL Java_com_example_testjava_TestjniActivity_initencoder(JNIEnv* env,jobject obj){
    
        av_register_all();
        avcodec_register_all();
        codec = avcodec_find_encoder(AV_CODEC_ID_H264);
        if (!codec) {
          __android_log_write(ANDROID_LOG_INFO, "debug", "not found");                
           return -1;
        }
    .
    .
    .
    }
    

    When I run my java source code that calls initencoder() I get -1 as return value and logcat prints "not found". That means avcodec_find_encoder() returns null and if condition is being ok. I don't know what's wrong. Why this function is returning null? I have searched a lot but did not find any solution that could guide me to right direction. some says to use avcodec_init(). But ndk-build command fails and shows error saying undefined reference to 'avcodec_init()'. I have started with library build because I thought may be I am doing wrong from the first stage. Did I make any mistake in library building like not enabling things that I should? Please help me here. This is kind of urgent for me.