Skip to content

Instantly share code, notes, and snippets.

@chapmanjacobd
Created September 30, 2022 02:43
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 chapmanjacobd/ece85d77def4fca0c876439e9b7f1bdc to your computer and use it in GitHub Desktop.
Save chapmanjacobd/ece85d77def4fca0c876439e9b7f1bdc to your computer and use it in GitHub Desktop.
def youtube_dl_id(file) -> str:
if len(file) < 15:
return ""
# rename old youtube_dl format to new one: cargo install renamer; fd -tf . -x renamer '\-([\w\-_]{11})\.= [$1].' {}
yt_id_regex = re.compile(r"-([\w\-_]{11})\..*$|\[([\w\-_]{11})\]\..*$", flags=re.M)
file = str(file).strip()
yt_ids = yt_id_regex.findall(file)
if not yt_ids:
return ""
return utils.conform([*yt_ids[0]])[0]
def has_internal_subtitle(file):
internal_sub = cmd(
f"ffmpeg -hide_banner -nostdin -i {quote(str(file))} -c copy -map 0:s:0 -frames:s 1 -f null - -v 0",
strict=False,
shell=True,
).returncode
if internal_sub == 0:
return True
def get(args, file) -> None:
try:
if has_internal_subtitle(file) or get_external(file):
return
except Exception:
pass
try:
yt_video_id = youtube_dl_id(file)
except Exception as e:
print(file)
print(e)
return
run_subliminal = not args.youtube_only
run_youtube = args.youtube_only # for new videos I already have yt-dlp get the subtitle
if run_youtube and yt_video_id:
print(yt_video_id)
cmd(
(
"yt-dlp --sub-lang 'en,EN,en.*,en-*,EN.*,EN-*eng,ENG,english,English,ENGLISH'"
" --embed-subs --compat-options no-keep-subs --write-sub --write-auto-sub"
" --no-download-archive --skip-download --limit-rate 10K"
f" https://youtu.be/{yt_video_id}"
),
cwd=str(Path(file).parent),
strict=False,
)
if run_subliminal:
print(f"[{file}] Downloading subtitles")
cmd(
"subliminal",
"--opensubtitles",
os.environ["OPEN_SUBTITLE_CREDENTIALS"],
"download",
"-l",
"en",
file,
# strict=False
)
def main() -> None:
parser = argparse.ArgumentParser(prog="library subtitle")
parser.add_argument("paths", nargs="*")
parser.add_argument("--youtube-only", action="store_true")
parser.add_argument("--subliminal-only", action="store_true")
parser.add_argument("--verbose", "-v", action="count", default=0)
args = parser.parse_args()
video_files = get_media_files(args)
Parallel(n_jobs=6 if args.verbose == 0 else 1)(delayed(get)(args, file) for file in video_files)
if __name__ == "__main__":
main()
@chapmanjacobd
Copy link
Author

chapmanjacobd commented Sep 30, 2022

def get_subtitle_text(ydl: yt_dlp.YoutubeDL, video_path, req_sub_dict) -> str:
    def dl_sub(url):
        temp_file = tempfile.mktemp(".srt", dir=SUB_TEMP_DIR)

        try:
            ydl.dl(temp_file, {"url": url}, subtitle=True)
        except HTTPError:
            log.info("Unable to download subtitles; skipping")
            sleep(5)
            return None

        return temp_file

    urls = [d["url"] for d in list(req_sub_dict.values())]
    paths = utils.conform([dl_sub(url) for url in urls])

    subs_text = subs_to_text(video_path, paths)
    for p in paths:
        utils.trash(p)

    return subs_text

I thought this code was working but it doesn't make sense that it should work. That said, I'm too lazy to test whether or not it actually works. I think it does. Feel free to try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment