Linux Linux shell scripts and ffmpeg builds for you to compress mp4 video file according to your needs.

rupeshforu3

Disciple
Hi I am Rupesh from India and I have some mp4 video files which are downloaded from YouTube and captured from android smartphone camera. I have compressed these files using ffmpeg script and I am providing to you which may help you to do the same.

Generally these files are of large size specifically captured from android smartphone camera. So I tried a lot to compress these files using ffmpeg command and some guis like handbrake, X media recode etc.,. But all of these have their own pros and cons. I have developed a small shell script to compress these mp4 videos using ffmpeg command and x265 video codec, fdkaac. This script can be modified according to your needs and run.

Simply place the script in the folder which you have mp4 videos you want to compress and after that give execution permission and after that execute it in terminal emulator.

The system requirements are working Linux PC, ffmpeg built with libfdk\_aac and libx265 and gpl, opencl, opengl support.

Many of you may think as there are many ffmpeg guis and scripts available why I must use the current one. But all of the guis can't run according to your needs for example I have used one ffmpeg gui where it tries to encode 3 to 4 videos at a time but if there is any power failure again you must start the process from the beginning. There are a number of scripts available for ffmpeg but they may be difficult to understand to you. The script I am providing is easy to understand and use. It uses all the system resources to compress videos one by one with optimal quality.

I have read a lot of documents and tutorials related to x265 including

[https://x265.readthedocs.io/en/2.5/cli.html](https://x265.readthedocs.io/en/2.5/cli.html)

[https://slhck.info/video/2017/03/01...lhck.info/video/2017/03/01/rate-control.html)


[https://ffmpeg.org/ffmpeg.html](https://ffmpeg.org/ffmpeg.html)

In these scripts I have used ffmpeg tool which is used in many projects and even in YouTube. After that I have used x265 which is the latest video standard developed which uses less memory for quality videos. After that I have used fdkaac which is the best audio codec developed which uses less memory for quality audio.

I am providing three scripts one which uses 2 pass encoding and another which uses CRF encoding and another which doesn't use any of the above two.

In the above scripts the first one uses two pass encoding to compress video file with maximum quality but takes lot of time and produces lowest possible memory size. The second script that used CRF takes lot of memory space with maximum quality and takes less time. The third script takes less time and less memory space but the quality is unknown.

I am finally suggesting you to modify the cpucount option to your processor core count -1. If you don't do this cpu usage may be upto 99 percent which will generate much heat and destroy the PC.

I have created these scripts according to my needs like I have not included option for intel quick sync as I don't have such new processor.So if you want create your own version with modifications and run and distribute to others.

I am providing the scripts, ffmpeg builds which are compiled by me from latest git repository.

I am providing these scripts to help you in any way if you are working with compression of video files. I hope that it may help you in any way.

Any critics are always welcome.

I have created a project in source forge and I am providing the link below



Regards,
Rupesh.
 
Thanks for sharing the scripts. Bash-fu is always useful. I used to do a lot of such stuff a decade ago when i was a member of teamicf and telugu torrents dot com.
If you are trying video look into avisynth scripting. You can do magic. But nowadays davinci resolve is the best tool for editing personal videos.
 
Hi @rupeshforu3
Thanks for sharing this! I remember your other thread discussing issues as you developed the script.

I'm curious about your experiences with various conversion settings with x265. If possible, could you share insights on say for e.g. the CRF range for different input video quality, conversion speed etc?
 
My experiences are as follows.

I thought that converting x264 mp4 to x265 mp4 file would reduce the memory space to half but it's not true. The source file and compressed file are of same size and some times output video file size is greater than source file.

There are two streams in any video file they are video and audio. If you play any video file you are going to view video and at the same time you are going to listen audio.

Suppose I have a x264 video mp4 file with video bitrate as 128 kbps and audio bitrate as 128 kbps and memory space of 93 mb. I have tried to compress this file in a number of ways.

When I have used ffmpeg and x265, with crf 24 option, fdkaac with audio bitrate as 50 kbps. The output video file size is 105 mb. After that I have seen the properties of the output video file with media info tool and found that video bitrate as 190 kbps and audio bitrate as 50 kbps. The quality of source and output video files are same.

After that I have used ffmpeg tool and x265 with crf 28 option this time the output video file size is 95 mb. After that I have seen the properties of output video file using media info tool and found that the video bitrate as 160 kbps. The quality of source and output video files are same.

After that I have used ffmpeg tool and x265 with no crf option but I have used -preset slow and this time the output video file size is 96 mb. After that I have seen the properties of output video file using media info tool and found that the video bitrate is 160 kbps.

After that I have used ffmpeg tool and x265 and two pass encoding without any crf option and I have not used any video bitrate and this time the output video file size 95 mb. After that I have seen the properties of output video file using media info tool and found that the video bitrate as 130 kbps.

Finally I have used ffmpeg tool and x265 with 2 pass encoding and video bitrate same as the source video file. This time the output video file size is 90 mb. After that I have seen the properties of output video file using media info tool and found that the video bitrate is same as source video file. The quality of the output video file is very good compared to all previous.

Here the lesson I learnt is when we pass crf option below 28 the video is converted with bitrate greater than the bitrate of source video file. The same is happening when you use options such as preset slow etc.,. Finally the output video file size is greater than source file.

The best way is to use ffmpeg tool and x265 with 2 pass encoding and fdkaac 50 kbps and passing the video bitrate as option. Here the video bitrate passed is same as the source file video bitrate.

After a lot of experiments I found that libx264 and libx265 offer same quality with same parameters used.

Now work is going on to produce x266 and vvc etc,. I think that using these may produce better compressed video files.

Another interesting point is that there are av1, daala video codecs which provides better quality than mp4. Try to visit the wikipedia pages of av1, daala.

Regards,
Rupesh.
 
There seems to be something wrong with your methodology. I recently converted 400GB of Doctor Who episodes from h264 to h265. Only with CRF set to 17 was the file size higher than source. I ended up with CRF 20 and got a reduction of 1/3rd in size. The higher you go in CRF, the lower the file size you will get. I used both Ffmpeg directly via CLI and a software called DVD Fab which should be using ffmpeg as backend. Got same results in both cases. I didn't make any changes to the audio stream as that was not needed. Like I had pointed out earlier, you could also have just chosen the video bitrate you wanted and that would definitely result in delivering the size you wanted.
 
As I have already stated mp4 video file consists of both video and audio. If you see the reduction of output file size then the audio stream might have compressed with lower audio bitrate not video stream.

At present try to convert any x264 video file of size 100 mb with crf 20 option.

After that try to view the properties of both source file and output file using media info tool. In this tool go to text view.

Media info tool is used to view all the properties of any video file or audio files. This tool also displays the video bitrate and how much size is consumed for video and at the same time it also displays the audio bitrate as well as how much size is consumed for audio stream.

I think that nothing is going to be achieved with crf option but if you use 2 pass encoding you can get quality output at lowest file size.

In the scripts provided by me I have tried to pass the source video bitrate to ffmpeg tool x265 encoder.

My final suggestion is suppose you have a mp4 video file which is captured from android smartphone camera with video bitrate as 15 mbps.

Try to convert this particular x264 file by providing target video bitrate as 400 kbps to the 2 pass script provided by me. After that try to provide crf 20 option to the crf script provided by me.

Compare the quality of two output video files. After that say whether crf or 2 pass encoding is best.
 
See, I tried to do the same thing as you did. But my file sizes and bit-rates were higher. With both ffmpeg and DVD fab, no changes were made to audio stream and it was copied as it is. This is the command I ran.
Code:
 ffmpeg - i D:\input.mkv -c:v libx265 - crf 20 - c:a copy D:\output.mkv

Running this command reduced the file size from 2.7GB to 1.7GB per file. If you don't provide a CRF value in this command, the default value of 23 is taken which results in even smaller size.
So I don't understand how it didn't work for you even at CRF 28. If you want to keep video bit-rate same then of course the size is going to remain the same or increase. The difference in the 2 protocols is that x265 provides similar quality at reduced bit-rates. So by using x265, I was able to reduce video bit-rate, hence the size, keeping the quality of the video same.

Edit - Also, you don't need the Media Info tool to get the bit-rate. You can calculate it using simple maths. Let us take both of the cases of my files. We can ignore the audio bit-rate because it will be same in both cases as I have just copied the audio stream as it is.

Now the length of the video was 45 minutes.
So 2.7GB for 45 minutes gives us the video bit-rate as 1.024MBps ( 2.7x1024/45/60) or 8.192Mbps.

And 1.7GB for same duration gives us the video bit-rate as 0.644MBps or 5.157Mbps.

So I was able to reduce the bit-rate from 8.192Mbps to 5.157Mbps without a discernable reduction in PQ by converting from x264 to x265 and hence reducing the file size from 2.7GB to 1.7GB.

That's the whole point of x265. It provides you similar PQ as x264 but at lower bit-rates and hence lower sizes. If you are going to keep the parameters aka the video bit-rate same, then no wonder the size of the files is going to remain the same.
Of course using 2 passes will result in an even lower file size, but you have to do a time vs size analysis to see if it is even worth it. For me it wasn't worth wasting my time on a second pass because further size reduction wasn't that much.
 
Last edited:
My opinion is

The main role of reducing the output file size is the video bitrate passed to the x265 encoder.

If crf is set lower higher video bitrate is passed to x265 encoder while crf is set higher lower video bitrate is passed to x265 encoder.

When crf option is used one cannot specify maximum video bitrate to be used to convert.

Instead try to use 2 pass encoding and if you want you can specify maximum video bitrate. If you want to use same video bitrate as source file don't modify my 2 pass encoding script.

Again I am saying size of output video file is dependent on how much video bitrate is passed to x265 encoder in convertion.

Higher video bitrate is passed higher the output video file size is.

Two pass encoding is same as vbr mode in audio encoding.

In two pass encoding first pass analyses input file to produce quality output video file and after that real encoding is done in second pass.
 
So basically you are saying the same thing I am.

Then why did you say this in your above post
Code:
 After a lot of experiments I found that libx264 and libx265 offer same quality with same parameters used.

This is not true. The main benefit of x265 is that even with lower video bit-rate (which is the main parameter in any video conversion) it can provide the same quality that x264 provides with a higher bit-rate. So it's wrong of you to say they provide same quality with same parameters. If that was true then no one would have shifted to x265 ever. It is the main discerning point between x264 and x265 that size reduces aka lower video bit rate produces no discernable difference in quality over x264.
 
500 kbps or 1000 kbps is enough to mp4 video file.

The video captured from android smartphone consists of 18 mbps.

So if you use crf 24 or crf 27 options the output video bitrate will be 5 mbps.

Definitely the output video file size will be less.

If you pass 500 kbps or 1 mbps to the video bitrate of x265 encoder the output video file size will be reduced a lot which you can't believe.

If you want even the size to be reduced try to pass 50 kbps to fdkaac or aac audio encoder.
 
So basically you are saying the same thing I am.

Then why did you say this in your above post
Code:
 After a lot of experiments I found that libx264 and libx265 offer same quality with same parameters used.

This is not true. The main benefit of x265 is that even with lower video bit-rate (which is the main parameter in any video conversion) it can provide the same quality that x264 provides with a higher bit-rate. So it's wrong of you to say they provide same quality with same parameters. If that was true then no one would have shifted to x265 ever. It is the main discerning point between x264 and x265 that size reduces aka lower video bit rate produces no discernable difference in quality over x264.
Like I said in the other thread raised by OP I don't really like the CRF option after trial and error as it adjusts the bitrate on a case by case basis based on each frame of the video and bitrate is the greatest factor in the file size. Hell going from 540p to 720p on same bitrate makes for a size delta of 5-7% at max.

My usual approach is copied from the 720p HEVC TV series files that I pirate. :P

For 720p a video bitrate of 1000 Kbps along with AAC dual channel audio with bitrate of 128 Kbps and a framerate of 23.976 works out well enough for me.

If you see the online stuff they usually go with 750 Kbps for the video bitrate for 720p x265 files.

For lower res files I go with 1200 Kbps for <=540p.

With NVenc HEVC encoder in Handbrake and a medium preset a 30 min video takes 1-2 min to encode.
 
Like I said in the other thread raised by OP I don't really like the CRF option after trial and error as it adjusts the bitrate on a case by case basis based on each frame of the video and bitrate is the greatest factor in the file size. Hell going from 540p to 720p on same bitrate makes for a size delta of 5-7% at max.

My usual approach is copied from the 720p HEVC TV series files that I pirate. [emoji14]

For 720p a video bitrate of 1000 Kbps along with AAC dual channel audio with bitrate of 128 Kbps and a framerate of 23.976 works out well enough for me.

If you see the online stuff they usually go with 750 Kbps for the video bitrate for 720p x265 files.

For lower res files I go with 1200 Kbps for
With NVenc HEVC encoder in Handbrake and a medium preset a 30 min video takes 1-2 min to encode.
With DVDfab that I used, even I just set the bitrate to 5000kbps and was done with it. But I first ran a test run with CRF value to see what size it achieves at value 20. With that size in mind, I calculated bit-rate and entered it in the tool to achieve similar file size for all consequent files.
 
500 kbps or 1000 kbps is enough to mp4 video file.

The video captured from android smartphone consists of 18 mbps.

So if you use crf 24 or crf 27 options the output video bitrate will be 5 mbps.

Definitely the output video file size will be less.

If you pass 500 kbps or 1 mbps to the video bitrate of x265 encoder the output video file size will be reduced a lot which you can't believe.

If you want even the size to be reduced try to pass 50 kbps to fdkaac or aac audio encoder.
I think you are not understanding the point I am making. I have an issue with this statement you made that their is no difference between x264 and x265 in quality when all parameters are same. Care to explain that, when it is clear that x265 achieves same picture quality at lower bit-rates compared to x264.
 
With DVDfab that I used, even I just set the bitrate to 5000kbps and was done with it. But I first ran a test run with CRF value to see what size it achieves at value 20. With that size in mind, I calculated bit-rate and entered it in the tool to achieve similar file size for all consequent files.
There's another approach that I have used in the past. You can use a sample TV Series x265 video downloaded using nefarious means.

Check the config for that file using mediainfo which will give you the myriad encoding settings as well so that you can copy it over as extra parameters for ffmpeg based GUI encoders.

I moved to a Ryzen 1700 specifically back in 2017 as NVenc was not very good at HEVC at that point and CPU encoding was slow as hell on an Intel 4670k.

But then once NVenc was sorted out I never tried CPU encoding any more.

And just a point to everyone else don't use Intel Quicksync if your bitrate is <=1000 Kbps. Video is far grainier compared to NVenc even if all other parameters remain the same.
 
I think you are not understanding the point I am making. I have an issue with this statement you made that their is no difference between x264 and x265 in quality when all parameters are same. Care to explain that, when it is clear that x265 achieves same picture quality at lower bit-rates compared to x264.
I have downloaded an video file from YouTube in my android smartphone and after that I have copied it to my system and tried to compress.

At first I have seen the properties of the above video file in media info tool and noticed that video codec used is x264 and video bitrate is 260 kbps. Audio codec is aac and audio bitrate as 128 kbps.

I thought that re encoding the same file to x265 would reduce the output file size and so I issued the following command.

ffmpeg -y -i source.mp4 -c:v libx265 -b:v 260k -c:a 128k output.mp4

After execution of the above command the output.mp4 is generated.

I thought that output.mp4 would be smaller than source.mp4 but it's not happened. The output.mp4 file size is almost equal to source.mp4.

I have seen the same for a number of files.

Upon seeing the above scenario I came to decission that x265 produces output with same file size as input source file.
 
I have downloaded an video file from YouTube in my android smartphone and after that I have copied it to my system and tried to compress.

At first I have seen the properties of the above video file in media info tool and noticed that video codec used is x264 and video bitrate is 260 kbps. Audio codec is aac and audio bitrate as 128 kbps.

I thought that re encoding the same file to x265 would reduce the output file size and so I issued the following command.

ffmpeg -y -i source.mp4 -c:v libx265 -b:v 260k -c:a 128k output.mp4

After execution of the above command the output.mp4 is generated.

I thought that output.mp4 would be smaller than source.mp4 but it's not happened. The output.mp4 file size is almost equal to source.mp4.

I have seen the same for a number of files.

Upon seeing the above scenario I came to decission that x265 produces output with same file size as input source file.
Do you have the same framerate in both files?

Keep in mind YT uses VP9 codec which is pretty efficient as well with a lot of other optimizations going. So yeah it makes sense that you saw larger size in x265 output file compared to YT video mpt.

But like others pointed out where x265 shines is in maintaining comparable video fidelity to x264 in lower bitrates.

So if your x264 file is like 300 MB at 1000 Kbps it will be comparable in terms of video quality to say a 220 MB x265 file with 750 Kbps.

x265 is even more size efficient at lower bit depth compared to x264.

Look at the shenanigans in terms of additional optimization that the encoder did for a sample file I got from ahem sources.

1639493839708.png
 
I have downloaded an video file from YouTube in my android smartphone and after that I have copied it to my system and tried to compress.

At first I have seen the properties of the above video file in media info tool and noticed that video codec used is x264 and video bitrate is 260 kbps. Audio codec is aac and audio bitrate as 128 kbps.

I thought that re encoding the same file to x265 would reduce the output file size and so I issued the following command.

ffmpeg -y -i source.mp4 -c:v libx265 -b:v 260k -c:a 128k output.mp4

After execution of the above command the output.mp4 is generated.

I thought that output.mp4 would be smaller than source.mp4 but it's not happened. The output.mp4 file size is almost equal to source.mp4.

I have seen the same for a number of files.

Upon seeing the above scenario I came to decission that x265 produces output with same file size as input source file.
You need to get your basics correct first. Do you know the relation between video bit-rate and file size. How can you expect to reduce file size while keeping the bit rate same?
The only reason you would convert a x264 file to an x265 file would be to keep the picture quality same while lowering the bit-rate so to get a smaller file size. The main measure to judge is the PQ.

I don't know if you know this or not but
Code:
 File size = Bit-rate x Length of video

So how were you expecting to reduce the file size while keeping the bit-rate same.
 
Ok thanks for your clarification.

My source files which are downloaded from YouTube and captured from android smartphone are mostly x264 only.

I thought x265 offers good quality at small file size and it's true.

Another interesting fact is that there is some work going on related to x266 and vvc etc., which offer even better quality output.

I found that there are other video codecs like av1, daala which offer better quality than mp4 or hevc. But I have not tried.
 
Back
Top