aboutworkServicesLAB186WeLLREDCAREERSContactAccessibility
aboutworkserviceslab186wellredcareerscontact
click here to listen to replyall
in the red

How To Use Scripts To Automate Video Compression For Social Media

Scripts Automated Video Compression Social Media

We create and publish lot of videos at The Pollack Group. Whether a documentary series about hemp farming, short-form pieces about farm techsocial media content featuring medical professionals, or mobile-optimized ads for e-commerce – not to mention best wishes for the holidays – we publish various pieces for clients and the industry in general. And we aim to deliver our videos in the best quality for their respective medium (streaming for TV or optimized for mobile).

Unfortunately, the default settings for video editing programs are to export videos as quickly as possible, and, as such, quality gets sacrificed. This is primarily a problem for universal platforms, like YouTube or Facebook, which do the hard work of optimizing videos for you. For instance, YouTube can encode videos into nearly 50 unique picture and audio files, utilizing whichever combination is best for your device and internet bandwidth.

So, what do we do when the editing platform we’re working on at Pollack isn’t up to par? We build our own tools.

Like I wrote about in a previous article about optimizing photos for the web with a few bash scripts, I’m a believer in the idea that a true creator builds their own tools. I take the example from my father who owned a picture framing company, and would often get work orders of thousands of frames. (A few clients were restaurant chains that needed framed pictures for hundreds of restaurants around the country). When the team on the factory floor needed to prepare, assemble, and ship thousands of frames at a time, they quickly recognized certain tasks that need to be repeated.  If there was a way to streamline a repetitive task, this could save a lot of money in time and labor.

For a specific example about framing, the vertical placement of the wire used to hang a picture frame on the wall is ⅓ of the length of the frame. Multiply the length of the frame by .33, and you’ll have the distance that you need to place the “D-Ring” of the frame. So, when you’re building a thousand frames, write that measurement on a Post-It note that you’ll reference when you measure the next frame to place the D-Ring, right? And measure each frame twice for each D-Ring? First, on a factory floor, you’ll have hundreds of Post-It notes with numbers that you, or another team member, will forget or not understand. And even though using a standard ruler to measure each placement will take a few seconds, a few seconds multiplied by a thousand adds up to hours.

So, what did my father do? He built an L-Bracket out of wood that set its measurements by the length of the frame. So, 4 inches down, the bracket will read 12”. Not only does this free the worker from not having to make calculations in their head all day, but they also don’t need to mark their spot with a pencil- they have their drill ready to go. Tools don’t have to be complex; they need to work.

Likewise, if there’s a task on the computer that I find needs to be repeated multiple times, especially if the task doesn’t require a lot of differentiation, I try to find a way to simplify and automate the process. In the case of video compression, many editing software will reference data libraries that are out of date or that can be improved with free and open-source tools. And when YouTube publishes the settings they encourage users to encode for videos, there’s no reason to use another software’s settings.

Like the last article, this set of scripts uses the command line, but it will utilize a different program for video compression. FFMPEG is a free and open-source program that has been around for 20 years. I was first aware of it as a program to convert files from one format to another, (like converting my old video camera’s AVI files to RM for RealPlayer). Still, a few years ago, I was shocked to find out how many filters the program now contains that can do anything from stabilizing footage, remove grain, even transcribe text on the screen. And because it is a command-line program, it can save a lot of time in running a graphic user interface (GUI) that requires you to set things up manually. I just write a script, link it to the file, and the script is good to go.

To get started, follow the last article for more detail about the command line and Homebrew. FFMPEG is much more comprehensive than Imagemagick in that some file formats and reference libraries get revised over time. You won’t need to install everything FFMPEG has to offer, but what you’re getting is far beyond the scope of this article. Homebrew has done an excellent job of including the essentials in its basic install.

To install FFMPEG:

1) Open the Command Line
2) Type
brew install ffmpeg

Wait a few minutes for the installation process.

While you wait, create the YouTube compression script. Again, YouTube provides their recommended settings, so you will be utilizing those settings.

3) Open up TextEdit/Notepad
4) Type

#!/bin/bash
# $1 = input file
ffmpeg -i "$1" -c:v libx264 -preset fast -profile:v high -crf 18 -coder 1 -pix_fmt yuv420p -g 30 -bf 2 -c:a aac -b:a 384k -profile:a aac_low -strict -2 -r:a 48000 -flags +cgop -movflags +faststart "$1_ffmpeg.mp4"

What this script will do, is to take an input video file (FFMPEG can work with a lot of file formats, so I’ve often used this just to make copies of videos more compatible with my computer to browse) and optimize it for YouTube and streaming. It will keep the video’s same dimensions (i.e., a 4K video will remain 4K) and add “_ffmpeg” to the end of the filename (which I just manually rename if necessary).

5) Save this as youtube.sh in your user folder, which for Macs is usually
Macintosh HD > Users > [Your user name]
Handbrake should be finished installing FFMPEG by now, so you can give this script a shot.

6) In Terminal, type
sh youtube.sh
7) Drag your video file into the Terminal window (this is easier than manually typing the file directory).
8) Click Enter

The Terminal window will update with its progress, which should be around ⅓ the duration of your video, depending on the power of your computer. It will save the file in the same folder as your original video file.

Now what if you need to resize the video? If there’s a 4K video that needs to be 1080p, or embedded on a site that only needs 540p, FFMPEG can easily and intelligently scale down the video (there’s a variety of resizing algorithms you can choose; it’s nuts).

Here’s the script I use:

#!/bin/bash
# $1 = input file
# $2 = video width
ffmpeg -i "$1" -vf "scale=$2:-2:flags=lanczos" -c:v libx264 -preset fast -profile:v high -crf 18 -coder 1 -pix_fmt yuv420p -movflags +faststart -g 30 -bf 2 -c:a aac -b:a 384k -profile:a aac_low -strict -2 -r:a 48000 -flags +cgop "$1_ffmpeg.mp4"

9) Save this as youtuberesize.sh in your User folder

This script is the same, but it adds an additional scaling setting that you’ll need to type after you link to the original video file. So, if we want to scale a 3840x2160 pixel video to 304x540, you run the command like so

10) In terminal, type
sh youtubresize.sh
11) Drag the video file into the Terminal window
12) Type the width of the video you want after the filename
540

FFMPEG will now do its job.

How about if you created an animation that doesn’t have any sound, like a background video for a website? If you were to remove the audio layer from the file, this would reduce the size of the file and help the website load faster.

Here’s the script I use:

#!/bin/bash

# $1 = input file

ffmpeg -i "$1" -c:v libx264 -preset fast -profile:v high -crf 18 -coder 1 -pix_fmt yuv420p -movflags +faststart -g 30 -bf 2 -an -strict -2 -r:a 48000 -flags +cgop "$1_ffmpegsilent.mp4"

13) Save this file as
youtubesilent.sh

Or resize a silent video?

#!/bin/bash

# $1 = input file

ffmpeg -i "$1" -vf "scale=$2:-2:flags=lanczos" -c:v libx264 -preset fast -profile:v high -crf 18 -coder 1 -pix_fmt yuv420p -movflags +faststart -g 30 -bf 2 -an -strict -2 -r:a 48000 -flags +cgop "$1_ffmpeg.mp4"

14) Save this file as
youtubesilentresize.sh

These are just a few of the scripts I often find myself using on a nearly daily basis. In my usual workflow, I’ll export a video in ProRes 422 in the project’s native dimensions (4K if possible) and compress it with one of these scripts. Yes, this does add an extra step, but the video quality is far better at a reasonable file size than what comes by default in Adobe Premiere or Final Cut Pro. Handbrake is a free application that utilizes FFMPEG and, although they come with some settings included, but I find typing sh youtube.sh and dragging a file, much faster.

I even use more bash scripts than this, particularly when I need to crop a 16:9 wide-angle video to 1:1 square for social media or apply special color filters for HDR videos. Still, these scripts are more useful when I’m certain they’ll work for my project. For example, I would only crop a wide video if I knew the action was featured in the center of the frame, and I didn’t have to worry about text and titles getting cut off.

The important part of the tools we create is that they help make our tasks easier, faster, or optimized for our goals. Tools shouldn’t be an end goal in and of themselves, but chances are there’s a process that can be streamlined.

want more wellred? click here!