I am new to FFmpeg. I read some essential basics but cannot figure out why the following command does not work. The script is written in Windows in PowerShell.
ffmpeg -hide_banner `
-loop 1 -t 3 -i image.jpg `
-i input.mp4 `
-filter_complex "`
[1:v] split=2 [v1][v2];
[1:a] asplit=2 [a1][a2]; `
[v1] trim=start=10:duration=10, setpts=PTS-STARTPTS [ov1];`
[a1] atrim=start=10:duration=10, asetpts=PTS-STARTPTS [oa1];`
[v2] trim=start=40:duration=10, setpts=PTS-STARTPTS [ov2];`
[a2] atrim=start=40:duration=10, asetpts=PTS-STARTPTS [oa2];`
[0][ov1][ov2] concat=n=3:v=1:a=0 [v]; `
[oa1][oa2] concat=n=2:v=0:a=1 [ca]; `
[ca] adelay=3s|3s|3s|3s|3s|3s, asetpts=PTS-STARTPTS [a]; `
"`
-map "[v]" -map "[a]" `
-c:v libx264 -preset slow -crf 20 `
-c:a aac -b:a 128k `
output.mp4
It only produces an image and FFMpeg reports the following warning:
Non-monotonic DTS; previous: 1024, current: 0; changing to 1025. This may result in incorrect timestamps in the output file.
I figured out how to apply filters: split
and concat
. I knew I would need to label inputs and outputs in each part of the filter. I understood -map
was required to indicate streams included in the output file.
I'm not sure if -loop 1 -t 3 -i ...
is correct and the part with PTS
has been copied from somewhere--I have a little understanding of what PTS is. Finally, the adelay
would make audio stream synced with video streams after the image.
What I am trying to achieve here is:
- convert an image to a video
- clone the second input into two separate streams
- trim both such streams, and
- concatenate everything into one video/audio.
The image should take 3 secs, the remaining streams 10 secs each, so the whole video should be 23 secs long. It does not do that.
EDIT. More info about input/output files and FFMpeg itself. Please note all the above outputs were created by FFMpeg version by gyan.dev. I am current working on FFMpeg by BtbN
The video is downloaded from Download Sample Videos and the sample video itself is .mp4
10MB (direct link). Image is just a random JPEG cropped to size of 1280x720.
This is what ffmpeg -version
prints:
ffmpeg version 2024-01-11-git-5e751dabc5-full_build-www.gyan.dev Copyright (c) 2000-2024 the FFmpeg developers
built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --pkg-config=pkgconf --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-dxva2 --enable-d3d11va --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
libavutil 58. 36.101 / 58. 36.101
libavcodec 60. 37.100 / 60. 37.100
libavformat 60. 20.100 / 60. 20.100
libavdevice 60. 4.100 / 60. 4.100
libavfilter 9. 17.100 / 9. 17.100
libswscale 7. 6.100 / 7. 6.100
libswresample 4. 13.100 / 4. 13.100
libpostproc 57. 4.100 / 57. 4.100
The output of ffprobe -hide_banner input.mp4
:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '.\input.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 1970-01-01T00:00:00.000000Z
encoder : Lavf53.24.2
Duration: 00:01:02.31, start: 0.000000, bitrate: 1347 kb/s
Stream #0:0[0x1](und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 959 kb/s, 25 fps, 25 tbr, 12800 tbn (default)
Metadata:
creation_time : 1970-01-01T00:00:00.000000Z
handler_name : VideoHandler
vendor_id : [0][0][0][0]
Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 383 kb/s (default)
Metadata:
creation_time : 1970-01-01T00:00:00.000000Z
handler_name : SoundHandler
vendor_id : [0][0][0][0]
The output of ffprobe -hide_banner output.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '.\output.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf60.20.100
Duration: 00:00:05.28, start: 0.000000, bitrate: 921 kb/s
Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 842 kb/s, 25 fps, 25 tbr, 12800 tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
encoder : Lavc60.37.100 libx264
Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
Output of mediainfo on input.jpg
General
Complete name : C:\Users\zbkoz\Desktop\demo-video\image.jpg
Format : JPEG
File size : 519 KiB
Image
Format : JPEG
Width : 1 280 pixels
Height : 720 pixels
Color space : YUV
Chroma subsampling : 4:4:4
Bit depth : 8 bits
Compression mode : Lossy
Stream size : 519 KiB (100%)
ColorSpace_ICC : RGB
;
after[a]
. Beside that, the issue is not reproducible. I tried to reproduce the issue by generating synthetic patterns:ffmpeg -y -f lavfi -i testsrc=size=192x108:rate=1:duration=1 -update 1 -pix_fmt yuvj420p image.jpg
andffmpeg -y -f lavfi -i testsrc=size=192x108:rate=25 -f lavfi -i sine=frequency=400 -f lavfi -i sine=frequency=600 -filter_complex "[1:a][2:a]amerge=inputs=2[a]" -map 0:v -map "[a]" -vcodec libx264 -acodec aac -pix_fmt yuv420p -t 30 input.mp4
(it's always better to have the same input files as you have, for testing).ffmpeg -y -hide_banner -loop 1 -t 3 -i image.jpg -i input.mp4 -filter_complex "[1:v]split=2[v1][v2];[1:a]asplit=2[a1][a2];[v1]trim=start=10:duration=10,setpts=PTS-STARTPTS[ov1];[a1]atrim=start=10:duration=10,asetpts=PTS-STARTPTS[oa1];[v2]trim=start=40:duration=10,setpts=PTS-STARTPTS[ov2];[a2]atrim=start=40:duration=10,asetpts=PTS-STARTPTS[oa2];[0][ov1][ov2]concat=n=3:v=1:a=0[v];[oa1][oa2]concat=n=2:v=0:a=1[ca];[ca]adelay=3s|3s,asetpts=PTS-STARTPTS[a]" -map "[v]" -map "[a]" -c:v libx264 -preset slow -crf 20 -c:a aac -b:a 128k output.mp4
. It looks OK.input.mp4
?input.mp4
is1347 kb/s
. I added all outputs from FFMpeg and Mediainfo. I hope you can use the same file apart from the image--I can't remember where I get it from. It's just a regular jpeg cropped to 1280x720 and exported to JPEG again in GIMP.