当前位置:网站首页>Video file too large? Use ffmpeg to compress it losslessly

Video file too large? Use ffmpeg to compress it losslessly

2022-06-27 09:08:00 Denglong

If we often take videos , Will encounter a dilemma : The video file is too large , Storage takes up a lot of space . If you directly use the video compression tool to compress the bit rate , And worry about losing the image quality of the video , What should I do ?

This is the time , We can use ffmpeg Of hevc Coding function to achieve lossless compression !

What is? FFmpeg?

FFmpeg It is a famous open source image and video processing program in the industry . It is widely used , In many video editing software 、 In image processing software , They all use FFmpeg, And for example. OBS This kind of broadcasting guide software also uses FFmpeg

What is HEVC code ?

We often use MP4 In the format , In general, we use H.264 Coding format , It's on 2003 It was officially released and widely used in various occasions .

Ten years later , That is to say 2013 year ,HEVC Code officially released .HEVC yes High Efficiency Video Coding( Efficient video coding ) Abbreviation , Also known as H.265. Compare with H.264, It has a higher compression ratio . In the same picture quality ,HEVC The encoding format can save 25%~50% Of storage space . therefore , For every few GB Video file , Use HEVC Code for archiving and storage , It could not be more suitable .

HEVC What are the disadvantages of the encoding format ?

Compare with H.264,HEVC The encoding format is in the process of encoding and decoding , Need to occupy more hardware resources . In plain English , More slowly . therefore , If your computer doesn't have a good graphics card to support HEVC Hardware acceleration of format , It will compress your video very slowly , Jamming may also occur when playing .

also , because HEVC The popularity of the format is not as good as it has been 20 Year history H.264, therefore , Some playback software cannot play it . But in the near future , This problem is no longer a problem .

Let's get started !

install FFmpeg

First , We turn on FFmpeg Its official website https://ffmpeg.org  And then click “Download” Button , Go to download page , Download the corresponding version . And then install it on your computer .Windows You also need to add the installation directory to the environment variable .

Use FFmpeg

FFmpeg Is a program with only a command line interface , So you need to run it in the console of the computer .

Windows tip

Windows Next , By keyboard win+R, Then input cmd, You can open the command line interface

see FFmpeg Help interface

Let's enter the following command at the console :

ffmpeg -h

then ffmpeg Will output its help information . Because the information is in English , So it takes a little patience to read it . Here are two help messages associated with our goals :

This is information about encoder and decoder , Then we can enter the following code , Check the options of encoder and decoder respectively :

ffmpeg -decoders
ffmpeg -encoders
ffmpeg -codecs

We will find that a lot of information is output , Let's be dazzled . Yes ,FFmpeg It's so powerful , Support many codecs .

Start compressing !

We use FFmpeg Of libx265 Library , Just a few simple steps :

For safety's sake , Let's copy the video to be compressed to a new folder , Prevent misoperation and loss of data . Then open the console in the new folder (windows You need to use cd Command to enter the corresponding folder ).

next , Let's create a new folder under the folder , be called output, The video we output will be stored in this folder .

This should look like this under the folder :

next , Let's study how to use ffmpeg Compressed video .

A simple example

We can use the following command format to output a No visual damage In the video :

ffmpeg -i  Input file name  -c:v libx265 -x265-params crf=18 output/ Output file name .mp4

For the above file , The command is as follows :

ffmpeg -i demo.mp4 -c:v libx265 -x265-params crf=18 output/demo_1.mp4

then , After a period of waiting , We can see the results !

On the left is the source video , On the right is the compressed video . We can see , It saves 29.6% Space ! and , After my test , On the vision , The picture quality of these two videos is similar .

More advanced compression

ad locum , Let's explain the meaning of the above parameter settings .crf yes Constant Rate Factor Abbreviation , The less it's worth , The higher the quality of the picture , The more space it takes . Its options are 0~51, The default is 28. When crf stay 20 Now , Can achieve visually lossless .

Besides ,FFmpeg It also provides us with some preset options , We can go through preset Options , Quick setup x265 The parameters used in coding .

preset The options of are from fast to slow according to the encoding speed , Arranged as follows :

  • ultrafast
  • superfast
  • veryfast
  • faster
  • fast
  • medium
  • slow
  • slower
  • veryslow
  • placebo

The slower the encoding speed , The better the compression effect and image quality .preset The default parameter for the option is medium.

Output visual lossless video with the highest compression effect

ffmpeg -i  Input file name  -c:v libx265 -x265-params crf=18:preset=placebo output/ Output file name .mp4

For the video above , We can use the following compression command :

ffmpeg -i demo.mp4 -c:v libx265 -x265-params crf=18:preset=placebo output/demo_2.mp4

The size comparison before and after compression is as follows :

Although this time the volume is only reduced 13.1%, But when you look at the video with the naked eye , Obviously, the video quality is no different from the original video .

So let's do that ,1GB In the video , Lossless compression with the highest image quality , Can save 131MB What about the space ! Still considerable !

Batch compressed video

Because the above method can only compress video one by one , For this reason, I wrote a simple python Script to complete the function of automatically compressing all videos in the folder .

Install... On your computer python and ffmpeg after , You can run this script . then , Create... In the folder where the script is located input Folders and output Folder . And put the video to be compressed in input Under the folder . function python After the script , Will start compressing the video , The output file is placed in output Under the folder .

I have uploaded the script to GitHub:Utility-gadgets/cv/batch_transcode2HEVC at main · fslongjin/Utility-gadgets · GitHubhttps://github.com/fslongjin/Utility-gadgets/tree/main/cv/batch_transcode2HEVC

Conclusion

FFmpeg It's very powerful , Far more than video compression . Its functions are yet to be explored , Interested students , Can be in FFmpeg View official documents and tutorials on the official website , Learn more ~

Please quote source for reprint : The video file is too large ? Use FFmpeg To compress it losslessly ! | | Longjin's blog https://longjin666.cn/?p=1443 Welcome to my official account. : Denglong

Let's explore more interesting things together !

原网站

版权声明
本文为[Denglong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270855015776.html