Skip to content

Instantly share code, notes, and snippets.

@jnbek
Forked from ohjho/ffmpeg_cheatsheet.md
Created April 3, 2022 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnbek/9728d85e47878029addde6afe5f239f1 to your computer and use it in GitHub Desktop.
Save jnbek/9728d85e47878029addde6afe5f239f1 to your computer and use it in GitHub Desktop.

Conversion without lose of Quality

ffmpeg -i file.mov -c copy out.mp4

you can remove audio by using the -an flag

Conversion with Compression

ffmpeg -i input.mov -vcodec libx264 -crf 20 output.mp4

The CRF, which, in this case, is 20, can range from 18 to 24, where a higher number will compress the output to a smaller size.

The video codec used here is x264

Reference from here

Slicing Video

use the -ss option to specify a start timestamp, and the -t option to specify the encoding duration. The timestamps need to be in HH:MM:SS.xxx format or in seconds

Example for clipping 10 seconds, 30 seconds from the start:

ffmpeg -ss 00:00:30.0 -i input.wmv -c copy -t 00:00:10.0 output.wmv
ffmpeg -ss 30 -i input.wmv -c copy -t 10 output.wmv

Note that -t is an output option and always needs to be specified after -i. Use the -to arg to specify the target end time

ffmpeg -i <input> -filter:v fps=fps=30 <output>

Time Lapse from a Video

To take every 15th frame, use

ffmpeg -i in.mp4 -vf select='not(mod(n,15))',setpts=N/FRAME_RATE/TB out.mp4

Another method is to use the framestep filter

ffmpeg -i in.mp4 -vf framestep=15,setpts=N/FRAME_RATE/TB out.mp4

Video to Gif

see this useful post

Getting Information like FPS

ffmpeg -i filename

For more see this post

The font file path you need is probably gonna be at /usr/share/fonts on Linux or '/Library/Fonts/' on MacOS

ffmpeg -i input.mp4 -vf "drawtext=fontfile=Arial.ttf: text='Frame\: %{frame_num}': start_number=0: x=(w-tw)/2: y=h-(2*lh): fontcolor=black: fontsize=20: box=1: boxcolor=white: boxborderw=5" -c:a copy output.mp4

This create a video that's like this:

frame number overlay demo

ffmpeg -i input.mp4 -q:a 0 -map a output_file.m4a
  • -q:a: variable bitrate with 0 being the highest quality
  • -map a: select audio stream only
  • file extension, in general m4a is a good idea because ffmpeg should be smart enough to guess your intent and figure out the right codec of your output audio file and encapsulate the raw AAC into the output M4A container
 ffmpeg -i /input/track1.m4a a -i ~/input/track2.m4a -filter_complex '[0:0][1:0]concat=n=2:v=0:a=1[out]' -map '[out]' output/track.m4a

for adding a third track simple replace [0:0][1:0]concat=n=2 with [0:0][1:0][2:0]concat=n=3

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment