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

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://slhck.info/video/2017/03/01/rate-control.html

https://trac.ffmpeg.org/wiki/Encode/H.265

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

https://sourceforge.net/projects/ffmpeg-small-shell-scripts/files//

Interesting.

For a single file, I typically I use a simple bash shell command:


videofile="input-file"; ffmpeg -i $videofile.mp4 -c:v libx265 -q:v 5 $videofile-h265.mp4

I have that saved in a text file on my desktop, and anytime I want to change compression from mp4 to h265, for foo.mp4 (for example) I simply replace “input-file” with “foo” (minus the extension) and the one line ffmpeg command does the rest.

Assuming the file with the command is called ffcomp (you can of course use a name to your liking).

What about changing it into

#!/usr/bin/bash
ffmpeg -i $1.mp4 -c:v libx265 -q:v 5 $1-h265.mp4

Moving it in your bin directory (which is mentioned in your PATH environment variable):

mv ffcomp ~/bin/ffcomp

Allow yourself to execute it:

chmod u+x ~/bin/ffcomp

And then using it in the future by a simple (no editing required anymore):

ffcomp <the-file-name-without-the-.mp4-suffix>

May I know the meaning of -q:v 5.

I copied this off of the web - where another user recommended using “q:v 5” . I think it is to set the constant quality (VBR) encoding with ffmpeg. So I believe the -q:v option can be used to set the encoding quality. I think one can tweak the q-value for quality/size tradeoff.

I note this web site: General FFmpeg encoding guide | evilsoup

which states:[INDENT]
You can target a certain ‘quality level’, letting the encoder decide what bit rate should be used to achieve that quality. Normally, this is set with the -qscale (‘quality scale’), or -q option, with a numerical scale determined by the specific encoder (but generally between 1-31). For example, using -c:v mpeg4 -q:v 3 will encode MPEG4 video at a very high quality. There are some encoders (like x264 and libfdk_aac) that use their own special option for determining quality.

Unless you absolutely need the output file to be a certain size, you should generally take this approach.

[/INDENT]

This seems to work for me for h265, but I have not researched this in any detail.

I tried to glance through

man ffmpeg

But it is a bit beyond my interest and knowledge. It seem that the v in -q:v is a “stream specifier” that indicates “video”.
See the -q option and the chapter about StreamSpecifiers.

while this is a nice exercise and I regularly use ffmpeg to recode my own kdenlive videos, there is hardly a point in recoding YT videos. Just use a downloader like Android NewPipe and choose the format (and thus file size) you like. It is somewhat unlikely that you can beat Google’s choices in terms of size and compatibility.

As for recoding my own videos, kdenlive actually shows the ffmpeg call it uses so it is easy to play around with opts. Avidemux offers graphical codec config with tons of knobs to turn. There is really no need to spend hours in googling cli params just for recoding unless we talk automation.

Ok thanks for your comments.

Avidemux is really a nice tool and I have tried to convert x264 files to x265 by filling all the options required to convert but I can’t find the convert button in the whole gui. Can you specify any link on how to use this tool for converting x264 to x265 or atleast to convert from one format to another.

Thanks. That works nicely.

You may want to check your other thread where you’re asking about x265 conversions, or your previous threads on using ffmpeg and avidemux that go back several years now. While it’s laudable that you have tried to give back to the community, you scripts lack a good bit. You’re limiting things to 3 cores? Why would I want to do that when I have 64 cores available to me on my main system, and 16 on my laptop? What if I want to change the bitrate? I have no option to. What about the audio? I can’t select which of the language tracks I want using your script, nor can I select/burn-in subtitles if needed.

And using 100% of my CPU isn’t going to “destroy” my computer, and most systems have throttling software to keep things from overheating and/or shut the system down totally.

The avidemux website/forum has several links that describe how to do what you’re after, so reading their documentation would be an excellent place to start in regards to x264-65 conversions. You may also wish to check with the person who got your scripts to work:

And I’m a bit puzzled by the fact you wrote scripts, especially since you’ve said previously you couldn’t:
https://forums.opensuse.org/showthread.php/539079-Apologizes-regarding-my-posts-and-about-myself?highlight=script