all repos — auto-movie-tagger @ 184800c6d3e6312304e4677860663a5b90a39918

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

Merge pull request #8 from prithugoswami/iss7

[Closes #7] Proper stream selection
Prithu Goswami prithugoswami524@gmail.com
Fri, 23 Feb 2018 08:32:53 +0530
commit

184800c6d3e6312304e4677860663a5b90a39918

parent

3beb82d8077da66f915880acff810a57fd2068a8

1 files changed, 44 insertions(+), 22 deletions(-)

jump to
M amt.pyamt.py

@@ -10,15 +10,7 @@ this is my first, or maybe second python script.

TO-DO 1. Add a way to notify when the script is done running -2. Seems a little too much for now but I could change/add stream specific -metadata according to the file -For example depending upon what kind of audio the mkv file has (like AAC 5.1, -DTS 5.1), the script can also change the title/handler of the audio stream -right now it just blanks it out. -The same can be done for subtitles depending upon language. This can also be -implemented to discard dvd subtitles that are sometimes found -in MKVs but are not supported by MP4 -3. Add proper error handleling for ffmpeg +2. Add proper error handleling for ffmpeg """ import os import subprocess

@@ -26,9 +18,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 +117,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 +198,39 @@ .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 ' - '-metadata:s:a:0 handler="" ' - '-metadata:s:v:0 handler="" "' - + newfilename + '"') + + stream_map_str + + ' -map 1 -c copy -c:s mov_text ' + '"' + 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 ' - '-metadata:s:a:0 handler="" ' - '-metadata:s:v:0 handler="" ' + command = ('ffmpeg -i ' + + '"' + filename + '" ' + + stream_map_str + + ' -c copy -c:s mov_text ' '"' + 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