Skip to content
Gallery
nytgames
CreativeTech Wiki (internal)
Share
Explore
Recipes

icon picker
ffmpeg Media Compression

Features

is a handy tool to batch compress medias, specifically videos and images to be used in Paid Posts. With a shell script we can automate their compression.

How to

With ffmpeg installed on your system, follow and personalize these steps.

1) Save the assets

Add all original assets to a folder.

2) Add a shell script

In the same folder, add one of those script, saving them as shell script, for example “yourscript.sh”.
You can modify the parameters to adjust the workflow, specifically FFMPEG_PARAMS, to modify the compression parameters and OUTPUT_DIR, to change the name of the output folder.

Optimise MP4s for Web

# Compress MP4 files for web
FFMPEG_PARAMS="-vcodec libx264 -crf 23 -preset slow -an -map_metadata -1"
# Compress MP4 files for web, resizing them to 1920 if needed
# FFMPEG_PARAMS="-vcodec libx264 -crf 23 -preset slow -an -map_metadata -1 -vf scale='min(1920\\,iw)':-1"

# Output directory for the converted files
OUTPUT_DIR="compressed-mp4"

# ANSI color codes
RED="\033[0;31m"
GREEN="\033[0;32m"
NC="\033[0m" # No Color

# Check if the output directory exists; create it if not
if [ ! -d "$OUTPUT_DIR" ]; then
mkdir -p "$OUTPUT_DIR"
echo -e "${GREEN}Created output directory: ${OUTPUT_DIR}${NC}"
else
echo -e "${GREEN}Using existing output directory: ${OUTPUT_DIR}${NC}"
fi

# Convert each MP4 file in the current directory
for i in *.mp4;
do
name=$(echo "$i" | cut -d'.' -f1)
echo -e "${GREEN}Converting $i...${NC}"
ffmpeg -i "$i" $FFMPEG_PARAMS "${OUTPUT_DIR}/${name}.mp4"
done

echo -e "${GREEN}Conversion completed. Files saved in ${OUTPUT_DIR}${NC}"

Compress JPEGs for Web

#!/bin/sh

# Compress JPG files for web
FFMPEG_PARAMS="-compression_level 10"
# Compress JPG files for web, resizing them to 1920 if needed
# FFMPEG_PARAMS="-vf scale=1920:-1 -compression_level 10"

# Output directory for the converted files
OUTPUT_DIR="compressed-jpegs"

# ANSI color codes
RED="\033[0;31m"
GREEN="\033[0;32m"
NC="\033[0m" # No Color

# Check if the output directory exists; create it if not
if [ ! -d "$OUTPUT_DIR" ]; then
mkdir -p "$OUTPUT_DIR"
echo -e "${GREEN}Created output directory: ${OUTPUT_DIR}${NC}"
else
echo -e "${GREEN}Using existing output directory: ${OUTPUT_DIR}${NC}"
fi

# Convert each JPG file in the current directory
for i in *.jpg;
do
name=$(echo "$i" | cut -d'.' -f1)
echo -e "${GREEN}Converting $i...${NC}"
ffmpeg -i "$i" $FFMPEG_PARAMS "${OUTPUT_DIR}/${name}.jpg"
done

echo -e "${GREEN}Conversion completed. Files saved in ${OUTPUT_DIR}${NC}"

3) Run the script

Open your folder in a Terminal (you can drag the folder on the Terminal app icon, or use the integrated terminal in VS Code) and run the following command:
bash yourscript.sh

4) Check the output

Check the messaging in your terminal and the output folder to check the result.

5) (Optional) Upload them to a GC Bucket

To serve them, you might want to .

6)Editor Setup

TBD

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.