10 FFmpeg Tricks on Linux: Best Fixes for Common Errors
These FFmpeg tricks on Linux cover the jobs people repeat every week: converting, extracting audio, resizing, joining, cutting and batch processing. Every command ran on Debian 13 with FFmpeg 7.1.5 against generated test media, and several popular one-liners failed. Those failures are shown too, because they explain which option to use.
The test file is a 10 second, 1280x720, 30 fps AVI with MP3 audio. -y overwrites output without asking and -loglevel error keeps the log short; add them only when you mean it.
Contents
- 1. Convert, or just change the container
- 2. Extract audio with the right container
- 3. FFmpeg tricks on Linux for resize and crop
- 4. Add a watermark
- 5. FFmpeg tricks on Linux to join files safely
- 6. Change speed, video and audio together
- 7. Frame rate and exact cuts
- 8. Screen capture needs a display
- 9. Streaming output
- 10. Batch FFmpeg tricks on Linux that survive failures
- Which of these FFmpeg tricks on Linux to remember
1. Convert, or just change the container
$ ffmpeg -hide_banner -loglevel error -y -i input.avi -c:v libx264 -crf 23 -preset medium -c:a aac -movflags +faststart output.mp4 && ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate -show_entries format=duration,size -of compact output.mp4
stream|codec_name=h264|width=1280|height=720|r_frame_rate=30/1
stream|codec_name=aac|r_frame_rate=0/0
format|duration=10.033333|size=3946104
[exit 0]
-movflags +faststart moves the index to the front so a web player can start sooner. If the codecs are already right and only the container is wrong, copy the streams. It took a fraction of a second because nothing was re-encoded:
$ TIMEFORMAT='%R s'; time ffmpeg -hide_banner -loglevel error -y -i output.mp4 -c copy remux.mkv; ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate -show_entries format=duration,size -of compact remux.mkv
0.066 s
stream|codec_name=h264|width=1280|height=720|r_frame_rate=4500/151
stream|codec_name=aac|r_frame_rate=0/0
format|duration=10.054000|size=3940540
[exit 0]
-crf controls quality with libx264; a higher number means a smaller file and more loss:
$ for c in 18 23 28; do ffmpeg -hide_banner -loglevel error -y -i input.avi -c:v libx264 -crf $c -an crf$c.mp4; stat -c '%n %s bytes' crf$c.mp4; done
crf18.mp4 6447019 bytes
crf23.mp4 3851160 bytes
crf28.mp4 1797109 bytes
[exit 0]
2. Extract audio with the right container
A common tip copies the audio stream straight into an .mp3 file. It fails whenever the source audio is AAC, as in most MP4 files:
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -vn -acodec copy audio.mp3
[mp3 @ 0x556acbf18680] Invalid audio stream. Exactly one MP3 audio stream is required.
[out#0/mp3 @ 0x556acbf15000] Could not write header (incorrect codec parameters ?): Invalid argument
[exit 234]
Copy into .m4a to keep the quality, or re-encode when you really need MP3:
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -vn -c:a copy audio.m4a && ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate -show_entries format=duration,size -of compact audio.m4a
stream|codec_name=aac|r_frame_rate=0/0
format|duration=10.032000|size=89701
[exit 0]
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -vn -c:a libmp3lame -q:a 2 audio2.mp3 && ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate -show_entries format=duration,size -of compact audio2.mp3
stream|codec_name=mp3|r_frame_rate=0/0
format|duration=10.080000|size=43923
[exit 0]
3. FFmpeg tricks on Linux for resize and crop
Scaling a 16:9 video to 640x480 does not crop anything. FFmpeg keeps the display aspect ratio in the metadata, so the frame is stored squeezed and still reports 16:9:
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -vf scale=640:480,crop=640:480:0:0 -an stretched.mp4 && ffprobe -v error -show_entries stream=width,height,display_aspect_ratio -of compact stretched.mp4
stream|width=640|height=480|display_aspect_ratio=16:9
[exit 0]
Scale to the target height first, then crop the width:
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -vf 'scale=-2:480,crop=640:480' -an cropped.mp4 && ffprobe -v error -show_entries stream=width,height -of compact cropped.mp4
stream|width=640|height=480
[exit 0]
libx264 with the usual pixel format needs even dimensions. -2 asks the scale filter to pick an even value:
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -vf scale=853:480 -c:v libx264 -an odd.mp4
[libx264 @ 0x55a083da25c0] width not divisible by 2 (853x480)
[vost#0:0/libx264 @ 0x55a083dfd900] Error while opening encoder - maybe incorrect parameters such as bit_rate, rate, width or height.
[vf#0:0 @ 0x55a083dfea40] Error sending frames to consumers: Generic error in an external library
[vf#0:0 @ 0x55a083dfea40] Task finished with error code: -542398533 (Generic error in an external library)
[vf#0:0 @ 0x55a083dfea40] Terminating thread with return code -542398533 (Generic error in an external library)
[vost#0:0/libx264 @ 0x55a083dfd900] Could not open encoder before EOF
[vost#0:0/libx264 @ 0x55a083dfd900] Task finished with error code: -22 (Invalid argument)
[vost#0:0/libx264 @ 0x55a083dfd900] Terminating thread with return code -22 (Invalid argument)
[out#0/mp4 @ 0x55a083dfb000] Nothing was written into output file, because at least one of its streams received no packets.
[exit 187]
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -vf scale=-2:480 -c:v libx264 -an even.mp4 && ffprobe -v error -show_entries stream=width,height -of compact even.mp4
stream|width=854|height=480
[exit 0]
4. Add a watermark
Like most FFmpeg tricks on Linux, this one is a filter. W and H are the video size and w and h the logo size, so this places the logo 10 pixels from the bottom right and copies the audio:
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -i logo.png -filter_complex 'overlay=W-w-10:H-h-10' -c:a copy marked.mp4 && ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate -show_entries format=duration,size -of compact marked.mp4
stream|codec_name=h264|width=1280|height=720|r_frame_rate=30/1
stream|codec_name=aac|r_frame_rate=0/0
format|duration=10.033333|size=3680915
[exit 0]
5. FFmpeg tricks on Linux to join files safely
cat does not join MP4 files. The result is twice the size and still plays 10 seconds:
$ cat a.mp4 b.mp4 > cat.mp4; ls -l a.mp4 cat.mp4 | awk '{print $5, $9}'; ffprobe -v error -show_entries format=duration -of csv=p=0 cat.mp4
3946104 a.mp4
7892208 cat.mp4
10.033333
[exit 0]
The concat demuxer works when both files share codecs, resolution and timing:
$ printf "file 'a.mp4'\nfile 'b.mp4'\n" > list.txt; ffmpeg -hide_banner -loglevel error -y -f concat -safe 0 -i list.txt -c copy joined.mp4; ffprobe -v error -show_entries format=duration -of csv=p=0 joined.mp4
20.087696
[exit 0]
With different inputs it can finish with status 0 and still produce a damaged file. The decoder check afterwards shows the problem:
$ printf "file 'a.mp4'\nfile 'other.mp4'\n" > mixed.txt; ffmpeg -hide_banner -loglevel error -y -f concat -safe 0 -i mixed.txt -c copy mixed.mp4; echo rc=$?; ffprobe -v error -show_entries stream=width,height -show_entries format=duration -of compact mixed.mp4
rc=0
stream|width=1280|height=720
stream|
format|duration=13.833208
[exit 0]
$ ffmpeg -hide_banner -v error -i mixed.mp4 -f null - 2>&1 | head -5
[null @ 0x5654192e2ec0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 301 >= 299
Last message repeated 47 times
[null @ 0x5654192e2ec0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 301 >= 300
[null @ 0x5654192e2ec0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 301 >= 301
[null @ 0x5654192e2ec0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 373 >= 373
[exit 0]
Of these FFmpeg tricks, the next one is the reliable choice. Normalise the inputs and use the concat filter instead. It re-encodes, which is the price of mixing formats:
$ ffmpeg -hide_banner -loglevel error -y -i a.mp4 -i other.mp4 -filter_complex '[0:v]scale=1280:720,fps=30,setsar=1[v0];[1:v]scale=1280:720,fps=30,setsar=1[v1];[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[v][a]' -map '[v]' -map '[a]' both.mp4 && ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate -show_entries format=duration,size -of compact both.mp4
stream|codec_name=h264|width=1280|height=720|r_frame_rate=30/1
stream|codec_name=aac|r_frame_rate=0/0
format|duration=15.063000|size=4687696
[exit 0]
6. Change speed, video and audio together
setpts changes video timing and atempo changes audio tempo. Apply both:
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -filter_complex '[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]' -map '[v]' -map '[a]' fast.mp4 && ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate -show_entries format=duration,size -of compact fast.mp4
stream|codec_name=h264|width=1280|height=720|r_frame_rate=60/1
stream|codec_name=aac|r_frame_rate=0/0
format|duration=5.013000|size=2649560
[exit 0]
Change only the video and the audio keeps its old length:
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -vf setpts=0.5*PTS -c:a copy half.mp4 && ffprobe -v error -show_entries stream=codec_type,duration -of compact half.mp4
stream|codec_type=video|duration=5.000000
stream|codec_type=audio|duration=10.032000
[exit 0]
7. Frame rate and exact cuts
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -vf fps=15 -c:a copy fps15.mp4 && ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate -show_entries format=duration,size -of compact fps15.mp4
stream|codec_name=h264|width=1280|height=720|r_frame_rate=15/1
stream|codec_name=aac|r_frame_rate=0/0
format|duration=10.066667|size=2538641
[exit 0]
A stream copy can only cut cleanly at keyframes. This file had them at 0 and 8.37 seconds:
$ ffprobe -v error -select_streams v -skip_frame nokey -show_entries frame=pts_time -of csv=p=0 output.mp4
0.000000,
8.366667
[exit 0]
Asking for 2 seconds from second 3 with -c copy gave 2.1 seconds; re-encoding gave exactly 2. The ffmpeg manual explains under -ss that with stream copy the extra data before the seek point is preserved.
$ ffmpeg -hide_banner -loglevel error -y -ss 3 -i output.mp4 -t 2 -c copy cut_copy.mp4 && ffprobe -v error -show_entries format=duration -of csv=p=0 cut_copy.mp4
2.100000
[exit 0]
$ ffmpeg -hide_banner -loglevel error -y -ss 3 -i output.mp4 -t 2 -c:v libx264 -c:a aac cut_exact.mp4 && ffprobe -v error -show_entries format=duration -of csv=p=0 cut_exact.mp4
2.000000
[exit 0]
8. Screen capture needs a display
On X11 the input device is x11grab. On a server or a session without that display it cannot start:
$ ffmpeg -hide_banner -loglevel error -f x11grab -i :0.0 -t 2 screen.mp4
[x11grab @ 0x56032c7c3f40] Cannot open display :0.0, error 1.
[in#0 @ 0x56032c7c3d40] Error opening input: Input/output error
Error opening input file :0.0.
Error opening input files: Input/output error
[exit 251]
$ ffmpeg -hide_banner -devices 2>&1 | grep -E 'x11grab|kmsgrab|pulse|alsa'
DE alsa ALSA audio output
D kmsgrab KMS screen capture
DE pulse Pulse audio output
D x11grab X11 screen capture, using XCB
[exit 0]
Under a Wayland session, use your desktop's recorder or a PipeWire-based tool; this was not tested here.
9. Streaming output
Writing HLS needs no server software to create the files:
$ ffmpeg -hide_banner -loglevel error -y -re -i output.mp4 -t 4 -c:v libx264 -preset veryfast -g 60 -c:a aac -f hls -hls_time 2 live.m3u8 && cat live.m3u8
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:2
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:2.000000,
live0.ts
#EXTINF:2.000000,
live1.ts
#EXT-X-ENDLIST
[exit 0]
Pushing to RTMP needs a server that listens. Without one:
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 -c copy -f flv rtmp://127.0.0.1/live/key
[tcp @ 0x55bf4bbf3280] Connection to tcp://127.0.0.1:1935?tcp_nodelay=0 failed: Connection refused
[rtmp @ 0x55bf4bbff280] Cannot open connection tcp://127.0.0.1:1935?tcp_nodelay=0
[out#0/flv @ 0x55bf4bbf2000] Error opening output rtmp://127.0.0.1/live/key: Connection refused
Error opening output file rtmp://127.0.0.1/live/key.
Error opening output files: Connection refused
[exit 145]
10. Batch FFmpeg tricks on Linux that survive failures
For batch work with FFmpeg tricks on Linux, a good loop quotes names with spaces, skips finished files, writes to a temporary name and reports failures. -nostdin stops FFmpeg from reading the keyboard or the loop's input:
$ cat conv.sh
#!/usr/bin/env bash
cd batch || exit 1
fail=0
for f in *.avi; do
out="${f%.avi}.mp4"
[ -e "$out" ] && { echo "skip $out"; continue; }
if ffmpeg -nostdin -hide_banner -loglevel error -n -i "$f" -c:v libx264 -c:a aac "$out.part.mp4"; then
mv -- "$out.part.mp4" "$out"; echo "ok $out"
else
rm -f -- "$out.part.mp4"; echo "FAIL $f"; fail=1
fi
done
[exit 0]
The second run skipped the finished files and tried the broken one again:
$ bash conv.sh
[in#0 @ 0x55ddb0be0e00] Error opening input: Invalid data found when processing input
Error opening input file broken.avi.
Error opening input files: Invalid data found when processing input
FAIL broken.avi
skip clip2.mp4
skip holiday video.mp4
[exit 1]
One more surprise for scripts: when the output exists and you did not pass -y, FFmpeg 7.1.5 declined to overwrite and still exited 0:
$ ffmpeg -hide_banner -loglevel error -i input.avi output.mp4
File 'output.mp4' already exists. Overwrite? [y/N] Not overwriting - exiting
Error opening output file output.mp4.
[exit 0]
Check that the output was really rewritten, or use -n and test for the file yourself. Using one file as input and output is refused:
$ ffmpeg -hide_banner -loglevel error -y -i output.mp4 output.mp4
Output output.mp4 same as Input #0 - exiting
Error opening output file output.mp4.
Error opening output files: Invalid argument
[exit 234]
Which of these FFmpeg tricks on Linux to remember
| Goal | Key option |
|---|---|
| Change container only | -c copy |
| Smaller file | higher -crf |
| Even size after scaling | scale=-2:H |
| Join identical files | concat demuxer |
| Join different files | concat filter |
| Exact cut | re-encode |
Whichever of the FFmpeg tricks you use, keep the original until you have played or decoded the result. If a job fails inside a larger script, the guide to checking exit codes in Bash helps you catch it. The filters behind these FFmpeg tricks on Linux are listed in the FFmpeg filters documentation.
Comments
Post a Comment