New functions to extract subtitles at the end of processing. A new option to extract them.

This commit is contained in:
Frédéric Tronel
2023-12-01 16:48:01 +01:00
parent cf4850c8dc
commit 0f46dc9fda

View File

@@ -666,8 +666,30 @@ def mergeMKVs(inputs, outputName):
return out
def findSubtitlesTracks(filename):
# ffprobe -loglevel quiet -select_streams s -show_entries stream=index:stream_tags=language -of json corgi.ts
logger = logging.getLogger(__name__)
with Popen(['ffprobe', '-i', filename, '-select_streams', 's', '-show_entries', 'stream=index:stream_tags=language', '-of', 'json'], stdout=PIPE, close_fds=False) as ffprobe:
out, _ = ffprobe.communicate()
out = json.load(BytesIO(out))
if 'streams' in out:
return out['streams']
else:
logger.error('Impossible to retrieve format of file')
pass
def extractSubTitleTrack(inputFileName, index, lang):
# mkvextract video.mkv tracks position:nom [position:nom]
logger = logging.getLogger(__name__)
with Popen(['mkvextract', inputFileName, 'tracks', '%d:%s' % (index,lang)], stdout=PIPE, close_fds=False) as mkvextract:
out, _ = mkvextract.communicate()
for lines in out:
logger.info(out)
def main():
logger = logging.getLogger(__name__)
coloredlogs.install()
@@ -677,7 +699,8 @@ def main():
parser.add_argument("-o", "--output", dest='outputFile', type=str, required=True, help="Output MKV file to produce.")
parser.add_argument("-p", "--part", dest='parts', nargs='+', required=False, action='append', metavar="hh:mm:ss[.mmm]-hh:mm:ss[.mmm]", help="Extract this exact part of the original file.")
parser.add_argument("-k", "--keep", action='store_true', help="Do not cleanup temporary files after processing.")
parser.add_argument("--dump-pictures", action='store_true', dest='dump', help="For debug purpose, dump pictures of headers (and trailers) before (after) each part. They are kept in memory only otherwise.")
parser.add_argument("--dump-pictures", action='store_true', dest='dump', help="For debug purpose, dump pictures of headers (and trailers) before (after) each part. They are kept in memory only otherwise.")
parser.add_argument("-s","--srt", action='store_true', dest='srt', help="Dump subtitles ")
args = parser.parse_args()
logger.debug("Arguments: %s" % args)
@@ -862,6 +885,38 @@ def main():
else:
logger.info("Nothing else to do.")
if args.srt:
logger.info("Find subtitles tracks and language.")
subtitles = findSubtitlesTracks(args.outputFile)
sts = {}
for subtitle in subtitles:
index = subtitle['index']
if 'tags' in subtitle:
if 'language' in subtitle['tags']:
lang = subtitle['tags']['language']
if lang in sts:
sts[lang].append(index)
else:
sts[lang] = [index]
else:
logger.error("Dropping subtitle: %s because it is missing language indication")
else:
logger.error("Dropping subtitle: %s because it is missing language indication")
for lang in sts:
indexes = sts[lang]
if len(indexes) == 0:
# Nothing to do. This should not happen.
continue
if len(indexes) == 1:
index = indexes[0]
filename = 'essai-%s.srt' % lang
elif len(indexes) > 1:
nbsrt = 1
for index in indexes:
filename = 'essai-%s-%d.srt' % (lang, nbsrt)
nbsrt+=1
if not args.keep:
logger.info("Cleaning temporary files")
for f in temporaries: