update and test twitch clip downloading

This commit is contained in:
2026-07-20 12:42:52 -04:00
parent 0505d2a581
commit ff15ded0c9
9 changed files with 553 additions and 85 deletions
+39 -14
View File
@@ -1,23 +1,45 @@
import os
import subprocess
import whisper
from moviepy.editor import VideoFileClip
from moviepy import VideoFileClip
from whisper.utils import get_writer
def extract_audio(video_path, audio_temp_path):
print("Extracting uncompressed WAV audio...")
video = VideoFileClip(video_path)
# Extract as WAV, strictly setting the sample rate to 16000Hz for Whisper
video.audio.write_audiofile(
audio_temp_path,
codec="pcm_s16le",
ffmpeg_params=["-ar", "16000", "-ac", "1"]
)
video.close()
model = whisper.load_model("base")
def transcribe_to_srt(audio_path, output_directory, output_filename):
print("Loading Whisper model...")
model = whisper.load_model("base")
def extract_audio(video_path, audio_temp_path):
# Create a temporary path for a sanitized copy of the video
sanitized_video_path = video_path.replace(".mp4", "_clean.mp4")
print("Sanitizing video metadata for MoviePy parser...")
# -map_chapters -1 removes chapter layouts that break the parser.
# -sn strips text/subtitle streams that crash MoviePy.
# -c copy copies video and audio instantly without quality loss.
cleanup_cmd = [
"ffmpeg", "-y", "-i", video_path,
"-map_chapters", "-1", "-sn",
"-c", "copy", sanitized_video_path
]
# Run the sanitization process silently
subprocess.run(cleanup_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("Extracting uncompressed WAV audio...")
try:
# Load the sanitized file instead of the raw Twitch clip
with VideoFileClip(sanitized_video_path) as video:
video.audio.write_audiofile(
audio_temp_path,
fps=16000,
codec="pcm_s16le",
ffmpeg_params=["-ac", "1"]
)
finally:
# Always clean up the temporary sanitized video on Windows 11
if os.path.exists(sanitized_video_path):
os.remove(sanitized_video_path)
def transcribe_to_srt(audio_path, output_directory, output_filename):
print("Transcribing audio...")
result = model.transcribe(audio_path)
@@ -26,6 +48,9 @@ def transcribe_to_srt(audio_path, output_directory, output_filename):
srt_writer(result, output_filename, {})
print(f"SRT subtitle file saved in: {output_directory}")
if os.path.exists(audio_path):
os.remove(audio_path)
if __name__ == "__main__":
video_path = "my_video.mp4"