diff --git a/main.py b/main.py index af84929..3dce6fe 100644 --- a/main.py +++ b/main.py @@ -17,6 +17,9 @@ import filerule from ftplib import FTP import time import file +import shlex +import subprocess +import json def ftpwalk(directory, ftp): @@ -228,7 +231,9 @@ def handle_films(Lfilm_loc, Lfilm_api, domain, api, rules, tok): try: resp = api.post_file(path=domain['server'] + film.path, name=film.name, type='film', type_id=filmID) if 'id' in resp: + probe_data = probe_file(film, domain) post_markers(api, film, resp['id']) + post_probe(api, resp['id'], probe_data) time.sleep(1) except Exception as e: print(e) @@ -244,7 +249,9 @@ def handle_films(Lfilm_loc, Lfilm_api, domain, api, rules, tok): resp = api.post_file(path=domain['server'] + film.path, name=film.name, type='film', type_id=filmID, **film.additional_info()) if 'id' in resp: + probe_data = probe_file(film, domain) post_markers(api, film, resp['id']) + post_probe(api, resp['id'], probe_data) time.sleep(1) except Exception as e: print(e) @@ -272,7 +279,9 @@ def handle_films(Lfilm_loc, Lfilm_api, domain, api, rules, tok): print('post: path=', domain['server'] + film.path) resp = api.post_file(path=domain['server'] + film.path, name=film.name, type='film', type_id=resp["id"]) if 'id' in resp: # id du file + probe_data = probe_file(film, domain) post_markers(api, film, resp['id']) + post_probe(api, resp['id'], probe_data) else: Lcannot_post.append(film) @@ -390,6 +399,9 @@ def handle_episodes(Lepisode_loc, Lepisode_api, domain, api, rules, tok): resp = api.post_file(path=domain['server'] + episode.path, name=episode.name, type='episode', type_id=resp["id"]) print('response: ', resp) + if 'id' in resp: + probe_data = probe_file(episode, domain) + post_probe(api, resp['id'], probe_data) else: print('episode not posted:', resp) except Exception as e: @@ -446,6 +458,57 @@ def find_by_common(L1, L2, f_prop): break return found +def probe_file(file_, domain): + filename = domain['server'].replace('ftp://', 'ftp://'+domain['username']+':'+domain['password']+'@')+file_.path+'/'+file_.name + print("probe:", filename) + try: + output = subprocess.check_output("ffprobe -v quiet -show_format -show_streams "+shlex.quote(filename)+" -print_format json", shell=True) + except: + print('invalid file:', file_.name) + return {'sub':[], 'audio':[], 'video':[]} + outp = json.loads(output.decode('utf-8')) + #print('got output:', outp) + container = outp['format']['format_name'] + streams = outp['streams'] + streaminfo = {'sub':[], 'audio':[], 'video':[], 'container':container} + for s in streams: + if s['codec_type'] == 'subtitle': + if 'tags' in s: + if 'language' in s['tags']: + streaminfo['sub'].append(s['tags']['language']) + elif 'LANGUAGE' in s['tags']: + streaminfo['sub'].append(s['tags']['LANGUAGE']) + elif s['codec_type'] == 'audio': + if 'tags' in s: + if 'language' in s['tags']: + streaminfo['audio'].append({'lang':s['tags']['language'], 'name':s['codec_name']}) + elif 'LANGUAGE' in s['tags']: + streaminfo['audio'].append({'lang':s['tags']['LANGUAGE'], 'name':s['codec_name']}) + else: + streaminfo['audio'].append({'name':s['codec_name']}) + elif s['codec_type'] == 'video': + if s['codec_name'] != 'mjpeg': + streaminfo['video'].append({'name':s['codec_name'], 'width':s['width'], 'height':s['height']}) + return streaminfo + +def post_probe(api, fileid, probe_data): + name1 = None + for codec in probe_data['audio']: + if not name1: + name1 = codec['name'] + elif name1 != codec['name']: + print('different audio codecs for file:', fileid) + api.put_audio_codec(fileid, codec['name']) + time.sleep(1) + if 'container' in probe_data: + api.put_audio_container(fileid, probe_data['container']) + time.sleep(1) + if len(probe_data['video']) > 0: + api.put_video_codec(fileid, probe_data['video'][0]['name']) + time.sleep(1) + if 'container' in probe_data: + api.put_video_container(fileid, probe_data['container']) + time.sleep(1) def post_markers(api, file_, fileid): """