diff --git a/vid_convert.py b/vid_convert.py index ac3717c..ca0eb71 100755 --- a/vid_convert.py +++ b/vid_convert.py @@ -28,8 +28,11 @@ def get_infos(file): v_infos_raw = subprocess.getoutput(v_infos_cmd) a_infos_cmd = f"ffprobe -v quiet -print_format json -show_format -show_streams -select_streams a {file}" a_infos_raw = subprocess.getoutput(a_infos_cmd) + s_infos_cmd = f"ffprobe -v quiet -print_format json -show_streams -select_streams s {file}" + s_infos_raw = subprocess.getoutput(s_infos_cmd) full_v_infos = json.loads(v_infos_raw) full_a_infos = json.loads(a_infos_raw) + full_s_infos = json.loads(s_infos_raw) v_stream = full_v_infos['streams'][0] for prop in v_infos.keys(): try: @@ -49,6 +52,12 @@ def get_infos(file): 'language': a_stream['tags']['language'], 'title': a_stream['tags']['title']} a_infos.append(a_stream_infos) + s_infos = [] + for s_stream in full_s_infos['streams']: + s_stream_infos = { + 'index': s_stream['index'], + 'language': s_stream['tagsd']['language']} + s_infos.append(s_stream_infos) duration = subprocess.getoutput(f"ffprobe -v quiet -print_format json -show_format {file}") duration = json.loads(duration) duration = float(duration['format']['duration']) @@ -58,7 +67,7 @@ def get_infos(file): if 'metadata detected' in hdr10_v_raw: hdr10_cmd = f'ffmpeg -loglevel panic -i {file} -c:v copy -vbsf hevc_mp4toannexb -f hevc - | hdr10plus_parser -o /tmp/{file}_hdr10_metadata.json -' v_infos.update({'hdr10': True, 'hdr10_metdata': f'/tmp/{file}_hdr10_metadata.json'}) - infos = {'duration': duration, 'video': v_infos, 'audio': a_infos} + infos = {'duration': duration, 'video': v_infos, 'audio': a_infos, 'subtitles': s_infos} logging.debug("Informations du film : \n" + json.dumps(infos, indent=True)) return infos @@ -128,13 +137,20 @@ def stabilization(file): subprocess.getoutput(cmd_stab) +def extract_subs(file, track, lang): + command = f'ffmpeg -loglevel error -i {file} -map 0:{track} -map_metadata -1 -vn -an -c:s copy -metadata language={lang} -y {file}_subtitle_{track}_{lang}.mkv' + logging.debug(command) + result = subprocess.getoutput(command) + logging.info(result) + + def convert_audio(file, track, volume_adj, channels, channel_layout, language, title): bitrate = f'{64*channels}k' if channel_layout == "5.1(side)": channel_layout = "5.1" codec = 'libopus' metadatas = f'-metadata language="{language}" -metadata title="{title}"' - command = f'ffmpeg -loglevel error -i {file} -map 0:{track} -vn -sn -c:a {codec} -b:a {bitrate} -mapping_family 1 -filter:a volume={volume_adj},aformat=channel_layouts={channel_layout} -y {file}_audio_{track}.mka' + command = f'ffmpeg -loglevel error -i {file} -map 0:{track} -map_metadata -1 -vn -sn -c:a {codec} -b:a {bitrate} -mapping_family 1 -filter:a volume={volume_adj},aformat=channel_layouts={channel_layout} {metadata} -y {file}_audio_{track}.mka' logging.debug(command) result = subprocess.getoutput(command) logging.info(result) @@ -208,6 +224,8 @@ if __name__ == '__main__': if args.stab: stabilization(file) if not args.starttime: + for track in infos['subtitles']: + extract_subs(file, track['index'], track['language']) for track in infos['audio']: convert_audio(file, track['index'], volumes[track['index']], track['channels'], track['channel_layout'], track['language'], track['title']) if args.starttime: @@ -218,3 +236,7 @@ if __name__ == '__main__': crf = 19 convert_video(file, infos, vid_part_time, cropsize, crf) vid_part_time += 300 + + +# Reste mkvmerge, e.g. : +# /usr/bin/mkvmerge --ui-language fr_FR --output /cifs/nas/DVD-BR/ICE_AGE_2.mkv --no-track-tags --no-global-tags --no-chapters --language 0:en --display-dimensions 0:1920x1024 ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t00000.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t00300.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t00600.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t00900.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t01200.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t01500.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t01800.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t02100.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t02400.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t02700.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t03000.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t03300.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t03600.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t03900.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t04200.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t04500.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t04800.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t05100.mkv ) --no-track-tags --no-global-tags --no-chapters + ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_video_t05400.mkv ) --no-track-tags --no-global-tags --language 0:fr --track-name 0:Surround 5.1 ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_audio_1.mka ) --no-track-tags --no-global-tags --no-chapters --language 0:en --track-name 0:Surround 5.1 --default-track 0:no ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_audio_2.mka ) --no-track-tags --no-global-tags --no-chapters --language 0:fr --default-track 0:no ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_subtitle_4.mkv ) --no-global-tags --no-chapters --language 0:en --default-track 0:no ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_subtitle_3.mkv ) --no-track-tags --no-global-tags --no-chapters --language 0:fr --default-track 0:no ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_subtitle_5.mkv ) --no-track-tags --no-global-tags --no-chapters --language 0:fr --default-track 0:no ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_subtitle_6.mkv ) --no-track-tags --no-global-tags --no-chapters --language 0:en --default-track 0:no ( /cifs/nas/DVD-BR/ICE_AGE_2/ICE_AGE_2.mkv_subtitle_7.mkv ) --title ICE_AGE_2 --track-order 0:0,19:0,20:0,21:0,22:0,23:0,24:0,25:0 --append-to 1:0:0:0,2:0:1:0,3:0:2:0,4:0:3:0,5:0:4:0,6:0:5:0,7:0:6:0,8:0:7:0,9:0:8:0,10:0:9:0,11:0:10:0,12:0:11:0,13:0:12:0,14:0:13:0,15:0:14:0,16:0:15:0,17:0:16:0,18:0:17:0