Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells, Featured, Files

Squeezing Video Files

Submitted by on June 30, 2019 – 11:16 am

All that crap I’ve been saving from YouTube, Facebook and whatnot tends to add up. As quality is not a huge concern here (not that it was very high to begin with), optimizing those video files can recover a surprisingly significant amount of disk space. If you have CPU cycles to spare that is. The main advantage here is really not the disk space, which is cheap nowadays, but faster network transfer rates.

All of this has been done in Windows 10 Pro running Ubuntu 18.04.1 LTS installed from the Microsoft store. The ffmpeg utility was installed like so:

The script below does a few things:

  1. Finds video files in the source folder. You can control the depth of search.
  2. Identifies the original encoding (this is if you want to stay with it).
  3. Calculates the optimal bitrate based on the duration of the video.
  4. Re-encodes the file using H.265 (or you can stick with the original codec).
  5. Tells you how much disk space you saved.

So let me break this down a bit. This line will find “video” files in the specified folder, without descending into subfolders. You can control this by modifying the -maxdepth option.

The two lines below will attempt to get the name of the codec used by the original file and to identify the appropriate encoder available on your system. This does not always work as expected, unfortunately, so feel free to make improvements.

The nested ffmpeg in the command below identifies the duration of the video in seconds. It then divides 10^9 (1GB) by that value to get the optimal bitrate and passes that to the -b option.

The script is below and is also available on my GitHub.

[su_spoiler title=”Das Skript” icon=”arrow-circle-1″] [/su_spoiler]

While I am on the subject of videos, recently I went to an event and made a few rather shaky videos with my little Sony camera (lots of people pushing and shoving, and the two beers I had didn’t help steady the hand either).

So here’s a quick-and-dirty way to remove some of the shakiness out of your videos.

If your video is  extremely shaky, you may need to pass some additional parameters to the first command that generates the transforms.trf file. However, keep in mind that the more smoothing you do, the more weird the outcome might be.

Here’s a useful one: you can make a side-by-side video of the original and the stabilized versions to see the overall effect:

Thanks for watching.

Print Friendly, PDF & Email

Leave a Reply