Skip to content

Latest commit

 

History

History
233 lines (174 loc) · 6.05 KB

File metadata and controls

233 lines (174 loc) · 6.05 KB

FFMPEG FFMPEG

Official: www.ffmpeg.org

1. Basic

ffmpeg -i <input_file> <output_file>

2. Presets

ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow

ffmpeg -i video.mov -preset veryslow video.mp4

3. video ⇐⇒ images

3.1. video ⇒ images

ffmpeg -i video.mov   -q:v 1                           %05d.jpg   # (1)
ffmpeg -i video.mov   -q:v 1 -vframes 1 -ss 00:10:00   image.jpg  # (2)
  1. convert video to a sequence of jpeg files.

  2. extracts a single image at time 10s.

option example comment

-q:v 1

Set the quality of the video encoder. Values range from 1 to 31, where lower means better.

%05d.jpg

save to jpeg files, numbered in order 00000.jpg, 00001.jpg, …​

-vframes 1

Set the number of frames to output.

-ss 00:10:00

starting at 10 min from the beginning.

-r 4 -i input.mp4

As an input option, set FPS. ie. regenerate timestamp with given FPS.

-i input.mp4 -r 4

As an output option, duplicate or drop input frames to achieve constant output frame rate fps.

3.2. images ⇒ video

ffmpeg -framerate 1/5 -i img%03d.png -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4
ffmpeg -pattern_type glob -i "image-*.png" out.mp4  # Not numbered or no regular pattern
Note
Use -loop 1 as an input option for an infinite loop, and then use -t 00:30:00 as an output option to cut off the video at thirty minutes.

If file names does not comply the %d nomenclature (not starting 0), print the list into a file like:

mylist.txt
# this is a comment
file '/path/to/file1.jpg'
file '/path/to/file2.jpg'
file '/path/to/file3.jpg'
ls /volumes/data/sample/images/left/* | xargs -L 1 -I {} echo "file '{}'" >> mylist.txt
ffmpeg -f concat -i mylist.txt -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4

4. Timing / framerate

4.1. trim

You can 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.

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

4.2. framerate

option

placement

description

-r

input

retimes input frames at that rate.

output

will duplicate or drop frames to achieve the given rate.

ffmpeg         -i input.mp4   -r 2                output.gif  # (1)
ffmpeg         -i input.mp4   -filter:v "fps=2"   output.gif  # (1)
ffmpeg -r 2    -i input.mp4                       output.gif  # (2)
  1. retime: drop frames to meet 2 fps.

  2. reinterpret: slow down framerate to meet 2 fps.

4.3. Patrol Cycle (back and forth)

ffmpeg -i input.mp4 -filter_complex "[0]reverse[r];[0][r]concat,setpts=N/25/TB" output.mp4

The setpts is applied to avoid frame drops, and the value 25 should be replaced with the framerate of the clip.

5. Geometry

5.1. crop

ffmpeg -i input.mp4    -filter:v "crop=100:100:12:34"   output.mp4
ffmpeg -i input.mp4    -filter:v "crop=200:ih:12:34"    output.mp4

5.2. scale

ffmpeg -i input.mp4    -filter:v "scale=640:-1"      output.mp4 # (1)
ffmpeg -i input.mp4    -filter:v "scale=iw/2:-1"     output.mp4 # (2)
  1. rescale the video 640 pixel width, and height keeps aspect ratio.

  2. downscale the video by 4 (2 in each dimension).

5.3. rotation

fmpeg -i input.mp4    -filter:v "transpose=1"     output.mp4

with possible values :

  • 0 = 90CounterCLockwise and Vertical Flip (default)

  • 1 = 90Clockwise

  • 2 = 90CounterClockwise

  • 3 = 90Clockwise and Vertical Flip

6. Stabilization

# first pass
ffmpeg -i movie.mp4 -vf vidstabdetect=stepsize=6:shakiness=10:accuracy=15:result=motions.trf -f null -
# second pass
ffmpeg -i movie.mp4 -vf vidstabtransform=input=motions.trf:smoothing=60:relative=1,unsharp=5:5:0.8:3:3:0.4 \
-vcodec libx264 -preset veryslow -tune film -acodec copy -y smooth.mp4

7. stereo 3D

Note
to use filter stereo3d, you may have a recent version of ffmpeg.

side by side half width left first to Red cyan gray/monochrome

ffmpeg -i SbS.mp4 -vf stereo3d=sbs2l:arbg -y anaglyph.mp4

with :

sbs

side by side

2

half width

l

left first

a

anaglyph

rbg

red blue grey

If the output video is still squeezed, use :

ffmpeg -i SbS.mp4 -vf "stereo3d=sbsl:arcg,scale=iw*2:ih" -y anaglyph.mp4

with :

sbsl

side by side left first

arcg

anaglyph red/cyan gray

scale=iw*2:ih

squeeze horizontally

8. Audio

ffmpeg -i input.wav -codec:a libmp3lame -qscale:a 0 output.mp3

0 is better

9. Metadata

definition
ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 input.mp4
1280x720
number of frames
# query the video stream
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 input.mp4

# query the container
ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 input.mp4