The most useful FFmpeg commands for audio and video conversion

Andrei Maksimov

Andrei Maksimov

0
(0)

FFmpeg is a free and open-source software project consisting of a large suite of libraries and programs for handling video, audio, and other multimedia files and streams. It is a cross-platform set of tools that work on Mac (OSX), Linux, and Windows. This article covers FFmpeg installation and most commonly used commands examples for getting video information, cutting and splitting video files, converting videos to various formats including images, managing audio, subtitles and so much more.

Installing FFmpeg multimedia framework

The installations of FFmpeg is easy and simple process for most common operating systems.

Ubuntu

To instal FFmpeg on the latest Ubuntu Linux, use the following commands:

sudo apt-get update
sudo apt install ffmpeg -y
1. The most useful FFmpeg commands for audio and video conversion - Install FFMpeg - Ubuntu

OSX

If you’d like to install FFmpeg on your Mac, use Homebrew package manager:

brew update && brew upgrade ffmpeg
2. The most useful FFmpeg commands for audio and video conversion - Install FFMpeg - OSX

Windows

To install FFmpeg on Windows, use Chocolatey:

choco install ffmpeg
3. The most useful FFmpeg commands for audio and video conversion - Install FFMpeg - Windows

The most useful FFmpeg commands

Now, as soon as you’ve installed FFmpeg, we can jump to its most commonly used command examples.

For the purpose of this article we’ll use free sample video files.

Get video file information

To obtain detailed information about the video file, run the following command:

ffmpeg -i sample-mp4-file.mp4 -hide_banner
4. The most useful FFmpeg commands for audio and video conversion - Get video file information

The -hide_banner argument hides copyrights information.

Cut video into small clips

FFmpeg has a capability that allows you to trim video into short chunks.

We’re going to use the following arguments:

  • -ss parameter specifies the start time stamp in HH:MM:SS.ms format
  • -t argument specifies the final clip’s duration (time) in seconds
  • -codec copy – parameter would copy all streams without re-encoding
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
    -ss 00:01:00.0 \
    -codec copy \
    -t 20 \
    OutputVideo.mp4
5. The most useful FFmpeg commands for audio and video conversion - Cut video into small clips

Split video file into multiple parts

Use the command shown below to divide a video into two or more parts.

In our example we’re splitting the file into two parts:

  • The first part is 30 secs long from the beginning of the file (-t argument)
  • The second part will contain the video data from the 50th second till the end of the file (-ss argument)
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
    -t 00:00:30 \
    -c copy OutputVideo-1.mp4 \
    -ss 00:00:50 \
    -codec copy \
    OutputVideo-2.mp4
7. The most useful FFmpeg commands for audio and video conversion - Splitting a video into multiple parts

Mute audio

Use the following command to disable output video file audio:

ffmpeg -i sample-mp4-file.mp4 -hide_banner \
    -an OutputVideo.mp4
8. The most useful FFmpeg commands for audio and video conversion - Mute audio

Extract audio from video

You can extract the audio from the Video by executing the command:

ffmpeg -i sample-mp4-file.mp4 -hide_banner \
    -vn \
    -ac 2 \
    -ab 187 \
    -f mp3 \
    Audio.mp3

Here we’re using the following options:

  • -vn disables video recording to the output file
  • -ac sets the number of audio channels
  • -ab sets audio bitrate to 187kb/s
  • -f sets an output format to mp3
10. The most useful FFmpeg commands for audio and video conversion - Extract audio from video

Merge audio and video

Use the following command to add audio track to video file:

ffmpeg -i Audio.mp3 -i sample-mp4-file.mp4 \
    -hide_banner \
    OutputVideo.mp4
11. The most useful FFmpeg commands for audio and video conversion - Merge audio and video

Convert video to images

Use the below-stated command to convert Video into images:

ffmpeg -i sample-mp4-file.mp4 -hide_banner \
    image%d.jpg
9. The most useful FFmpeg commands for audio and video conversion - Convert video to images

Convert images to video

All images can be converted the video:

ffmpeg -f image2 -i image%d.jpg \
    -hide_banner \
    OutputVideo.mp4
12. The most useful FFmpeg commands for audio and video conversion - Convert images to video

Try to add audio to the final clip yourself.

Converting MP4 to FLV

Run the command demonstrated below to convert MP4 format video file to FLV format:

ffmpeg -i sample-mp4-file.mp4 -hide_banner \
    -vn \
    -ac 2 \
    -ab 187 \
    -f flv \
    OutputVideo.flv
13. The most useful FFmpeg commands for audio and video conversion - Converting MP4 to FLV

Convert FLV to MPG

Run the command shown below to convert FLV format video file to MPG format:

ffmpeg -i OutputVideo.flv -hide_banner \
    OutputVideo.mpg
14. The most useful FFmpeg commands for audio and video conversion - Convert FLV to MPG

Convert MPG into FLV

Copy the command demonstrated below to convert MPG format video file to FLV:

ffmpeg -i OutputVideo.mpg -hide_banner \
    -ab 26k \
    -f flv \
    OutputVideo.flv
16. The most useful FFmpeg commands for audio and video conversion - Convert MPG into FLV

Convert video into an animated GIF

To convert a video file into an animated gif file, use the command:

ffmpeg -i sample-mp4-file.mp4 -hide_banner \
    Output.gif
15. The most useful FFmpeg commands for audio and video conversion - Convert video into an animated GIF

Convert to CD/DVD format

Use -target type to create video CD or DVD formatted output file.

Target type supports vcd, svcd, dvd, dv and dv50 formats that may be prefixed with pal-, ntsc- or film- to use the corresponding standard:

ffmpeg -i sample-mp4-file.mp4 -hide_banner \
    -target film-vcd \
    vcd_video.mpg
17. The most useful FFmpeg commands for audio and video conversion - Convert to CD and DVD format

Insert subtitles

Let’s create a demo subtitles file (more information about subtitles):

cat << EOF > subtitles.srt
1
00:00:00,000 --> 00:00:20,000
This is demo subtitle 1

2
00:00:20,000 --> 00:00:40,000
This is demo subtitle 2
EOF

You can add subtitle file to video file by adding a separate subtitle file to the command:

ffmpeg -i sample-mp4-file.mp4 -hide_banner \
    -i subtitles.srt \
    -c copy \
    -c:s mov_text \
    OutputVideo.mp4

If you’d like to show subtitles in the video:

ffmpeg -i sample-mp4-file.mp4 -hide_banner \
    -vf subtitles=subtitles.srt \
    OutputVideo.mp4
18. The most useful FFmpeg commands for audio and video conversion - Insert subtitles

Extract subtitles

To extract subtitles from the video file, check which stream you should use first:

ffmpeg -i OutputVideo.mp4
19. The most useful FFmpeg commands for audio and video conversion - Get subtitle stream information

The output indicates, that subtitles are available in file at stream #0:2.

Now, we can extract them using the following command:

ffmpeg -i OutputVideo.mp4 -hide_banner \
    -map 0:2 subtitles.srt
20. The most useful FFmpeg commands for audio and video conversion - Extract subtitles

Where -map 0:2 specifies the required stream.

Rotate video

And finally, here’s a command to rotate the video clip to the 90 degrease clockwise (use transpose=2 to rotate to anti clockwise direction):

ffmpeg -i sample-mp4-file.mp4 -hide_banner \
    -filter:v 'transpose=1' \
    OutputVideo.mp4
21. The most useful FFmpeg commands for audio and video conversion - Rotate video

Summary

FFmpeg is a versatile multimedia framework that allows us to make various different operations with video files.

You can use FFmpeg to convert video and audio files into other formats, do image and audio extraction, rotating videos, adding subtitles, splitting video clips into multiple parts, and many more. In this guide, we illustrated how to apply the most commonly used commands to get the results you may need.

How useful was this post?

Click on a star to rate it!

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Top rated Udemy Courses to improve you career

Subscribe to our updates

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Want to be an author of another post?

We’re looking for skilled technical authors for our blog!

Leave a comment

If you’d like to ask a question about the code or piece of configuration, feel free to use https://codeshare.io/ or a similar tool as Facebook comments are breaking code formatting.