Sample code for converting video using ffmpeg command line

Sample code for converting video using ffmpeg command line

Before starting the main text of this article, you must first install the ffmpeg program (x264 encoding must also be installed under Linux). Install directly with brew on Mac:

brew install ffmpeg --with-faac --with-fdk-aac --with-ffplay --with-fontconfig --with-freetype --with-libass --with-libbluray --with-libcaca --with-libsoxr --with-libquvi --with-frei0r --with-libvidstab --with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-openssl --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theroa --with-tools --with-x265

After installation, you can use the ffmpeg command to compress MP4 files:

ffmpeg -i MVI_7274.MOV -vcodec libx264 -preset fast -crf 20 -y -vf "scale=1920:-1" -acodec libmp3lame -ab 128k a.mp4

The parameters are explained as follows (modified from here: https://vistb.net/2012/02/x264-video-compress-intro/):

-preset: specifies the encoding configuration. The x264 encoding algorithm has many configurable parameters. Different parameter values ​​can result in very different encoding speeds and may even affect the quality. To save users the trouble of understanding the algorithm and then manually configuring parameters. x264 provides some preset values, which can be specified by preset. The presets are: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, and placebo. Ultrafast has the fastest encoding speed, but has a low compression rate and generates larger files, while Placebo is just the opposite. The default value used by x264 is medium. It should be noted that preset mainly affects the encoding speed and does not greatly affect the quality of the encoded results. When compressing HD movies, I usually use slow or slower. If your machine is very good, you can use veryslow, but it usually doesn't bring much benefit.

-crf: This is the most important option, used to specify the quality of the output video. The value range is 0-51, and the default value is 23. The smaller the number, the higher the quality of the output video. This option will directly affect the bit rate of the output video. Generally speaking, I would use around 20 for compressing 480p, 16-18 for compressing 720p, and I haven't tried 1080p. Personally, I think it is not necessary to go below 16 under normal circumstances. The best way is to try several values, each for a few minutes, to see the final output quality and file size, and then choose according to your needs.

In fact, there is also a parameter like -b 1024k, but I found that -b is useless after -crf is set. According to my own simple attempt, the relationship between the crf and the compressed file size of an 18-second 1920x1080 video shot by 5D2 (natural light in the afternoon, simple image, large white walls, only one black door) is as follows:

crf File size
16 54M
18 39M
20 25M
twenty two 17M
twenty four 11M
26 7.3M
28 5.0M
30 3.6M
32 2.7M
default 14M (crf is 23)

I compared the video quality when the crf was 20, 28, and 32, and found that the quality still declined at 32. 20 is indeed very fine, but the difference between 28 and 20 is not that big. It is better to set the crf value between 26-28. If you have requirements for the size, you can leave it alone and use the default value (probably 31).

In addition, regarding preset, slow and fast are only related to the running time. The running time of slow is much longer than that of fast. The mp4 file produced by slow will be smaller (12M), and the file produced by fast will be larger (14M), but the difference in video quality is not obvious.

If the original video size is reduced from 1920x1080 to 960x540, the video size becomes:

crf File size
16 11M
18 6.7M
20 4.4M
twenty two 3.0M
twenty four 2.1M
26 1.6M
28 1.3M
30 1.1M
32 893K
default 2.5M (crf is 23)

To sum up, when you have higher requirements on quality, choose 22 or below; when you have very high requirements on size, choose 26 (but the quality will indeed be slightly worse), otherwise 24 is more cost-effective (or the default 23 is also fine), and if you have very, very high requirements on size, then choose 28 or above.

Postscript (from @gghyoo)

You can use -threads n to implement multi-threaded operations and make full use of multi-core CPUs

Here are some examples:

ffmpeg -threads 2 -crf 20 -y -i ML-02.avi -strict experimental ML-02.mp4

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Code for getting flv video thumbnail and video time using Ffmpeg
  • Detailed explanation of the process of building a streaming media server using nginx and ffmpeg
  • Detailed tutorial on compiling FFmpeg decoding library for Android Studio under Mac OS
  • Detailed description of ffmpeg Chinese parameters
  • ffmpeg Chinese parameter description and usage examples
  • Graphic tutorial on installing FFmpeg on MAC

<<:  Detailed explanation of how CocosCreator system events are generated and triggered

>>:  Reasons and solutions for being unable to remotely connect to MySQL database under CentOS7

Recommend

A detailed introduction to seata docker high availability deployment

Version 1.4.2 Official Documentation dockerhub st...

12 Laws of Web Design for Clean Code [Graphic]

Beautiful code is the foundation of a beautiful we...

The homepage design best reflects the level of the web designer

In the many projects I have worked on, there is b...

Detailed explanation of Vue mixin

Table of contents Local Mixin Global Mixins Summa...

MySQL master-slave principle and configuration details

MySQL master-slave configuration and principle, f...

CSS3 achieves various border effects

Translucent border Result: Implementation code: &...

Vue+node realizes audio recording and playback function

Result: The main part is to implement the code lo...

How to Run a Command at a Specific Time in Linux

The other day I was using rsync to transfer a lar...

How to solve jQuery conflict problem

In front-end development, $ is a function in jQue...

Detailed explanation of how to solve the problem of too long content in CSS

When we write CSS, we sometimes forget about the ...

Analysis of the principles of docker containers

Table of contents 01 What is the essence of a con...

Docker exec executes multiple commands

The docker exec command can execute commands in a...

React new version life cycle hook function and usage detailed explanation

Compared with the old life cycle Three hooks are ...