FFmpeg Basic commands


Basic commands for FFmpeg

Change media file container

$ ffmpeg -i input.flv -codec copy output.mp4

This command will change input media file’s container into your desire container.

Force encode

$ ffmpeg -i input.flv -strict -2 output.mp4

This command is experimental and might not product best outcome.

Auto insert multiple videos

-fflags +discardcorrupt

Tip:This command will allow ffmpeg takes a flag which is discard corrupted data in input meida file.

$ ffmpeg -fflags +discardcorrupt -f concat -safe 0 -i merge.txt -c copy output.mp4

This command will allow you to insert multiple videos to a single video file.

Set target bitrate and audio rate

-b:a //audio rate with unit
-b:v //video rate with unit

Cut using a duration

$ ffmpeg -i input.mp4 -ss 00:08:20 -t 00:10:00 -c:v copy -c:a copy output.mp4

This command will take a input video input.mp4, and cut out 10 minutes from it starting from 00:08:20.
For example, if it cut from 00:08:20, the output video file will have duration from 00:08:20 to 00:18:20.

-ss specifies the starting position and -t specifies the duration from the start position. In the above command, we cut 10 minutes from the 00:08:20 mark.

The -c:v copy -c:a copy commands copy the original audio and video without re-encoding.

To specify time, you can use two different time unit formats: sexagesimal (HOURS:MM:SS.MILLISECONDS, e.g. 01:23:45.678), or in seconds. If using the former, you can leave out the milliseconds HOURS:MM:SS as we did in our example.

If you specify a duration that will result in a stop time that is beyond the length of the input video, the output video will end where the input video ends.

Cut using a specific time

$ ffmpeg -i input.mp4 -ss 00:08:20 -to 00:20:20 -c:v copy -c:a copy output.mp4

This command uses to to specify an exact time to cut to from the starting position. The cut video will be from 00:08:20 to 00:20:20, resulting in a 12 minutes video.

If you specify a time -to that is longer than the input video, e.g. -to 00:35:00 when the input video is 20 minutes long, the cut video will end where the input video ends. If you specify a -to that is smaller than -ss, then the command won’t run. You’ll get the following error: Error: -to value smaller than -ss; aborting.

Note that if you specify -ss before -i, -to will have the same effect as -t, i.e. it will act as a duration.

Cut the end of a video

$ ffmpeg -sseof -500 -i input.mp4 -c copy output.mp4

$ ffmpeg -sseof -00:10:00 -i input.mp4 -c copy output.mp4

Both of the above two commands will make a cut of the last 10 minutes of the input video.

If you use a time that is longer than the input video, e.g. -01:10:00 for a 20 minute video, the command will still run. The output video will be the same length as the input.

Use hardware acceleration

Using NVIDIA cuda example:

$ ffmpeg -hwaccel cuda -i input.mp4 output.mp4

Example for using hardware acceleration and keep output video in 30 fps constant framerate(NVIDIA verson):

$ ffmpeg -hwaccel cuda -i input.mp4 -filter:v 30fps output.mp4

For Mac

Using h264_videotoolbox to enable hardware accelertaion.

$ ffmpeg -i input.mp4 -c:v h264_videotoolbox b:v 10M output.mp4

Adding image and subtitle to a audio file and output as video

Single image with audio

If adding audio (e.g audio.wav) with one single image and output as a video, you need to add -shortest to tell ffmpeg to stop after audio stream is ended. Using internal AAC encoder as example, but can use any other supported AAC encoder as well:

$ ffmpeg -loop 1 -i img.jpg -i audio.wav -c:v libx264 -c:a aac -b:a 192k -shortest out.mp4

If you want to set the frame size of the video. Add -vf scale=1920:1080

$ ffmpeg -loop 1 -i img.jpg -i audio.wav -c:v libx264 -c:a aac -b:a 192k -shortest -vf scale=1920:1080 out.mp4

If your audio file is using a codec that the output container supports (e.g. MP3 audio in AVI or M4A/AAC audio in MP4), you can stream copy (re-mux) it instead of re-encoding, which will preserve the audio quality:

$ ffmpeg -loop 1 -i img.jpg -i audio.m4a -c:v libx264 -c:a copy -shortest out.mp4

Optional commands

// loop input stream at 1 framerate.
-loop 1 -framerate 1 

// set aspect of the video as 16:9
-vf scale=1920:1080,setdar=16/9

Adding subtitle to video

There is two way of adding subtitle to a video. First one is to add it as hard subtitle which is burn captions into the video. Second one is to add it as soft subtitle that viewers can turn it on or off.

Hard subtitle

.ass file

$ ffmpeg -i video.mp4 -vf ass=subtitle.ass output_ass.mp4

.srt file

$ ffmpeg -i video.mp4 -vf subtitles=subtitle.srt output_srt.mp4

If you don’t want to re-render the audio source, add -c:a copy. Since you want to add subtitle to the video as hard way, you cannot use -c:v copy:

$ ffmpeg -i video.mp4 -c:a copy -vf ass=output_subtitle.ass out.mp4
Soft subtitle
$ ffmpeg -i video.mp4 -i subtitle.srt -c copy -c:s mov_text -metadata:s:s:0 language=eng out.mp4

The -c:s mov_text option sets the input video file format to the MOV_TEXT format.

We’re also using the -metadata:s:s:0 option, which sets metadata for Stream:Subtitle: Number of the stream, starting with 0.

Finally, the language=eng option sets the subtitle language to English.

Reference

FFmpeg Documentation

How to trim a video using FFmpeg

Using Hardware Acceleration on MacOS with FFmpeg

Slideshow

How to Embed Subtitles into a Video Using FFmpeg


Author: Ashless
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Ashless !
  TOC