abdeladim-s / subsai

🎞️ Subtitles generation tool (Web-UI + CLI + Python package) powered by OpenAI's Whisper and its variants 🎞️
https://abdeladim-s.github.io/subsai/
GNU General Public License v3.0
1.15k stars 96 forks source link

Not an issue, made a script to batch process videos #134

Closed secretlycarl closed 3 weeks ago

secretlycarl commented 4 weeks ago

I had trouble getting the CLI command working but the python method worked fine. I changed it to ask the user for a folder path and iterate through it and any subfolders.

import os
import logging
import time
from subsai import SubsAI

# Prompt user for the root folder path
root_folder = input("Enter the root folder path: ")

# Setup logging
logging.basicConfig(level=logging.INFO)

# Initialize SubsAI
subs_ai = SubsAI()
model = subs_ai.create_model('openai/whisper', {'model_type': 'base'})

# Function to process each video file
def process_video_file(file_path):
    start_time = time.time()
    logging.info(f"Processing video file: {file_path}")
    subs = subs_ai.transcribe(file_path, model)
    subtitle_path = os.path.splitext(file_path)[0] + '.srt'
    subs.save(subtitle_path)
    end_time = time.time()
    processing_time = end_time - start_time
    logging.info(f"Subtitle saved to: {subtitle_path}")
    logging.info(f"Processing time: {processing_time:.2f} seconds")

# Walk through the directory and process each video file
for root, dirs, files in os.walk(root_folder):
    for file in files:
        if file.lower().endswith(('.mp4', '.mkv', '.avi', '.mov')):
            file_path = os.path.join(root, file)
            process_video_file(file_path)
abdeladim-s commented 4 weeks ago

@secretlycarl, The CLI takes a txt file containing the absolute file of the media files, so it won't work for your use case. But this is a good contribution, if you can add this script to the examples folder I'll be more than happy to merge it.

secretlycarl commented 3 weeks ago

@abdeladim-s not too familiar with how github works with editing someone else's repo, tried to add file but i didnt have permissions. i've attached it subsai_batch.txt

abdeladim-s commented 3 weeks ago

No worries, It's OK in here as well. Thanks @secretlycarl.