Skip to content

Instantly share code, notes, and snippets.

@miguelgargallo
Forked from midudev/README.md
Created January 11, 2023 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelgargallo/b493a10f579b094e1ebb1e5d76f0de74 to your computer and use it in GitHub Desktop.
Save miguelgargallo/b493a10f579b094e1ebb1e5d76f0de74 to your computer and use it in GitHub Desktop.
Transcribe vídeo de YouTube con Whisper e Inteligencia Artificial

Requisitos

Necesitas tener instalado Python 3.9 e instalar la dependencia de Whisper y PyTube:

pip install git+https://github.com/openai/whisper.git
pip install pytube

También necesitas tener instalado ffmpeg. Según tu sistema operativo se instala de esta forma:

# Ubuntu
sudo apt update && sudo apt install ffmpeg
# Arch Linux
sudo pacman -S ffmpeg
#  MacOS con Homebrew (https://brew.sh/)
brew install ffmpeg
# Windows con Chocolatey (https://chocolatey.org/)
choco install ffmpeg
# Windows con Scoop (https://scoop.sh/)
scoop install ffmpeg

Cómo usar la línea de comandos

Necesitas indicar la URL del vídeo de YouTube que quieres transcribir:

python3 transcript.py -h

python3 transcript.py --video "https://www.youtube.com/watch?v=oHrjAbDanpw"

# también puedes indicar el modelo de IA que usará Whisper
# cuanto más grande, más tardará en descargarlo la primera vez
python3 transcript.py --video "https://www.youtube.com/watch?v=oHrjAbDanpw" --model "large"
import logging
import pytube
import whisper
import sys
import argparse
parser = argparse.ArgumentParser(description='Transcript a YouTube video using Whisper')
parser.add_argument("--video", help = "Pass the YouTube url to transcribe")
parser.add_argument("--model", help = "Indicate the Whisper model to download", default="small")
args = parser.parse_args()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.StreamHandler(sys.stdout)
]
)
if not args.video:
logging.error("Please pass a YouTube url to transcribe")
exit()
logging.info("Downloading Whisper model")
model = whisper.load_model(args.model)
logging.info("Downloading the video from YouTube...")
youtubeVideo = pytube.YouTube(args.video)
logging.info("Get only the audio from the video")
audio = youtubeVideo.streams.get_audio_only()
audio.download(filename='tmp.mp4')
logging.info("Transcribe the audio")
result = model.transcribe('tmp.mp4')
print(result["text"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment