Videos recorded on smartphones are typically 1080p or higher. In practical scenarios, such as sharing with friends, processing them can be more convenient. Below are several common methods for handling videos using ffmpeg.
(If you only need format conversion, refer to the steps below.)
Converting Videos Using H.264 Encoding
H.264 (also known as MPEG-4 AVC) is currently a popular video encoding format. Compared to MPEG-2, it can compress video size by 50% to 75% while maintaining similar image quality. For example, if an MPEG-2 (such as DVD) video is 1GB in size, H.264 encoding can reduce it to around 250MB. Additionally, H.264 encoded videos can be played directly in browsers (like Chrome) and mobile devices (like iPhones and Android phones).
If you have a collection of home videos (older home DVs are generally in MPEG-2 format) that you want to burn to discs for preservation, or videos in formats not directly supported by your phone that you want to play on your device, converting and compressing them with H.264 encoding is a good choice.
mencoder is a very convenient video encoding program that supports almost all video formats, offers rich parameters, and is fast.
First, you need to install the mencoder program (installation commands for Archlinux, Fedora, and Ubuntu are provided below):
$ sudo pacman -S memcoder
$ sudo yum install mencoder
$ sudo apt-get install mencoder
Then, you can check which video and audio encoders and container formats your current system supports:
$ mencoder -ovc help
$ mencoder -oac help
$ mencoder -of help
If you see x264 video encoder, mp3lame audio encoder, and mp4 container format listed, you can proceed with the encoding conversion. Otherwise, you may need to install the corresponding audio and video encoders; usually, installing the ffmpeg package includes these encoders.
Compressing an MPEG-2 video:
$ mencoder m001.mpg -o m001.mp4 -oac mp3lame -ovc x264 -of lavf -vf lavcdeint
In the command above, m001.mpg and m001.mp4 are the input and output filenames, respectively. -oac specifies the audio encoder, -ovc specifies the video encoder, and -of specifies the output file container format. lavf indicates that the container format is determined by the output filename extension (e.g., m001.mp4 uses MP4 container, m001.avi uses AVI container). Finally, the -vf lavcdeint parameter is used to remove combing artifacts (interlacing lines) from the video; if none are present, this parameter can be omitted.
The H.264 encoding process can be time-consuming. For example, on an AMD quad-core 2.8GHz processor, the encoding speed is approximately 30fps, which is roughly the same as the normal playback speed of the video.
If there are many video files to encode and convert, it is best to write a batch processing script:
#!/bin/bash
find . -type f ( -name "*.mpg" -o -name "*.mpeg" )|while read line;do
echo $line
mencoder $line -o ${line}.mp4 -oac mp3lame -ovc x264 -of lavf -vf lavcdeint
done
Executing the above script will encode all videos with extensions “mpg” and “mpeg” in the current directory into H.264 format.
Finally, besides mencoder, you can also use ffmpeg for encoding.
Resizing Videos
Assume the original video resolution is 1080p (i.e., 1920×1080 px, 16:9 aspect ratio). The following command can resize it to 480p:
$ ffmpeg -i a.mov -vf scale=853:480 -acodec aac -vcodec h264 out.mp4
Meaning of each parameter:
- -i a.mov specifies the filename of the video to be processed.
- -vf scale=853:480 The vf parameter specifies the video filter, where scale indicates resizing. The numbers represent resizing to 853×480 px. The width 853px is calculated based on the original aspect ratio. Since the original video has a 16:9 aspect ratio, to achieve a height of 480px for the target video, the width = 480 x 9 / 16 = 853.
- -acodec aac specifies AAC encoding for audio. Note: As of the writing of this article, ffmpeg’s built-in AAC encoder is still experimental, so you may need to add the parameter “-strict -2” to proceed. You can simply add it. Alternatively, you can use the external libfaac (requires recompiling ffmpeg).
- -vcodec h264 specifies H.264 encoding for video. Note: Currently, most mobile phone video recordings (container format, file format) are mov or mp4, both of which use AAC for audio and H.264 for video.
- out.mp4 specifies the output filename.
In the scale=853:480 parameter, in actual application scenarios, you usually only need to specify one dimension, such as height 480 or 720. The width can be set to “-1” to indicate automatic calculation based on the original video’s aspect ratio. Thus, the parameter can be written as: scale=-1:480, or scale=480:-1.
Cropping Videos
Sometimes you may only need the center part of the video and do not need the top and bottom content. In this case, you can crop the video (crop). For example, if you have a vertical video of 1080 x 1920 and want to keep only the central 1080×1080 portion, you can use the following command:
$ ffmpeg -i a.mov -strict -2 -vf crop=1080:1080:0:420 out.mp4
The crop=1080:1080:0:420 is the cropping parameter. Its specific meaning is crop=width:height:x:y, where width and height represent the dimensions after cropping, and x:y represents the coordinates of the top-left corner of the cropping area. In this example, since we only want to keep the middle part of the vertical video, x does not need offset (so 0 is passed), while y needs to be offset downwards: (1920 – 1080) / 2 = 420.
Video resizing and cropping can be performed simultaneously. The following command resizes the video to 853×480 and then crops to keep the horizontal center part:
$ ffmpeg -i IMG_4940.MOV -strict -2 -vf scale=853:480,crop=480:480:186:0 out.mp4
Clipping Videos
If you have a long video and only need to keep a segment of it, you can use the following command to clip the video.
$ ffmpeg -i a.mov -ss 00:00:21 -t 00:00:10 -acodec aac -vcodec h264 -strict -2 out.mp4
Here, -ss 00:00:21 indicates the start position (time point) for clipping, and -t 00:00:10 indicates the duration of the clip, i.e., 10 seconds.
Of course, a single command can perform clipping, resizing, and cropping simultaneously by combining the relevant parameters.