Table of Contents

ffmpeg

Converting formats with ffmpeg

Animated GIF to MPEG-4

From https://unix.stackexchange.com/questions/40638/how-to-do-i-convert-an-animated-gif-to-an-mp4-or-mv4-on-the-command-line and http://rigor.com/blog/2015/12/optimizing-animated-gifs-with-html5-video:

ffmpeg -i input.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" output.mp4

movflags: optimizes the structure of the MP4 file so the browser can load it as quickly as possible
pix_fmt: MP4 videos store pixels in different formats. We include this option to specify a specific format which has maximum compatibility across all browsers
vf: MP4 videos using H.264 need to have a dimensions that are divisible by 2. This option ensures that’s the case.

Converting AVI to MP4

From https://andre.blue/blog/converting-avi-to-mp4-with-ffmpeg/:

ffmpeg -i input.avi -c:a aac -b:a 128k -c:v libx264 -crf 23 output.mp4

Converting WEBM to MP4

ffmpeg -i input.webm -vcodec libx264 output.mp4

Converting MP4 to MP3

ffmpeg -i video.mp4 -vn -acodec libmp3lame -ac 2 -qscale:a 4 -ar 44100 -write_xing 0 audio.mp3

Converting MP3 to MP4

See also https://trac.ffmpeg.org/wiki/Slideshow:

ffmpeg -loop 1 -i image.jpg -i audio.mp3 -c:a copy -c:v libx264 -shortest out.mp4

Converting WEBP to JPG

for f in *.webp; do ffmpeg -i $f ${f%.webp}.jpg; done

Creating a slideshow from images

ffmpeg -framerate 0.5 -pattern_type glob -i 'frame-*.jpg' -c:v libx264 -pix_fmt yuv420p out.mp4

Simple Manipulations

Adjusting the speed of a video file

ffmpeg -i input.mov -filter:v "setpts=PTS/10" -an output.mp4

Double speed of audio and video, keeping the pitch the same

ffmpeg -i input.mkv -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mkv

Resizing

ffmpeg -i input.mp4 -vf scale=640:360 output.mp4

Superimpose an image onto some video

ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" -codec:a copy output.mp4

or just 'overlay' if you don't need an offset

Deinterlacing Video with ffmpeg

From http://macilatthefront.blogspot.com/2017/04/deinterlacing-hd-footage-without-losing.html:

ffmpeg -i "input_video.avi" -vf "bwdif" -c:v libx264 -preset slow -crf 18 -pix_fmt yuv420p -c:a aac -b:a 320k "output_video.mp4"

Adding Audio Tracks

From https://stackoverflow.com/questions/11779490/how-to-add-a-new-audio-not-mixing-into-a-video-using-ffmpeg:

ffmpeg -i video.mkv -i audio.mp3 -map 0 -map 1:a -c:v copy -shortest output.mkv

or -c copy if the input audio format is compatible with the output format. From https://askubuntu.com/questions/770370/how-would-you-label-an-audio-track-as-a-japanese-surround-5-1-in-mp4box-or-ffm, to label / title audio tracks (there doesn't seem to be a standard way of reading this):

<cpde>ffmpeg -i test.mp4 -c copy -metadata:s:a:0 language=jpn -metadata:s:a:0 title=“5.1 Surround” testing.mp4</code>