FFMPEG An Intermediate Guide/formats

Ever wondered how an MP3 file can tell you what the name of the song is and who made it? Well, there's a reason for that, and it has to do with how MPEG designed the format. It can handle metadata, called ID3 tags that MP3 players would read from the format, all while playing the music from a codec. Formats are also commonly called file formats, container formats, or containers. Some examples for video include (ASF),  (AVI), and  (.mp4),  (.ogg), and  (.mkv).

To understand how codecs are used in formats, you can look to commercial DVDs and Blu-Ray Discs produced and sold in countries where multiple languages are spoken by many people. The back of the case usually explains that the disc has two different audio tracks; one in the primary language of the country, and the other in a second language or the original language if the content is dubbed. Alternatively, some major studios publish movies with mastered audio for 5.1 and Stereo speaker setups, or they may provide an or visually impaired consumers. Whatever the case, most DVD players have the ability to change between these tracks on-the-fly, and if not automatically, the studio will usually provide a way for people to navigate to an options menu in the DVD and pick it before playing the movie. The same goes for subtitles.

With the right software, these tracks can be completely isolated from each other in a process known as demuxing, and it shouldn't be much of a surprise that FFmpeg is one of those programs. It can do both demuxing and decoding as one of the capabilities of a multimedia tool. Consult the general documentation for the list of codecs supported by FFmpeg.

Formats
FFmpeg uses a library called  to interact with formats. Some formats have default codecs and settings that you don't have to set yourself. This is why  works without any specific flags.

Codecs
FFmpeg uses a library called  to interact with codecs. Much like formats, some codecs have default settings that you don't have to set yourself.

Stream copying
It's possible to transfer codecs between formats without having to reencode streams.

ffmpeg -i input -acodec copy -vcodec copy -o output

While this does allow subtitles to be added on top of existing video codecs, it also means you sacrifice support from media players. Depending on the format you're switching to, you may need to reencode.

You can also cut out and convert a part from a multimedia file. This can be useful for extracting and compressing a track from a raw (PCM) audio CD rip file which contains multiple songs.

ffmpeg -c:a pcm_s16le -ss 1615.3 -t 279 -i RawAudio-example.bin Track-6.ogg

In this example, an audio track is extracted by specifying the codec ( PCM 16-bit little-endian), the start time within the source file , the length of the excerpt to extract which contains the track  , the source file name  , and an example output file name.

Batch conversion
While  lacks a native batch conversion feature, a for loop command can be used. For example, the following command converts all wavesound files in the working directory to the OGG format:

for i in *.wav; do ffmpeg -i "$i" "${i%.*}.ogg"; done

The file extensions are automatically detected by the FFMPEG tool. Note that the command is case-sensitive.