In my ever-growing collection of smart FFmpeg tricks, here is a way of converting from one container format to another. Here I will convert from a QuickTime (.mov) file to a standard MPEG-4 (.mp4), but the recipe should work between other formats too.
If you came here to just see the solution, here you go:
ffmpeg -i infile.mov -acodec copy -vcodec copy outfile.mp4
In the following I will explain everything in a little more detail.
One of the confusing things about video files is that they have both a container and a compression format. The container is often what denotes the file suffix. Apple introduced the .mov format for QuickTime files and Microsoft used to use .avi files.
Nowadays, there seems to a converge towards using MPEG containers and .mp4 files. However, both Apple and Microsoft software (and others) still output other formats. This is confusing and can also lead to various playback issues. For example, many web browsers are not able to play these formats natively.
The compression format denotes how the video data is organized on the inside of a container. Also, here there are many different formats. The most common today is to use the H.264 format for video and AAC for audio. These are both parts of the MPEG-4 standard and can be embedded in .mp4 containers. However, both H.264 and AAC can also be embedded in other containers, such as .mov and .avi files.
The important thing to notice is that both .mov and .avi files may contain H.264 video and AAC audio. In those cases, the inside of such files is identical to the content of a .mp4 file. But since the container is different, it may still be unplayable in certain software. That is why I would like to convert from one container format to another. In practice that means converting from .mov or .avi to .mp4 files.
There are many ways of converting video files. In most cases, you would end up with a lossy conversion. That means that the video content will be altered. The file size may be smaller, but the quality may also be worse. The general rule is that you want to compress a file as few times as possible.
For all sorts of video conversion/compression jobs, I have ended up turning to FFmpeg. If you haven’t tried it already, FFmpeg is a collection of tools for doing all sorts of audio/video manipulations in the terminal. Working in the terminal may be intimidating at first, but you will never look back once you get the hang of it.
Converting a file from .mov to .mp4 is as simple as typing this little command in a terminal:
ffmpeg -i infile.mov outfile.mp4
This will change from a .mov container to a .mp4 container, which is what we want. But it will also (probably) re-compress the video. That is why it is always smart to look at the content of your original file before converting it. You can do this by typing:
ffmpeg -i infile.mov
For my example file, this returns the following metadata:
Metadata: major_brand : qt minor_version : 0 compatible_brands: qt creation_time : 2016-08-10T10:47:30.000000Z com.apple.quicktime.make: Apple com.apple.quicktime.model: MacBookPro11,1 com.apple.quicktime.software: Mac OS X 10.11.6 (15G31) com.apple.quicktime.creationdate: 2016-08-10T12:45:43+0200 Duration: 00:00:12.76, start: 0.000000, bitrate: 5780 kb/s Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1844x1160 [SAR 1:1 DAR 461:290], 5243 kb/s, 58.66 fps, 60 tbr, 6k tbn, 50 tbc (default) Metadata: creation_time : 2016-08-10T10:47:30.000000Z handler_name : Core Media Video encoder : H.264 Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 269 kb/s (default) Metadata: creation_time : 2016-08-10T10:47:30.000000Z handler_name : Core Media Audio
There is quite a lot of information there, so we need to look for the important stuff. The first line we want to look for is the one with information about the video content:
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1844x1160 [SAR 1:1 DAR 461:290], 5243 kb/s, 58.66 fps, 60 tbr, 6k
Here we can see that this .mov file contains a video that is already compressed with H.264. Another thing we can see here is that it is using a weird pixel format (1844×1160). The bit rate of the file is 5243 kb/s, which tells something about how large the file will be in the end. And it is also interesting to see that it is using a framerate of 58.66 fps, which is also a bit odd.
Similarly, we can look at the content of the audio stream of the file:
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 269 kb/s (default)
Here we can see that the audio is already compressed with AAC at a standard sampling rate of 44.1 kHz and at a more nonstandard bit rate of 269 kb/s.
The main point of investigating the file before we do the conversion is to avoid re-compressing the content of the file. After all, the content is already in the right formats (H.264 and AAC) even though it is in an unwanted container (.mov).
Today’s little trick is how to convert from one format to another without modifying the content of the file, only the container. That can be achieved with the code shown on top:
ffmpeg -i original.mov -acodec copy -vcodec copy outfile.mp4
There are several benefits of doing it this way:
- Quality. Avoiding an unnecessary re-compression of the content, which would only degrade the content.
- Preserve the pixel size, sampling rates, etc. of the originals. Most video software will use standard settings for these. I often work with various types of non-standard video files, so it is nice to preserve this information.
- Save time. Since no re-compression is needed, we only copy content from one container to another. This is much, much faster than re-compressing the content.
All in all, this long explanation of a short command may help to improve your workflows and save some time.