Konrad Reiche About Photos Talks

Editing your video on the command-line

There are good video editors for Linux but every time I find myself in need of editing a video I end up spending more time learning how to use the program than benefiting from it.

Behind the scenes they use ffmpeg. Learning how to use ffmpeg is a much more transferable skill, for example if you want to write a service which encodes GIFs to videos or a service to crop images.

At our most recent hackathon I ended up editing the whole presentation video on the command-line using ffmpeg. These are some of the commands I used:

Trimming the beginning

A recording will often contain parts in the beginning that need to be removed:

# Remove the first three seconds
ffmpeg -i input.mp4 -ss 3 -c copy output.mp4

Trimming the end

The same is true for the end of the material:

# Remove everything after 3 minutes and 42 seconds
ffmpeg -i input.mp4 -t 00:03:42 -c copy output.mp4

Both, -ss and -t, can also be combined into one command.

Background Music

For a background music track the volume had to be lowered:

# Reduce volume by 50%
ffmpeg -i input.mp3 -filter:a "volume=0.5" output.mp3

There were also multiple audio tracks which had to be combined into one:

# Concatenate multiple mp3 files into one
ffmpeg -i "concat:input1.mp3|input2.mp3|input3.mp3" -c copy output.mp3

Several attempts to merge the background audio track failed due to the length of the audio track not matching the length of the video. This can be solved by generating a silent audio track and concatenating it to the end of the audio track:

# Generate 30 seconds of silence
ffmpeg -f lavfi -i anullsrc=channel_layout=5.1:sample_rate=48000 -t 30 output.mp3

Finally, to merge the audio track into the video track:

# Merge video with existing audio track and another audio track
ffmpeg -i input.mp4 -i input.mp3 -filter_complex "[0:a][1:a]amerge=inputs=2[a]" -map 0:v -map "[a]" -c:v copy -ac 2 output.mp4

Scaling a video

One of the video files could not be concatenated with the existing video file because its resolution was smaller than the recorded material:

# Scale video resolution to 1920x1080
ffmpeg -i input.mp4 -vf scale=1920:1080 output.mp4

Concatenate videos

Concatenating videos is not always as easy as concatenating audio tracks. One way is to use the concat video filter:

# Concatenate two video files
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v] [0:a] [1:v] [1:a] concat=n=2:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output.mp4

These commands were sufficient to do all of the video editing needed. There are a lot of approaches to most of these commands and depending on the existing encodings or formats more complex parameters might be necessary.

To me this was greatly useful to have one more task where I can stay on the terminal (if you exclude all the research I had to do to find the commands).