Using ffmpeg to Manipulate Video Files

FFmpeg is a collection of libraries and tools for manipulating video, audio, and other multimedia files and streams. It is frequently used for basic editing (cutting and joining), video scaling, post-production video effects, and standard compliance (SMPTE, ITU).

The main component of FFmpeg is the command-line tool ffmpeg, which reads an input file, applies transformations, and writes to an output file.

The basic usage pattern of ffmpeg is ffmpeg -i <input file> [options] <output file>. ffmpeg automatically selects the decoder and encoder based on the extension of <input file> and <output file>, and we specify what transformations we apply to the original video file in [options].

We now explain what to write in [options] to address typical video manipulation demands.

Cut a Video

Add the option -ss <start time in hours>:<start time in minutes>:<start time in seconds> -to <end time in hours>:<end time in minutes>:<end time in seconds>.

If there is no need to transcode the video format or apply other transformations, add -c:v copy -c:a copy for increased speed.

For example, ffmpeg -i input.mp4 -ss 00:05:10 -to 00:15:30 -c:v copy -c:a copy output.mp4 saves input.mp4 from 00:05:10 to 00:15:30 to output.mp4 with no transcoding or other transformations, resulting in a 10 minutes and 20 seconds video.

Change Frame Rate

Add the option -r <frame rate>.

For example, ffmpeg -i input.mp4 -r 12 output.gif controls the frame rate of output.gif generated by transcoding to 12FPS.

Change Output Resolution

Add the option -s <length> x <width>. The units of <length> and <width> are in pixels.

It is usually required that the length and width be scaled equally from the original length and width to avoid distortion of the picture.

For example, ffmpeg -i input.mp4 -s 320x180 output.gif controls the resolution of the output.gif generated by transcoding to 320x180.

Change Playback Speed

  • To speed up the output video to k times its original size: add the option -filter:v 'setpts=PTS/<k>'.
  • To slow down the output video to k times the original: add the option -filter:v 'setpts=<k>*PTS'.

For example, ffmpeg -i input.mp4 -filter:v 'setpts=PTS/2' output.gif (or ffmpeg -i input.mp4 -filter:v 'setpts=0.5*PTS' output.gif) will transcode the resulting output.gif to speed up to 2 times the original.

Extract Audio

Before we extract audio, we need to first look up media information using ffprobe, a command-line tool installed with ffmpeg:

1
2
3
4
5
6
7
8
9
10
$ ffprobe video.mkv
...
Input #0, matroska,webm, from 'video.mkv':
Metadata:
encoder : no_variable_data
creation_time : 1970-01-01T00:00:00.000000Z
Duration: 00:23:30.07, start: 0.000000, bitrate: 1392 kb/s
Stream #0:0: Audio: aac (LC), 48000 Hz, stereo, fltp (default)
Metadata:
...

We can see that the format of the audio stream is aac. Now, we can add the option -map 0:a -acodec copy to copy the audio stream. Note that the extension of output file should correspond with the format of the audio stream.

For example, we run the following command to save the audio stream of video.mkv to audio.mp4: ffmpeg -i video.mkv -map 0:a -acodec copy audio.mp4. Note that .mp4 is a valid extensions of aac audio streams.

References

  • https://en.wikipedia.org/wiki/FFmpeg
  • https://shotstack.io/learn/use-ffmpeg-to-trim-video/
  • https://homehack.nl/create-animated-gifs-from-mp4-with-ffmpeg/
  • https://superuser.com/questions/1261678/how-do-i-speed-up-a-video-by-60x-in-ffmpeg/1261681
  • https://www.baeldung.com/linux/ffmpeg-audio-from-video

Using ffmpeg to Manipulate Video Files
https://abbaswu.github.io/2023/02/18/Using-ffmpeg-to-Manipulate-Video-Files/
Author
Jifeng Wu
Posted on
February 18, 2023
Licensed under