Using FFmpeg Batch Files to Compress Files for Space-Saving
Have you ever faced an issue where your hard drive keeps filling up, and you want to save some space? One possible solution to this problem is to compress your files using FFmpeg through batch files. This article will focus on how you can compress files using FFmpeg batch files, which will help you save a significant amount of space on your hard drive.
What is FFmpeg?
FFmpeg is a free and open-source project consisting of a vast software suite of libraries and programs for handling video, audio, and other multimedia files and streams. It includes libavcodec, an audio/video codec library used to encode and decode various audio and video formats. FFmpeg can also be used to compress files.
What are batch files?
A batch file is a script file in DOS, OS/2, and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file.
How to use FFmpeg batch files to compress files?
In order to compress files using FFmpeg, you'll first need to create a batch file that will execute the FFmpeg command for each file you want to compress. Follow these steps to create a batch file to compress your files:
- Create a text file with the extension .bat, for example,
compress_files.bat. - Open the text file using a text editor like Notepad or Visual Studio Code.
- Add the following lines to the batch file:
@echo off for %%a in (*.mp4) do ( ffmpeg -i "%%a" -c:v libx265 -crf 28 -c:a aac -b:a 128k "compressed\%%~na.mp4" )
The batch file created above loops through all files in the current directory with the extension .mp4 and compresses them using the FFmpeg command. The compressed files are saved to the compressed folder, which you will have to create manually.
Explanation of the batch file commands:
-
@echo off- This command prevents the display of command echoes in the console window. It makes the output cleaner. -
for %%a in (*.mp4) do (- This command iterates through all files with the extension .mp4. The iteration variable is set to%%a. -
ffmpeg -i "%%a" -c:v libx265 -crf 28 -c:a aac -b:a 128k "compressed\%%~na.mp4"- This is the actual FFmpeg command used for compression. To save space, this command uses the HEVC encoder (libx265) to encode the video. The Constant Rate Factor (CRF) value is set to 28, which is a tradeoff between compression and quality. We also encode the audio using AAC at 128kbps. -
)- This command closes theforloop.
FFmpeg options used for compression:
-
-i- This option specifies the input file name and path, which is the file you want ```less to compress.