Karl SvenssonBlogLinkedIn

Nifty ffmpeg commands

Written 2023-11-25 by Kalle
2 min read | 362 words

Intro

Below is a list of nifty ffmpeg commands for various use cases

Concatenate two or more video files

Source

Given a video split in two files, such as file1.mp4 and file2.mp4, the following command concatenates the two without re-encoding them. The result is a single video file output.mp4.

Step 1

Create files.txt in the same directory as the video files and add the following to it:

file './file1.mp4'
file './file2.mp4'

Step 2

Run the following command:

ffmpeg -f concat -safe 0 -i files.txt -c copy output.mp4

Extract part of a video file

Source

When digitizing a VHS, I usually end up with a few initial seconds of useless noise and a few minutes of noise at the end. To trim this, I use the following commands (assuming my initial file is named input.mp4):

To cut video from [start] for [duration]:

ffmpeg -ss [start] -i input.mp4 -t [duration] -map 0 -c copy output.mp4

with [start] and [duration] formatted as 00:01:23.000 or 83 (seconds).

To cut the video from [start] to [end]:

ffmpeg -copyts -ss [start] -i input.mp4 -to [end] -map 0 -c copy output.mp4

Replace audio in a video file

Source

Given a video file v.mp4 and an audio file a.mp3, which have the same duration, the following command outputs a new file new.mp4 with the original audio in v.mp4 replaced by a.mp3.

ffmpeg -i v.mp4 -i a.mp3 -c:v copy -map 0:v:0 -map 1:a:0 new.mp4

Extract audio from video file

Source

Given a video file v.webm, the following command outputs a new file a.opus which contains the audio from v.webm:

ffmpeg -i v.webm -map 0:a -c copy a.opus

Note that the file extensions must be changed to the ones relevant for the specific file. .webm and .opus are relevant for files downloaded by youtube-dl.

Split audio file into chunks

Source

Given an audio file input.opus, the following command outputs N audio files, each of which is 300 seconds long.

ffmpeg -i input.opus -f segment -segment_time 300 -c copy out%03d.opus

The .opus extension can be replaced with other audio file formates.

© 2024