View Page Source

Revision (current)
Last Updated by GMP on 12/21/2022 3:11 PM
Back to Page

A compilation of FFmpeg commands that I have found helpful. Leaving it here for my reference (as well as for anyone it may be of help).

Note: I specified common formats like avi, mp4 for the sake of clarity, but ffmpeg works a lot of formats, so don't hesistate to try any video/audio format you find desirable.

%%TOC%%
!! The basic structure of a command
* All ffmpeg commands include a call to the ffmpeg execuatable an input file and an output file. The call to the executable can look just like {{ffmpeg}} if the ffmpeg binary is added to the system environment variables correctly, otherwise it may look like {{path/to/ffmpeg.exe}}.

* The input file is specified after the flag {{-i}}. In case of multiple input files, this flag must be specified each time.

* The output file is specified without any flags after the input file(s). If nothing is specified, default encoding parameters will be used, but they can be customized by specifying it between the input and output files.

!! Stream copy flag
Knowing about this flag and where to use it can save a lot of conversion time. If you are sure that the video stream doesn't need to be re-encoded, you can apply the {{-c copy}} flag to skip it. An example of where this applies is when you concat some files in the same format. An example where this doesn't apply is when you change the dimensions of the video. You can still use {{-c:a copy}} which copies the audio stream when you are doing operations only on the video, and {{-c:v copy}} when you operate only on the audio.

!! Changing the dimensions of the video
{{ffmpeg -i input.avi -vf scale=$w:$h output.avi}}

Where {{$w}} and {{$h}} are to be replaced with the desired height and width respectively. If you want to specify one of the parameters, you can set the other to -1 to maintain fixed aspect ratio. For example, you can convert to 1080p like so:
%%%{{ffmpeg -i input.avi -vf scale=-1:1080 output.avi}}

!! Concatenating a bunch of files
First you need to make a list.txt file which lists all the files that need to be concatenated. Here is an example of how that file can look like (assuming all the files are in the same folder):
 file 'start.avi'
 file 'middle.avi'
 file 'end.avi'
Then run the following command in the same folder:
%%%{{ffmpeg -f concat -safe 0 -i list.txt merge.mp4}}

This will concat the specified files into a single mp4 file. You can use other video filters along with this command as well and it will be applied to each input file.

!! Batch converting files in a folder
* Windows - {{for %i in (*.avi) do ffmpeg -i "%i" "%~ni.mp4"}}
* Linux, Mac - {{for i in *.avi; do ffmpeg -i "$i" "${i%.*}.mp4"; done}}

This will convert all the avi files in the current folder to mp4 files. You can change to work with other formats. If you are converting to the same format make sure to store the output files in a different folder else it may get stuck in an infinite loop.

!!Basic Fade effects
{{ffmpeg -i input.avi -vf "fade=t=in:st=$s:d=$d" output.avi}}
* This will add a fade in effect to the video. {{$s}} is the starting timestamp in seconds, and {{$d}} is the desired duration of the fade in seconds.
* Replace {{fade=t=in}} with {{fade=t=out}} for fade out.
* Add a similar {{-af "afade=t=in:st=$s:d=$d"}} flag for audio fade.

!! Horizontally stacking 2 videos
{{ffmpeg -i left.mp4 -i right.mp4 -filter_complex hstack output.mp4}}