vxhviet
5/11/2016 - 6:55 AM

FFmpeg commands

FFmpeg commands

FFmpeg commands:

General Info Useful Commands FFmpeg Docs

1. -y: Overwrite output files without asking.

2. Sample command to show GOP / I frame structure using ffprobe (hit q to cancel the output): Source

ffprobe -i input.mp4 -select_streams v -show_frames -of csv -show_entries frame=pict_type

or Source

ffprobe -select_streams v:0 -show_frames goptest.mov |grep key_frame|less

or Source

ffprobe -show_packets -print_format compact -select_streams v:0 filename | grep flags=K

3. Add more key frame: Source

ffmpeg -i Android_Test_Source.mp4 -force_key_frames "expr:gte(t,n_forced*0.05)" out5.mp4

4. Show metadata: Source, Source

ffprobe -show_streams 720p_VID_20160509_103821.mp4

5. Set video rotation without reencoding: Source

ffmpeg -i input.mp4 -c copy -metadata:s:v:0 rotate=90 output.mp4

6. Convert and set video rotation: Source

ffmpeg -noautorotate -i input.mp4 -force_key_frames "expr:gte(t,n_forced*0.05)" -metadata:s:v:0 rotate=90 output.mp4

7. Fast convert (reduce resolution, use x264 -preset ultrafast, remove audio -an): Source, Source

ffmpeg -i Android_Test_Source.mp4 -force_key_frames "expr:gte(t,n_forced*0.05)" -c:v libx264 -preset ultrafast -s 640*480 -y -an out.mp4

Possible enhancement Stack

8. Change the container from .mov to .mp4 (if the video bitstream is valid for mp4): Source, Source

ffmpeg -i input.avi -c:v copy -y output.mp4` or `ffmpeg -i input.mov -vcodec copy -y output.mp4

9. Trim Video with Input Seeking (both fast and accurate SOURCE), without reencoding SOURCE

ffmpeg.exe -ss 00:00:03 -i INPUT.MP4  -t 00:00:02 -c copy OUTPUT.MP4

-ss: Used before -i for input seeking, this seeks in the input file (INPUT.MP4) to position.

-i: This specifies the input file. In that case, it is (INPUT.MP4).

00:01:00: This is the time your trimmed video will start with.

-t: This specifies duration, like -ss 60 -t 10 to capture from second 60 to 70.

or

-to: This specify an out point, like -ss 60 -to 70 to capture from second 60 to 70.

00:02:00: This is the time your trimmed video will end with.

-c copy: This is an option to trim via stream copy. (NB: Very fast)

10. Add an animated image (GIF) to video as Overlay: Source, Source, not really relevant Source

FFmpeg is able to place the overlay in some particular coordinates in the video at each particular moment of time. Putting it simply you can set x=F(t) and y=G(t), where х and у are coordinates and F(t) and G(t) are general time functions. The command for simple adding looks like:


ffmpeg -i INPUT.mp4
-itsoffset 00:00:02 
-ignore_loop 0 
-i IMAGE.gif 
-filter_complex [1:v]scale=h=-1:w=300[overlay_scaled],[0:v][overlay_scaled]overlay=eval=init:x=W*0.5:y=H*0.5:shortest=1 
-preset ultrafast 
-g 120 
OUTPUT.mp4

String strFilter = "[1:v]scale=h=-1:w=" + widthOverlay + "[overlay_scaled],"
  + "[0:v][overlay_scaled]overlay=eval=init:x=W*" + xPositionPercent
  + ":y=H*" + yPositionPercent + ":shortest=1";

String[] сmd = new String[] {
  "-i",
  strPathSrcVideo,
  "-itsoffset",
  String.valueOf(timeSecStartAnimation),
  -ignore_loop",
  "0",
  "-i",
  strPathOverlay,
  "-filter_complex",
  strFilter,
  "-preset",
  "ultrafast",
  "-g",
  "120",
  strPathDstVideo
};

Where:

widthOverlay is the final width of the overlay just as it will be in the video.

xPositionPercent is the left margin in the percent from the video width.

yPositionPercent is the top margin in the percent from the video height.

strPathSrcVideo is the full way to the source video.

timeSecStartAnimation is the time when the overlay appears on the video, for example "00:00:02" (appear after 2 seconds).

strPathOverlay is the complete way to the source overlay.

strPathDstVideo is the way for saving the result.

-ignore_loop 0 to keep the animation looping, it should be used with shortest=1, otherwise the encoding will run indefinitely since one of the inputs is looping (See source 2).