diff --git a/vid_convert.py b/vid_convert.py index 874ec42..cad923a 100755 --- a/vid_convert.py +++ b/vid_convert.py @@ -52,11 +52,11 @@ def get_infos(file): duration = subprocess.getoutput(f"ffprobe -v quiet -print_format json -show_format {file}") duration = json.loads(duration) duration = float(duration['format']['duration']) - hdr10_v_cmd = f'ffmpeg -loglevel panic -i {file} -c:v copy -vbsf hevc_mp4toannexb -f hevc - | ./hdr10plus_parser -o metadata.json --verify -' + hdr10_v_cmd = f'ffmpeg -loglevel panic -i {file} -c:v copy -vbsf hevc_mp4toannexb -f hevc - | hdr10plus_parser -o metadata.json --verify -' hdr10_v_raw = subprocess.getoutput(hdr10_v_cmd) - logging.debug(hdr10_v_raw) + # logging.debug(hdr10_v_raw) 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 -' + 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} logging.debug("Informations du film : \n" + json.dumps(infos, indent=True)) @@ -93,6 +93,7 @@ def cropping(file, infos): ''' Cette fonction detecte les bandes inutiles de la vidéo ''' + logging.debug("Détection de la taille de l'image...") duration_tier = int(infos['duration'] / 3) command = f"ffmpeg -loglevel info -i {file} -ss {duration_tier} -t {duration_tier} -an -f null -vf cropdetect -y /dev/null" cropsize = subprocess.getoutput(command).splitlines()[-3].split()[-1] @@ -129,6 +130,8 @@ def stabilization(file): 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' @@ -138,47 +141,49 @@ def convert_audio(file, track, volume_adj, channels, channel_layout, language, t def convert_video(file, infos, start, crop, crf): - output = f'{file}_video_t{start}.mkv' + str_start = "{:05d}".format(start) + output = f'{file}_video_t{str_start}.mkv' fmt = infos['video']['pix_fmt'] track = infos['video']['index'] - codec = 'libx265 -crf {crf} -preset slower' + codec = 'libx265 -preset slow' + hdr = '' if 'side_data_list' in infos['video'].keys(): - light_level = f"{infos['video']['side_data_list'][1]['max_content']},{infos['video']['side_data_list'][1]['max_average']}" - color_primaries = infos['video']['color_primaries'] - color_transfer = infos['video']['color_transfer'] - color_space = infos['video']['color_space'] - green_x = infos['video']['side_data_list'][0]['green_x'].split('/') - green_x = int(int(green_x[0])*(int(green_x[1])/50000)) - green_y = infos['video']['side_data_list'][0]['green_y'].split('/') - green_y = int(int(green_y[0])*(int(green_y[1])/50000)) - green = f'G({green_x},{green_y})' - blue_x = infos['video']['side_data_list'][0]['blue_x'].split('/') - blue_x = int(int(blue_x[0])*(int(blue_x[1])/50000)) - blue_y = infos['video']['side_data_list'][0]['blue_y'].split('/') - blue_y = int(int(blue_y[0])*(int(blue_y[1])/50000)) - blue = f'B({blue_x},{blue_y})' - red_x = infos['video']['side_data_list'][0]['red_x'].split('/') - red_x = int(int(red_x[0])*(int(red_x[1])/50000)) - red_y = infos['video']['side_data_list'][0]['red_y'].split('/') - red_y = int(int(red_y[0])*(int(red_y[1])/50000)) - red = f'R({red_x},{red_y})' - white_point_x = infos['video']['side_data_list'][0]['white_point_x'].split('/') - white_point_x = int(int(white_point_x[0])*(int(white_point_x[1])/50000)) - white_point_y = infos['video']['side_data_list'][0]['white_point_y'].split('/') - white_point_y = int(int(white_point_y[0])*(int(white_point_y[1])/50000)) - white_point = f'WP({white_point_x},{white_point_y})' - min_luminance = infos['video']['side_data_list'][0]['min_luminance'].split('/') - min_luminance = int(int(min_luminance[0])*(int(min_luminance[1])/10000)) - max_luminance = infos['video']['side_data_list'][0]['max_luminance'].split('/') - max_luminance = int(int(max_luminance[0])*(int(max_luminance[1])/10000)) - luminance = f'L({max_luminance},{min_luminance})' - master_display = green + blue + red + white_point + luminance - hdr = f'-x265-params hdr-opt=1:repeat-headers=1:colorprim={color_primaries}:transfer={color_transfer}:colormatrix={color_space}:master-display={master_display}:max-cll={light_level}' - else: - hdr = "" + try: + light_level = f"{infos['video']['side_data_list'][1]['max_content']},{infos['video']['side_data_list'][1]['max_average']}" + color_primaries = infos['video']['color_primaries'] + color_transfer = infos['video']['color_transfer'] + color_space = infos['video']['color_space'] + green_x = infos['video']['side_data_list'][0]['green_x'].split('/') + green_x = int(int(green_x[0])*(int(green_x[1])/50000)) + green_y = infos['video']['side_data_list'][0]['green_y'].split('/') + green_y = int(int(green_y[0])*(int(green_y[1])/50000)) + green = f'G\({green_x},{green_y}\)' + blue_x = infos['video']['side_data_list'][0]['blue_x'].split('/') + blue_x = int(int(blue_x[0])*(int(blue_x[1])/50000)) + blue_y = infos['video']['side_data_list'][0]['blue_y'].split('/') + blue_y = int(int(blue_y[0])*(int(blue_y[1])/50000)) + blue = f'B\({blue_x},{blue_y}\)' + red_x = infos['video']['side_data_list'][0]['red_x'].split('/') + red_x = int(int(red_x[0])*(int(red_x[1])/50000)) + red_y = infos['video']['side_data_list'][0]['red_y'].split('/') + red_y = int(int(red_y[0])*(int(red_y[1])/50000)) + red = f'R\({red_x},{red_y}\)' + white_point_x = infos['video']['side_data_list'][0]['white_point_x'].split('/') + white_point_x = int(int(white_point_x[0])*(int(white_point_x[1])/50000)) + white_point_y = infos['video']['side_data_list'][0]['white_point_y'].split('/') + white_point_y = int(int(white_point_y[0])*(int(white_point_y[1])/50000)) + white_point = f'WP\({white_point_x},{white_point_y}\)' + min_luminance = infos['video']['side_data_list'][0]['min_luminance'].split('/') + min_luminance = int(int(min_luminance[0])*(int(min_luminance[1])/10000)) + max_luminance = infos['video']['side_data_list'][0]['max_luminance'].split('/') + max_luminance = int(int(max_luminance[0])*(int(max_luminance[1])/10000)) + luminance = f'L\({max_luminance},{min_luminance}\)' + master_display = green + blue + red + white_point + luminance + hdr = f'-x265-params hdr-opt=1:repeat-headers=1:colorprim={color_primaries}:transfer={color_transfer}:colormatrix={color_space}:master-display={master_display}:max-cll={light_level}' + except: + logging.debug("Aucune information HDR") command = f'ffmpeg -loglevel error -i {file} -map 0:{track} -ss {start} -t 300 -an -sn -c:v {codec} {hdr} -crf {crf} -pix_fmt {fmt} -filter:v {crop} -y {output}' logging.debug(command) - import pdb; pdb.set_trace() result = subprocess.getoutput(command) logging.info(result) @@ -189,6 +194,7 @@ if __name__ == '__main__': parser.add_argument("f_input") parser.add_argument("-d", "--debug", dest="debug", action="store_true") parser.add_argument("-s", "--stabilise", dest="stab", action="store_true") + parser.add_argument("-t", "--starttime", dest="starttime") args = parser.parse_args() if args.debug: logging.basicConfig(format='[%(asctime)s]\n%(message)s', level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S') @@ -196,15 +202,19 @@ if __name__ == '__main__': logging.basicConfig(format='[%(asctime)s]\n%(message)s', level=logging.INFO, datefmt='%d/%m/%Y %H:%M:%S') file = args.f_input infos = get_infos(file) - interlaced = is_interlaced(file, infos) + # interlaced = is_interlaced(file, infos) cropsize = cropping(file, infos) volumes = volume_audio(file, infos) if args.stab: stabilization(file) - for track in infos['audio']: - convert_audio(file, track['index'], volumes[track['index']], track['channels'], track['channel_layout'], track['language'], track['title']) - vid_part_time = 0 - crf = 20 + if not args.starttime: + 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: + vid_part_time = args.starttime + else: + vid_part_time = 0 while vid_part_time < infos['duration']: + crf = 20 convert_video(file, infos, vid_part_time, cropsize, crf) vid_part_time += 300