Skip to content

Instantly share code, notes, and snippets.

@r17x
Forked from trvswgnr/compress_video
Created August 22, 2024 09:19
Show Gist options
  • Save r17x/41f76e92f377c13697704923a54bd246 to your computer and use it in GitHub Desktop.
Save r17x/41f76e92f377c13697704923a54bd246 to your computer and use it in GitHub Desktop.

Revisions

  1. @trvswgnr trvswgnr revised this gist Aug 22, 2024. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions compress_video
    Original file line number Diff line number Diff line change
    @@ -22,12 +22,6 @@ get_filepath_without_extension() {
    echo "${1%."$ext"}"
    }

    # Check if ffmpeg is installed
    if ! command -v ffmpeg >/dev/null 2>&1; then
    echo "ERROR: ffmpeg is not installed. Please install it and try again"
    exit 1
    fi

    if [ $# -ne 1 ]; then
    echo "ERROR: input file is required"
    print_usage
  2. @trvswgnr trvswgnr revised this gist Aug 22, 2024. 1 changed file with 12 additions and 8 deletions.
    20 changes: 12 additions & 8 deletions compress_video
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ get_filepath_without_extension() {

    # Check if ffmpeg is installed
    if ! command -v ffmpeg >/dev/null 2>&1; then
    echo "ERROR: ffmpeg is not installed. Please install it and try again."
    echo "ERROR: ffmpeg is not installed. Please install it and try again"
    exit 1
    fi

    @@ -49,10 +49,15 @@ acodec="aac"
    format_opt=""

    case $input_file_ext in
    mp4|mov)
    mp4)
    vcodec="libx264"
    acodec="aac"
    ;;
    mov)
    vcodec="libx264"
    acodec="aac"
    format_opt="-f mov"
    ;;
    webm)
    vcodec="libvpx-vp9"
    acodec="libopus"
    @@ -72,13 +77,12 @@ avi|flv)
    ;;
    esac

    cmd="ffmpeg -i \"$input_file\" -c:v $vcodec -crf 23 -preset medium -c:a $acodec $format_opt \"$output_file\""

    echo "Compressing video. This may take a while..."
    if eval "$cmd"; then
    echo "Compression completed successfully"
    echo "Output file: $output_file"
    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"
    echo "ERROR: compression failed"
    exit 1
    fi
  3. @trvswgnr trvswgnr revised this gist Aug 22, 2024. 1 changed file with 23 additions and 11 deletions.
    34 changes: 23 additions & 11 deletions compress_video
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    print_usage() {
    echo "usage: compress_video <input_file>"
    echo "supported formats: mp4, webm, mkv, mov"
    echo "supported formats: mp4, webm, mkv, mov, avi, flv"
    }

    get_extension() {
    @@ -22,6 +22,12 @@ get_filepath_without_extension() {
    echo "${1%."$ext"}"
    }

    # Check if ffmpeg is installed
    if ! command -v ffmpeg >/dev/null 2>&1; then
    echo "ERROR: ffmpeg is not installed. Please install it and try again."
    exit 1
    fi

    if [ $# -ne 1 ]; then
    echo "ERROR: input file is required"
    print_usage
    @@ -37,8 +43,13 @@ 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)
    mp4|mov)
    vcodec="libx264"
    acodec="aac"
    ;;
    @@ -50,23 +61,24 @@ mkv)
    vcodec="libx265"
    acodec="libopus"
    ;;
    mov)
    avi|flv)
    vcodec="libx264"
    acodec="aac"
    format_opt="-f mov"
    format_opt="-f mp4"
    output_file="$(get_filepath_without_extension "$input_file")_compressed.mp4"
    ;;
    *)
    echo "ERROR: unsupported video format"
    exit 1
    echo "WARNING: unsupported video format - trying with default codecs"
    ;;
    esac

    cmd="ffmpeg -i $input_file -c:v $vcodec -crf 28 -c:a $acodec $format_opt $output_file"
    cmd="ffmpeg -i \"$input_file\" -c:v $vcodec -crf 23 -preset medium -c:a $acodec $format_opt \"$output_file\""

    if $cmd; then
    echo "compression completed successfully"
    echo "output file: $output_file"
    echo "Compressing video. This may take a while..."
    if eval "$cmd"; then
    echo "Compression completed successfully"
    echo "Output file: $output_file"
    else
    echo "ERROR: compression failed"
    echo "ERROR: Compression failed"
    exit 1
    fi
  4. @trvswgnr trvswgnr created this gist Aug 22, 2024.
    72 changes: 72 additions & 0 deletions compress_video
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #!/bin/sh

    print_usage() {
    echo "usage: compress_video <input_file>"
    echo "supported formats: mp4, webm, mkv, mov"
    }

    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"

    case $input_file_ext in
    mp4)
    vcodec="libx264"
    acodec="aac"
    ;;
    webm)
    vcodec="libvpx-vp9"
    acodec="libopus"
    ;;
    mkv)
    vcodec="libx265"
    acodec="libopus"
    ;;
    mov)
    vcodec="libx264"
    acodec="aac"
    format_opt="-f mov"
    ;;
    *)
    echo "ERROR: unsupported video format"
    exit 1
    ;;
    esac

    cmd="ffmpeg -i $input_file -c:v $vcodec -crf 28 -c:a $acodec $format_opt $output_file"

    if $cmd; then
    echo "compression completed successfully"
    echo "output file: $output_file"
    else
    echo "ERROR: compression failed"
    exit 1
    fi