Skip to content

Instantly share code, notes, and snippets.

@rasbt
Last active March 20, 2025 13:55
Show Gist options
  • Save rasbt/0d09932c861851f177bd8f13dc93b354 to your computer and use it in GitHub Desktop.
Save rasbt/0d09932c861851f177bd8f13dc93b354 to your computer and use it in GitHub Desktop.

Revisions

  1. Sebastian Raschka revised this gist Nov 30, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion video-subtitles-via-whisper.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    # macOS & homebrew
    # brew install ffmpeg
    # Ubuntu
    # sudo apt-get ffmpeg
    # sudo apt-get install ffmpeg

    # Install whisper
    # from repo https://github.com/openai/whisper
  2. Sebastian Raschka created this gist Sep 25, 2022.
    46 changes: 46 additions & 0 deletions video-subtitles-via-whisper.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    # Sebastian Raschka 09/24/2022
    # Create a new conda environment and packages
    # conda create -n whisper python=3.9
    # conda activate whisper
    # conda install mlxtend -c conda-forge

    # Install ffmpeg
    # macOS & homebrew
    # brew install ffmpeg
    # Ubuntu
    # sudo apt-get ffmpeg

    # Install whisper
    # from repo https://github.com/openai/whisper
    # pip install git+https://github.com/openai/whisper.git

    import os
    import os.path as osp
    from mlxtend.file_io import find_files
    from mlxtend.utils import Counter


    all_videos = find_files(substring=".mp4", path="./", recursive=True)
    print("Example path:", all_videos[0])
    print("Number of videos to process:", len(all_videos))

    audio_outdir = "./extracted_audio"
    subtitle_outdir = "./generated_subtitles"

    for this_dir in (audio_outdir, subtitle_outdir):
    if not osp.exists(this_dir):
    os.mkdir(this_dir)

    cnt = Counter()
    for v in all_videos:

    base, ext = osp.splitext(v)
    aac_file_out = osp.join(audio_outdir, osp.basename(base)) + ".aac"

    # exctract audio file from video
    os.system(f"ffmpeg -i {v} -vn -acodec copy {aac_file_out} ")

    os.system(
    f"whisper {aac_file_out} --model medium --language English --output_dir {subtitle_outdir} --verbose False"
    )
    cnt.update()