improve subtitle selection logic: can prefer hearing-impaired track or regular track

This commit is contained in:
2024-11-18 13:40:25 -08:00
parent 86bf99f81e
commit 5fec2c936d
2 changed files with 54 additions and 6 deletions

View File

@@ -8,7 +8,9 @@ a frontend to ffprobe that:
Usage: Usage:
pickstreams.py <file> <audio_lang> <sub_lang> pickstreams.py <file> <audio_lang> <sub_lang> <hi_preferred>
If audio_lang is left empty, all audio strrams are shown. If sub_lang is If audio_lang is left empty, all audio strrams are shown. If sub_lang is
left empty, all subtitle streams are shown. left empty, all subtitle streams are shown. If hi_preferred is set to "hi",
hearing-impaired subtitle tracks will be preferred; if left empty, regular
subtitle tracks will be preferred.

View File

@@ -6,6 +6,24 @@ import json
from pprint import pprint from pprint import pprint
try:
audio_lang=sys.argv[2]
except IndexError:
audio_lang=""
try:
sub_lang=sys.argv[3]
except IndexError:
sub_lang=""
try:
if sys.argv[4]=="hi":
prefer_hi=True
else:
prefer_hi=False
except IndexError:
prefer_hi=False
audio_info=""
sub_info=""
sub_hi_info=""
streams=json.loads(subprocess.run(["ffprobe", "-v", "error", "-show_streams", "-print_format", "json", sys.argv[1]], capture_output=True).stdout.decode("utf-8"))["streams"] streams=json.loads(subprocess.run(["ffprobe", "-v", "error", "-show_streams", "-print_format", "json", sys.argv[1]], capture_output=True).stdout.decode("utf-8"))["streams"]
for i in streams: for i in streams:
matched=True matched=True
@@ -19,9 +37,10 @@ for i in streams:
out=out+" "+str(int(i["tags"]["BPS"])/1000) out=out+" "+str(int(i["tags"]["BPS"])/1000)
except KeyError: except KeyError:
out=out+" unknown" out=out+" unknown"
print(out)
case "audio": case "audio":
try: try:
if sys.argv[2]!="" and sys.argv[2]!=i["tags"]["language"]: if audio_lang!="" and audio_lang!=i["tags"]["language"]:
matched=False matched=False
except: except:
pass pass
@@ -36,9 +55,14 @@ for i in streams:
out=out+" "+i["tags"]["language"] out=out+" "+i["tags"]["language"]
if i["disposition"]["default"]==1: if i["disposition"]["default"]==1:
out=out+" default" out=out+" default"
if matched==True:
audio_info=out
if audio_lang=="":
print(out)
case "subtitle": case "subtitle":
hi_flag=False
try: try:
if sys.argv[3]!="" and sys.argv[3]!=i["tags"]["language"]: if sub_lang!="" and sub_lang!=i["tags"]["language"]:
matched=False matched=False
except: except:
pass pass
@@ -47,12 +71,34 @@ for i in streams:
out=out+" default" out=out+" default"
if i["disposition"]["hearing_impaired"]==1: if i["disposition"]["hearing_impaired"]==1:
out=out+" hi" out=out+" hi"
if matched==True:
sub_hi_info=out
hi_flag=True
try: try:
if i["tags"]["title"].find("SDH")!=-1: if i["tags"]["title"].find("SDH")!=-1:
out=out+" hi" out=out+" hi"
if matched==True:
sub_hi_info=out
hi_flag=True
except KeyError: except KeyError:
pass pass
if hi_flag==False and matched==True:
sub_info=out
if sub_lang=="":
print(out)
case _: case _:
out=str(i["index"])+" "+i["codec_type"]+" "+i["codec_name"] out=str(i["index"])+" "+i["codec_type"]+" "+i["codec_name"]
if matched==True: if audio_info!="" and audio_lang!="":
print(out) print(audio_info)
if prefer_hi==False:
if sub_info!="" and sub_lang!="":
print(sub_info)
else:
if sub_hi_info!="" and sub_lang!="":
print(sub_hi_info)
else:
if sub_hi_info!="" and sub_lang!="":
print(sub_hi_info)
else:
if sub_info!="" and sub_lang!="":
print(sub_info)