Hello there! I am using a script to generate subtitles for videos, it extracts the audio from the video and then subtitles it. My problem is the audio extraction, check this out:
This is the code that generates subtitles, it needs to be called and passed the route of the video as an argument:
import os
import argparse
from datetime import timedelta
from moviepy.editor import VideoFileClip
import whisper
from moviepy.config import change_settings
change_settings({"FFMPEG_THREADS": 5})
def extract_audio(video, temp_file):
if video.audio is not None:
video.audio.write_audiofile(temp_file, codec="mp3")
else:
raise ValueError("Video has no audio.")
def generate_subtitles(video_path, srt_name, transcribe_segments, temp_file):
video_dir, video_filename = os.path.split(video_path)
file_name, _ = os.path.splitext(video_filename)
srt_path = os.path.join(video_dir, srt_name)
print('[i] Generating SRT...')
with open(srt_path, "w", encoding="utf-8") as f:
for segment_id, seg in enumerate(transcribe_segments, start=1):
start = format_time(seg["start"])
end = format_time(seg["end"])
text = seg["text"].lstrip()
f.write(f"{segment_id}\n{start} --> {end}\n{text}\n\n")
try:
os.remove(temp_file)
except FileNotFoundError:
print(f"Warning: Temporary MP3 file '{temp_file}' could not be found for deletion.")
def format_time(seconds):
return str(timedelta(seconds=int(seconds))) + ",000"
def main():
parser = argparse.ArgumentParser(description="auto caption generator v1.0")
parser.add_argument("-f", "--video-path", type=str, required=True, help="Filepath of the video")
parser.add_argument("--model", choices=["tiny", "base", "small", "medium", "large"], default="tiny", help="Choose the whisper model size (default: tiny)")
args = parser.parse_args()
path = args.video_path
try:
validate_path(path)
video_manager = VideoFileClip(path)
file_name, _ = os.path.splitext(os.path.basename(path))
temp_file = f"{file_name}_temp_for_srt.mp3"
srt_name = f"{file_name}.srt"
with video_manager:
extract_audio(video_manager, temp_file)
model = whisper.load_model(args.model)
transcribe = model.transcribe(audio=temp_file, fp16=False)
print('[i] Transcription finished.')
generate_subtitles(path, srt_name, transcribe["segments"], temp_file)
except FileNotFoundError:
print(f"Error: Temporary file '{temp_file}' not found.")
except Exception as e:
print(f"Error: {e}")
def validate_path(path):
if not os.path.exists(path):
raise ValueError("Invalid file path, quitting")
if __name__ == "__main__":
main()
This code calls the script + the route of every video on a folder:
import os
import subprocess
from natsort import natsorted # Asegúrate de tener instalado este módulo: pip install natsort
def process_videos(video_folder):
videos = natsorted([file for file in os.listdir(video_folder) if file.endswith(".mp4")])
for video in videos:
video_path = os.path.join(video_folder, video)
command = f"python auto_caption_generator.py -f \"{video_path}\" --model small"
print(f"Processing video: {video}")
process = subprocess.Popen(command, shell=True)
process.communicate()
print(f"Finished processing {video}")
if __name__ == "__main__":
video_folder_path = "D:\BTO\MostlyToons\CRUD-VIDS-SubsEn" # Reemplaza con tu ruta de carpeta
process_videos(video_folder_path)
And this is the output:
Processing video: Batman T.A.S - S01 E25 - The Cape and Cowl Conspiracy (1080p - BluRay).mp4
MoviePy - Writing audio in Batman T.A.S - S01 E25 - The Cape and Cowl Conspiracy (1080p - BluRay)_temp_for_srt.mp3
chunk: 19%|█████████████████▍ | 5507/29715 [00:30<00:48, 498.47it/s, now=None]
It just gets stuck! Help Please, I am a begginer I do not get most of the things that happen over here!
Hello there! I am using a script to generate subtitles for videos, it extracts the audio from the video and then subtitles it. My problem is the audio extraction, check this out: This is the code that generates subtitles, it needs to be called and passed the route of the video as an argument:
This code calls the script + the route of every video on a folder:
And this is the output:
It just gets stuck! Help Please, I am a begginer I do not get most of the things that happen over here!