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 Advanced Systems Format (ASF), Audio Video Interleave (AVI), and MPEG-4 Part 14 (.mp4), Ogg (.ogg), and Matroska (.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 audio description 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
[edit | edit source]FFmpeg uses a library called libavformat
to interact with formats. Some formats have default codecs and settings that you don't have to set yourself. This is why ffmpeg -i input -o output
works without any specific flags.
Codecs
[edit | edit source]FFmpeg uses a library called libavcodec
to interact with codecs. Much like formats, some codecs have default settings that you don't have to set yourself.
Stream copying
[edit | edit source]It's possible to transfer codecs between formats without having to reencode streams.[1]
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 (-c:a pcm_s16le
PCM 16-bit little-endian), the start time within the source file -ss 1615.3
, the length of the excerpt to extract which contains the track -t 279
, the source file name -i RawAudio-example.bin
, and an example output file name Track-6.ogg
.
Batch conversion
[edit | edit source]While ffmpeg
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.
References
[edit | edit source]- ↑ Stream Copy in FFmpeg's documentation.