all repos — auto-movie-tagger @ 6c69cae65e0c0f64d63ce0d6807db1dea8b0c2a1

A Python script that auto tags and adds poster to mkv or mp4 movie files.

[Closes #7] Proper stream selection

The stream selection is now done depending whether or not the stream's codec is supported by mp4. Right now only dvd subtitles are not supported by mp4, So the script discards them. There might me other uncompatible codec types but I have only come across this till now
Prithu Goswami prithugoswami524@gmail.com
Thu, 22 Feb 2018 23:46:20 +0530
commit

6c69cae65e0c0f64d63ce0d6807db1dea8b0c2a1

parent

3beb82d8077da66f915880acff810a57fd2068a8

1 files changed, 42 insertions(+), 8 deletions(-)

jump to
M amt.pyamt.py

@@ -26,9 +26,24 @@ import urllib

import shlex import linecache import sys +from json import JSONDecoder import tmdbsimple as tmdb from imdbpie import Imdb from mutagen.mp4 import MP4, MP4Cover +import pprint + + +def collect_stream_metadata(filename): + command = 'ffprobe -i "{}" -show_streams -of json'.format(filename) + args = shlex.split(command) + p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True) + out, err = p.communicate() + + json_data = JSONDecoder().decode(out) + + return json_data + def PrintException():

@@ -110,6 +125,16 @@

for filename in filenames: try: title = filename[:-4] + + stream_md = collect_stream_metadata(filename) + streams_to_process = [] + dvdsub_exists=False + for stream in stream_md['streams']: + if not stream['codec_name'] == "dvdsub": + streams_to_process.append(stream['index']) + else: + dvdsub_exists=True + print('\nSearching IMDb for "{}"'.format(title)) imdb = Imdb()

@@ -181,34 +206,43 @@ .replace('/', ' ')

.replace('?', '')) command = "" + stream_map = [] + for f in streams_to_process: + stream_map.append("-map 0:{}".format(f)) + stream_map_str = ' '.join(stream_map) + + if mode == 1: # it is required to rename it as its already an mp4 file that # wasn't proccessed by ffmpeg os.rename(filename, newfilename) if mode == 2 or mode == 4: + command = ('ffmpeg -i "' + filename + '" -sub_charenc UTF-8 -i "' + filename[:-4] + '.srt" ' - + '-map 0 -map 1 -c copy -c:s mov_text ' - '-metadata:s:s:0 handler="English Subtitle" ' - '-metadata:s:s:0 language=eng ' + + stream_map_str + + ' -map 1 -c copy -c:s mov_text ' '-metadata:s:a:0 handler="" ' '-metadata:s:v:0 handler="" "' + newfilename + '"') subprocess.run(shlex.split(command)) if mode == 3: - command = ('ffmpeg -i "' - + filename - + '" -c copy -c:s mov_text ' - '-metadata:s:s:0 handler="English" ' - '-metadata:s:s:0 language=eng ' + command = ('ffmpeg -i ' + + '"' + filename + '" ' + + stream_map_str + + ' -c copy -c:s mov_text ' '-metadata:s:a:0 handler="" ' '-metadata:s:v:0 handler="" ' '"' + newfilename + '"') subprocess.run(shlex.split(command)) + + if dvdsub_exists: + print("\nRemoved DVD Subtitles due to uncompatibility with " + "mp4 file format") # The poster is fetched from tmdb only if there is no file # named " filename + '.jpg' " in the working directory