ffmpeg
Updated: October 15, 2024
FFMPEG is a video and audio converter. Used under the hood in many applications.
Many of these applications functions can be performed right from CLI with better efficiency. It can even grab from live audio/video sources!
Table of Contents
tldr
ffmpeg commands
# simplest video or audio convert
ffmpeg -i video.mov out.mkv
# process without reencoding
ffmpeg -i video.mov -c:v copy -c:a copy out.mkv
# make a video streamable
ffmpeg -i video.mkv -movflags faststart out.mkv
# extract a frame from video to make thumbnail
ffmpeg -i video.mkv -vframes 1 10.0 thumbnail.jpg
# trim video from the beginning
ffmpeg -i video.mkv -ss 10.0 out.mkv
# trim video from the end
ffmpeg -i video.mkv -t 10.0 out.mkv
# scale down resolution (-2=auto)
ffmpeg -i video.mkv -vf scale=-2:720 out.mkv
# create a GIF or meme (7 fps)
ffmpeg -i video.mkv -ss 10.0 -t 5.0 -vf scale=-2:240 -r 7 -loop 0 thumbnail.gif
# take audio out of a video
ffmpeg -i video -map [0:v] out.mkv
# put audio track into a video
ffmpeg -i video.mkv -i audio.mp3 [0:v] -map [1:a]
# loop a video serveral times into a long video
ffmpeg -re -f lavfi -i "movie=filename=video.mkv:loop=0,setpts=n/(FRAME_RATE*TB)" -t 600 out.mkv
# create a video from an image
ffmpeg -loop 1 -i image.png -t 600 out.mkv
only for x11
# save screen recording
ffmpeg -f x11grab -i :0.0 -f pulse -i 0 output.mp4
# record screen as 5s gif
ffmpeg -f x11grab -framerate 10 -video_size 800_600 -i :0.0+0,30 -t 5 output.gif
# save a screenshot as png
ffmpeg -f x11grab -i :0 -t 1 -f mjpeg output.png
ffplay commands
# play a video
ffplay output.mp4
# play audio only
ffplay -nodisp output.mp4
# stream audio from yt vid
youtube-dl https://www.youtube.com/watch?v=ZEPQDxEXRBU -o - | ffplay - -nodisp
ARGUMENTS
ffmpeg
-i # input file
-t # record or transcode duration
-r # set frame rate
-L # show license
-map # add or change an audio track
-re # read input at native frame rate
-ss # set start time
-vn # no video
-vf scale # scale down resolution
-movflags faststart # transcode a video for the web (browser read file w/o downloading)
-c:v # copy video without transcoding
-c:a # copy audio without transcoding
-formats # show available formats
-aspect # set aspect ratio (4:3 16:9 etc)
X11
macOS
# find screen numbers
ffmpeg -f avfoundation -list_devices true -i ""
# once you have screen index number
ffmpeg -f avfoundation -i "<screen index>:<audio index>" out.mkv
Linux
# just grab video with no sound from upper left corner
ffmpeg -video_size 1920x1024 -framerate 25 -f x11grab -i :0.0+100,200 out.mkv
# record video and sound ALSA
ffmpeg -video_size 1920x1024 -framerate 25 -f x11grab -i :0.0+100,200 -f alsa -ac 2 -i hw:0 out.mkv
# record video and sound PULSE
ffmpeg -video_size 1920x1024 -framerate 25 -f x11grab -i :0.0+100,200 -f alsa -ac 2 -i default out.mkv
Windows
# list devices
ffmpeg -list_devices true -f dshow -i dummy
# grab image from entire desktop
ffmpeg -f dshow -i video="screen-capture-recorder" out.mkv
# grab entire screen with audio
ffmpeg -f dshow -i video="UScreenCapture":audio="Microphone" out.mkv
Use builtin GDI screengrabber
# capture all screens in one big display
ffmpeg -f gdigrab -framerate 30 -i desktop out.mkv
# limit region and show area being grabbed
ffmpeg -f gdigrab -framerate 30 -offset_x 10 -offset_y 20 -video_size 640x480 -show_region 1 -i desktop out.mkv
# grab contents from window named "something"
ffmpeg -f gdigrab -framerate 30 -i title=something out.mkv