Useful ffmpeg Recipes
A few ffmpeg commands I find useful, so I don't need to keep reading the documentation.
Contents
- Convert to mp4
- Concatenate
- Speed Up
- Seek and Crop
- Resize
- Create Animated Gif
- Merge Audio into a Video with Audio
- Stream Video Device via TCP
- List Suported Formats for Device
Convert to mp4
Converts the input to mp4 using libx264, using a quality of 35 (0-51, where o is lossless) and 256k audio compression (high quality). This video should be compatible with most devices.
ffmpeg -i "input.ext" -c:v libx264 -crf 35 -b:a 256k "output.mp4"
Concatenate
Concatenates the videos specified in file.txt
ffmpeg -f concat -safe 0 -i "file.txt" -c copy "output.ext"
The contents of file.txt (must be single quotes)
file 'path/to/file1.ext'
file 'path/to/file2.ext'
file 'path/to/file3.ext'
...
Speed Up
Speeds up the input video 256 times, and strips the audio with -an
ffmpeg -i "input.ext" -an -filter:v "setpts=PTS/256" "output.ext"
Seek and Crop
Do the following:
-ss 10seek to 10 seconds-ttake 4 seconds of videocrop=w=1280:h=500:x=0:y=110take a1280×500box at coordinate0,110
ffmpeg -ss 10 -t 4 -i "input.ext" -filter:v "crop=w=1280:h=500:x=0:y=110" "output.ext"
Resize
Resize the video to 640 pixels wide, making ffmpeg calculate the height based on the aspect ratio of the input video.
ffmpeg -i "input.ext" -vf scale=640:-1 "output.ext"
Create Animated Gif
Create an animated gif using a palette specifically for the video using a single command. Notable parameters:
-ss <n>to skip a specified number of seconds-t <n>to specify the length of the giffps=<n>to specify the frame rate (part of-vf)-loop 0to loop forever
ffmpeg -ss 2 -t 6 -i "input.ext" -vf "fps=24,scale=0:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 "output.gif"
Merge Audio into a Video with Audio
Merge an audio file into a video file which already has audio, without re-encoding the video:
ffmpeg -i "input-video.ext" -i "input-audio.ext" -filter_complex "[0:a:0][1:a:0]amix=inputs=2:duration=longest[aout];[aout]volume=2.0[a_final]" -c:v copy -map 0:v:0 -map [a_final] -c:a aac -b:a 192k output-video.mp4
The audio is boosted 2x, because I found that merging two stereo streams resulted in an audio track which peaks at 50% volume.
Stream Video Device via TCP
Stream a video device directly via TCP. Notable parameters:
-i /dev/video0the input device - can also be-i -to read from a piped command-c:vthe codec to use (h264_v4l2m2mcan be hardware encoded on Raspberry Pi 4 hardware)-b:v 1Mthe bitrate to use (e.g. 1 megabit)-f mpegts tcp://0.0.0.0:2000\?listenthe format to serve up on the specified socket
ffmpeg -i /dev/video0 -c:v h264_v4l2m2m -b:v 1M -f mpegts tcp://0.0.0.0:2000\?listen
An example of TCP streaming using camera input piped from rpicam-vid (in this case for use with a non-USB Raspberry Pi camera):
rpicam-vid --nopreview --timeout 0 --framerate 10 --width 1152 --height 864 --hflip --vflip --output - | ffmpeg -i - -c:v h264_v4l2m2m -b:v 1M -f mpegts tcp://0.0.0.0:2000\?listen
Another example, except this time there's no re-encoding, the stream is just re-muxed:
rpicam-vid --nopreview --timeout 0 --framerate 10 --width 1920 --height 1080 --codec h264 --bitrate 1000000 --inline --output - | ffmpeg -i - -c:v copy -mpegts_flags resend_headers -f mpegts tcp://0.0.0.0:2000?listen
And another example, where rpicam-vid streams directly, no use of ffmpeg:
rpicam-vid --nopreview --timeout 0 --framerate 10 --width 1080 --height 1080 --hflip --codec libav --libav-format mpegts --bitrate 1000000 --inline --output tcp://0.0.0.0:2000?listen=1
List Suported Formats for Device
The below command will list all video formats supported by /dev/video0 for use with ffmpeg:
ffmpeg -list_formats all -i /dev/video0
Example output:
[video4linux2,v4l2 @ 0x559b1735c0] Compressed: mjpeg : Motion-JPEG : 1920x1080 1600x1200 1360x768 1280x1024 1280x960 1280x720 1024x768 800x600 720x576 720x480 640x480
[video4linux2,v4l2 @ 0x559b1735c0] Raw : yuyv422 : YUYV 4:2:2 : 1920x1080 1600x1200 1360x768 1280x1024 1280x960 1280x720 1024x768 800x600 720x576 720x480 640x480
This shows that the v4l2 format can be used with an input format of yuyv422, with one of the listed resolutions. This can be used in a command like so:
ffmpeg -f v4l2 -input_format yuyv422 -video_size 1280x720 -i /dev/video0 <output>
🏷️ video audio stream device input command gif tcp ffmpeg convert mp4 seek resize animated merge
Please click here to load comments.