Skip to content

Instantly share code, notes, and snippets.

@trvswgnr
Last active February 5, 2025 20:37
Show Gist options
  • Save trvswgnr/55aa99a45781a3ff68e79b76a6ab7d70 to your computer and use it in GitHub Desktop.
Save trvswgnr/55aa99a45781a3ff68e79b76a6ab7d70 to your computer and use it in GitHub Desktop.
portable shell script to compress videos with ffmpeg
#!/bin/sh
print_usage() {
echo "usage: compress_video <input_file>"
echo "supported formats: mp4, webm, mkv, mov, avi, flv"
}
get_extension() {
f="${1##*/}"
case "$f" in
.*) get_extension "${f#.}" && return 0 ;;
esac
case "$f" in
.*.*) echo "${f#.}" ;;
*.*) echo "${f#*.}" ;;
*) return 0 ;;
esac
}
get_filepath_without_extension() {
ext=$(get_extension "$1")
echo "${1%."$ext"}"
}
if [ $# -ne 1 ]; then
echo "ERROR: input file is required"
print_usage
exit 1
fi
input_file="$1"
if [ ! -f "$input_file" ]; then
echo "ERROR: input file '$input_file' does not exist"
exit 1
fi
input_file_ext="$(get_extension "$input_file")"
output_file="$(get_filepath_without_extension "$input_file")_compressed.$input_file_ext"
# Default to libx264 and aac for unknown formats
vcodec="libx264"
acodec="aac"
format_opt=""
case $input_file_ext in
mp4)
vcodec="libx264"
acodec="aac"
;;
mov)
vcodec="libx264"
acodec="aac"
format_opt="-f mov"
;;
webm)
vcodec="libvpx-vp9"
acodec="libopus"
;;
mkv)
vcodec="libx265"
acodec="libopus"
;;
avi|flv)
vcodec="libx264"
acodec="aac"
format_opt="-f mp4"
output_file="$(get_filepath_without_extension "$input_file")_compressed.mp4"
;;
*)
echo "WARNING: unsupported video format - trying with default codecs"
;;
esac
echo "compressing video. this could take a while..."
if ffmpeg -i "$input_file" -c:v $vcodec -crf 23 -preset medium -c:a $acodec $format_opt "$output_file"; then
echo "compression completed successfully"
echo "output file: $output_file"
else
echo "ERROR: compression failed"
exit 1
fi
@masterdesky
Copy link

Does this cost $49.99 or $59.99 to use?

@bandinopla
Copy link

bandinopla commented Aug 22, 2024

why is there code? where is the exe??

it is a linux shell script. A text file that ends with .sh you run it using the console command...

chmod +x compress_video.sh
./compress_video.sh

You can run it on windows if you install ubuntu for WSL: https://ubuntu.com/desktop/wsl
Then, since you're using Ubuntu via WSL (Windows Subsystem for Linux), direct drag-and-drop from Windows to Ubuntu isn't supported. However, you can access Windows files in WSL and move them to your Linux environment. Windows files are available under the /mnt/ directory. For example, the C drive is accessible at /mnt/c/. Navigate to your Windows files like this:

cd /mnt/c/Users/your-username/path-to-file

So to use this script, in windows, you would open the ubuntu WSL console and run (assuming you downloaded this script in the root):

./compress_video.sh /mnt/c/some-video-folder/video-you-want-to-compress.mp4

@JD-The-65th
Copy link

why is there code? where is the exe??

I wish this was satire and hope to god that it is but with the general state of how things are I really can't bring myself to believe it is

@advnpzn
Copy link

advnpzn commented Aug 22, 2024

why is there code? where is the exe??

I wish this was satire and hope to god that it is but with the general state of how things are I really can't bring myself to believe it is

Definitely satire.

@narendnp
Copy link

I made the pwsh version for our poor Windows user (so that you don't have to install WSL).

function Write-Usage {
    Write-Host "Supported formats: mp4, webm, mkv, mov, avi, flv."
}

function Get-Extension {
    param (
        [string]$filePath
    )

    $fileName = [System.IO.Path]::GetFileName($filePath)
    $extension = [System.IO.Path]::GetExtension($fileName)
    return $extension.TrimStart('.')
}

function Get-FilePath-Without-Extension {
    param (
        [string]$filePath
    )

    $extension = Get-Extension -filePath $filePath
    return $filePath.Substring(0, $filePath.Length - $extension.Length - 1)
}

if ($args.Count -ne 1) {
    Write-Host "ERROR: Input file is required."
    Write-Usage
    exit 1
}

$inputFile = $args[0]
if (-not (Test-Path -Path $inputFile -PathType Leaf)) {
    Write-Host "ERROR: Input file '$inputFile' does not exist."
    exit 1
}

$inputFileExt = Get-Extension -filePath $inputFile
$outputFile = "$(Get-FilePath-Without-Extension -filePath $inputFile)_compressed.$inputFileExt"

# Default to libx264 and aac for unknown formats
$vcodec = "libx264"
$acodec = "aac"
$formatOpt = ""

switch ($inputFileExt) {
    "mp4" {
        $vcodec = "libx264"
        $acodec = "aac"
    }
    "mov" {
        $vcodec = "libx264"
        $acodec = "aac"
        $formatOpt = "-f mov"
    }
    "webm" {
        $vcodec = "libvpx-vp9"
        $acodec = "libopus"
    }
    "mkv" {
        $vcodec = "libx265"
        $acodec = "libopus"
    }
    "avi" {
        $vcodec = "libx264"
        $acodec = "aac"
        $formatOpt = "-f mp4"
        $outputFile = "$(Get-FilePath-Without-Extension -filePath $inputFile)_compressed.mp4"
    }
    "flv" {
        $vcodec = "libx264"
        $acodec = "aac"
        $formatOpt = "-f mp4"
        $outputFile = "$(Get-FilePath-Without-Extension -filePath $inputFile)_compressed.mp4"
    }
    default {
        Write-Host "WARNING: Unsupported video format, trying with default codecs...";
    }
}

Write-Host "Compressing video, this could take a while..."
$ffmpegCmd = "ffmpeg -i `"$inputFile`" -c:v $vcodec -crf 23 -preset medium -c:a $acodec $formatOpt `"$outputFile`""
Invoke-Expression $ffmpegCmd
if ($?) {
    Write-Host "Compression completed successfully."
    Write-Host "Output file: $outputFile"
}
else {
    Write-Host "ERROR: Compression failed."
    Write-Host "Make sure you have ffmpeg installed."
    exit 1
}

Setting up:

  1. Open PowerShell (or Terminal, if it runs Powershell by default).
  2. Run notepad compress-video.ps1.
  3. Paste the script.
  4. Save, if dialog box popped up, just press Yes.
  5. Close notepad.

Running the script is more or less the same as the original.
.\compress-video.ps1 path-to-the-video\video-you-want-to-compress.mp4

@WarrenBuffering
Copy link

is this rust?

@thomprycejones
Copy link

sorry i do not understand

@gautamkrishnar
Copy link

is it possible to run this on my Samsung refrigerator?

@GottZ
Copy link

GottZ commented Aug 23, 2024

magically-compression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment