auto-subtitle icon indicating copy to clipboard operation
auto-subtitle copied to clipboard

running auto-subtitle on multiple files

Open callista13 opened this issue 2 years ago • 3 comments

Hi, how can I run auto-subtitle on multiple files from the same folder? I tried setting /folder/*.mp4 but getting an error.

Also, would it be possible to extract subtitle only from audio along with the translation ie. producing 2 srts in one run?

Thanks.

callista13 avatar Jan 31 '23 16:01 callista13

Hey, I know it's been a while since you asked. I'm not good with shell scripts so I wrote a little python script to automate creating multiple subtitles for me. Maybe it will be of use to you as well:

import os
import subprocess

def create_video_list(path, keyword, count, language):
    video_list = {}
    supported_formats = (".mp4", ".mkv", ".mov")
    for root, _, files in os.walk(path):
        for file in files:
            subs_file = f"{os.path.splitext(file)[0]}.srt"
            #check for keyword, supported format and duplicates
            if keyword.lower() in file.lower() and file.endswith(supported_formats) and not os.path.isfile(os.path.join(root, subs_file)):
                file_path  = os.path.join(root, file)
                if len(video_list) < count:
                    video_list[file_path.replace("\\", "/")] = root.replace("\\", "/")
                else:
                    break
    if len(video_list) == 0:
        print(f"No videos containing {keyword} found in {path}.")
        return
    else:
        print(f"{len(video_list)} video(s) added to list:")
        print(f"{video_list.keys()}")
        print("Proceeding with subtitle generation:")
        create_auto_subtitle(video_list, language)
        return
    
def create_auto_subtitle(video_list, language):
    videos = video_list.items()
    for video, root in videos:
        command = f"auto_subtitle \"{video}\" --model medium --task translate -o \"{root}\" --srt_only True --language {language}"
        subprocess.run(command, shell=True)
        print(f"Created subtitles for {video}")
    print("Finished tasks, python")
    return


def prompt_params(parameters):
	for param_name, def_value in parameters.items():
		user_value = input(f"Enter {param_name} (default {def_value}): ")
		if user_value:
			if param_name == "count":
				parameters[param_name] = int(user_value)
			else:
				parameters[param_name] = user_value
	create_video_list(**parameters)
	
# Example Use:
	
parameters = {
	"path": ".",
	"keyword": "",
	"count": 1000,
	"language": "auto"
}

# run

prompt_params(parameters)

NB:

  • It looks for a specified amount of video files in a path, recursively and is able to check for specific files that contain keywords (leave empty for no filter).
  • It is intended for use with Windows, I haven't tried it on a linux machine yet. But since I used forward-slashes '/' for paths it should work
  • It only creates .srt files. and is set to translate. You might want to adjust the script accordingly by modifying the command variable (l.30). The .srt files are created in the same directory in which the file is found. I use mpv, so it automatically recognizes them.
  • It checks for duplicate .srt files, if you make it create overlayed videos you might have to make it check for duplicate videos instead, look at l.9 for that
  • I'm a total noob at coding, I've only created this with very limited knowledge and googling.

Kaiton121 avatar Jun 19 '23 18:06 Kaiton121

If you use shell, you can use find command, find . -name '*.mp4' -exec auto_subtitle --model medium --srt_only true --output_srt true --task transcribe {} \;

cary-sas avatar Jun 29 '23 03:06 cary-sas

You can use the "find" command as cary-sas pointed out, and also filter out videos that already have an embedded subtitle or an existing SRT file (with the same filename but with srt extension) using the following (e.g. with medium model, translating from Chinese):

find ./ -type f \( -iname "*.mkv" -o -iname "*.webm" -o -iname "*.mp4" \) -exec sh -c 'for file; do [ ! -f "${file%.*}.srt" ] && ! ffprobe -hide_banner -loglevel panic -show_streams "$file" | grep -qE "codec_type=subtitle" && auto_subtitle --model medium --output_srt true --srt_only true --task translate --language zh "$file"; done' _ {} \+

mkowen1 avatar Aug 20 '23 18:08 mkowen1