Skip to content

Instantly share code, notes, and snippets.

@moregatest
Forked from danirukun/whisper-transcribe.bash
Created October 31, 2023 03:37
Show Gist options
  • Select an option

  • Save moregatest/c9f85d4aad0f2e44b496e834cf06823f to your computer and use it in GitHub Desktop.

Select an option

Save moregatest/c9f85d4aad0f2e44b496e834cf06823f to your computer and use it in GitHub Desktop.

Revisions

  1. Daniils Petrovs revised this gist Nov 12, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion whisper-transcribe.bash
    Original file line number Diff line number Diff line change
    @@ -121,4 +121,4 @@ ffmpeg -i "${temp_dir}/vod.mp4" \

    cleanup

    msg "Done! Your finished file is ready: res.mp4"
    msg "Done! Your finished file is ready: res.mp4"
  2. Daniils Petrovs revised this gist Oct 21, 2022. No changes.
  3. Daniils Petrovs revised this gist Oct 20, 2022. 1 changed file with 12 additions and 4 deletions.
    16 changes: 12 additions & 4 deletions whisper-transcribe.bash
    Original file line number Diff line number Diff line change
    @@ -29,8 +29,9 @@
    set -Eeuo pipefail

    # You can find how to download models in the OG repo: https://github.com/ggerganov/whisper.cpp/#usage
    MODEL_PATH="${MODEL_PATH:-models/ggml-base.en.bin}"
    WHISPER_EXECUTABLE="${WHISPER_EXECUTABLE:-whisper}"
    MODEL_PATH="${MODEL_PATH:-models/ggml-base.en.bin}" # Set to a multilingual model if you want to translate from foreign lang to en
    WHISPER_EXECUTABLE="${WHISPER_EXECUTABLE:-whisper}" # Where to find the whisper.cpp executable
    WHISPER_LANG="${WHISPER_LANG:-en}" # Set to desired lang to translate from

    temp_dir="tmp"
    source_url="$1"
    @@ -45,7 +46,8 @@ cleanup() {
    }

    print_help() {
    echo "Usage: ./whisper-transcribe <video_url>"
    echo "Usage: ./transcribe-vod <video_url>"
    echo "See configurable env variables in the script"
    echo "This will produce an MP4 muxed file called res.mp4 in the working directory"
    echo "Requirements: ffmpeg yt-dlp whisper"
    echo "Whisper needs to be built into the main binary with make, then you can rename it to something like 'whisper' and add it to your PATH for convenience."
    @@ -99,7 +101,13 @@ ffmpeg -i "${temp_dir}/vod.mp4" \
    msg "Transcribing to subtitle file..."
    msg "Whisper specified at: ${WHISPER_EXECUTABLE}"

    $WHISPER_EXECUTABLE -m "${MODEL_PATH}" -f "vod-resampled.wav" -t 8 -osrt
    $WHISPER_EXECUTABLE \
    -m "${MODEL_PATH}" \
    -l "${WHISPER_LANG}" \
    -f "vod-resampled.wav" \
    -t 8 \
    -osrt \
    --translate

    msg "Embedding subtitle track..."

  4. Daniils Petrovs revised this gist Oct 19, 2022. No changes.
  5. Daniils Petrovs renamed this gist Oct 19, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion transcribe-vod.sh → whisper-transcribe.bash
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ cleanup() {
    }

    print_help() {
    echo "Usage: ./transcribe-vod <video_url>"
    echo "Usage: ./whisper-transcribe <video_url>"
    echo "This will produce an MP4 muxed file called res.mp4 in the working directory"
    echo "Requirements: ffmpeg yt-dlp whisper"
    echo "Whisper needs to be built into the main binary with make, then you can rename it to something like 'whisper' and add it to your PATH for convenience."
  6. Daniils Petrovs revised this gist Oct 17, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions transcribe-vod.sh
    Original file line number Diff line number Diff line change
    @@ -74,6 +74,8 @@ if [[ "$1" == "help" ]]; then
    exit 0
    fi

    check_requirements

    msg "Downloading VOD..."

    # Optionally add --cookies-from-browser BROWSER[+KEYRING][:PROFILE][::CONTAINER] for members only VODs
  7. Daniils Petrovs revised this gist Oct 17, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion transcribe-vod.sh
    Original file line number Diff line number Diff line change
    @@ -64,7 +64,7 @@ check_requirements() {
    fi

    if ! command -v "$WHISPER_EXECUTABLE" &>/dev/null; then
    echo "Whisper is required ()."
    echo "Whisper is required (https://github.com/ggerganov/whisper.cpp)."
    exit 1
    fi
    }
  8. Daniils Petrovs created this gist Oct 17, 2022.
    114 changes: 114 additions & 0 deletions transcribe-vod.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    #!/usr/bin/env bash

    # Small shell script to more easily automatically download and transcribe live stream VODs.
    # This uses YT-DLP, ffmpeg and the CPP version of Whisper: https://github.com/ggerganov/whisper.cpp
    # Use `./transcribe-vod help` to print help info.

    # MIT License

    # Copyright (c) 2022 Daniils Petrovs

    # Permission is hereby granted, free of charge, to any person obtaining a copy
    # of this software and associated documentation files (the "Software"), to deal
    # in the Software without restriction, including without limitation the rights
    # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    # copies of the Software, and to permit persons to whom the Software is
    # furnished to do so, subject to the following conditions:

    # The above copyright notice and this permission notice shall be included in all
    # copies or substantial portions of the Software.

    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    # SOFTWARE.

    set -Eeuo pipefail

    # You can find how to download models in the OG repo: https://github.com/ggerganov/whisper.cpp/#usage
    MODEL_PATH="${MODEL_PATH:-models/ggml-base.en.bin}"
    WHISPER_EXECUTABLE="${WHISPER_EXECUTABLE:-whisper}"

    temp_dir="tmp"
    source_url="$1"

    msg() {
    echo >&2 -e "${1-}"
    }

    cleanup() {
    msg "Cleaning up..."
    rm -rf "${temp_dir}" "vod-resampled.wav" "vod-resampled.wav.srt"
    }

    print_help() {
    echo "Usage: ./transcribe-vod <video_url>"
    echo "This will produce an MP4 muxed file called res.mp4 in the working directory"
    echo "Requirements: ffmpeg yt-dlp whisper"
    echo "Whisper needs to be built into the main binary with make, then you can rename it to something like 'whisper' and add it to your PATH for convenience."
    echo "E.g. in the root of Whisper.cpp, run: 'make && cp ./main /usr/local/bin/whisper'"
    }

    check_requirements() {
    if ! command -v ffmpeg &>/dev/null; then
    echo "ffmpeg is required (https://ffmpeg.org)."
    exit 1
    fi

    if ! command -v yt-dlp &>/dev/null; then
    echo "yt-dlp is required (https://github.com/yt-dlp/yt-dlp)."
    exit 1
    fi

    if ! command -v "$WHISPER_EXECUTABLE" &>/dev/null; then
    echo "Whisper is required ()."
    exit 1
    fi
    }

    if [[ "$1" == "help" ]]; then
    print_help
    exit 0
    fi

    msg "Downloading VOD..."

    # Optionally add --cookies-from-browser BROWSER[+KEYRING][:PROFILE][::CONTAINER] for members only VODs
    yt-dlp \
    -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" \
    --embed-thumbnail \
    --embed-chapters \
    --xattrs \
    "${source_url}" -o "${temp_dir}/vod.mp4"

    msg "Extracting audio and resampling..."

    ffmpeg -i "${temp_dir}/vod.mp4" \
    -hide_banner \
    -loglevel error \
    -ar 16000 \
    -ac 1 \
    -c:a \
    pcm_s16le -y "vod-resampled.wav"

    msg "Transcribing to subtitle file..."
    msg "Whisper specified at: ${WHISPER_EXECUTABLE}"

    $WHISPER_EXECUTABLE -m "${MODEL_PATH}" -f "vod-resampled.wav" -t 8 -osrt

    msg "Embedding subtitle track..."

    ffmpeg -i "${temp_dir}/vod.mp4" \
    -hide_banner \
    -loglevel error \
    -i "vod-resampled.wav.srt" \
    -c copy \
    -c:s mov_text \
    -y res.mp4

    cleanup

    msg "Done! Your finished file is ready: res.mp4"