New functions to extract subtitles at the end of processing. A new option to extract them.
This commit is contained in:
55
removeads.py
55
removeads.py
@@ -666,6 +666,28 @@ def mergeMKVs(inputs, outputName):
|
|||||||
|
|
||||||
return out
|
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():
|
def main():
|
||||||
@@ -678,6 +700,7 @@ def main():
|
|||||||
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("-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("-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()
|
args = parser.parse_args()
|
||||||
logger.debug("Arguments: %s" % args)
|
logger.debug("Arguments: %s" % args)
|
||||||
@@ -862,6 +885,38 @@ def main():
|
|||||||
else:
|
else:
|
||||||
logger.info("Nothing else to do.")
|
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:
|
if not args.keep:
|
||||||
logger.info("Cleaning temporary files")
|
logger.info("Cleaning temporary files")
|
||||||
for f in temporaries:
|
for f in temporaries:
|
||||||
|
|||||||
Reference in New Issue
Block a user