corrections de bugs, gère le démarrage à un moment précis (pour reprendre un encodage)

This commit is contained in:
Antoine Van Elstraete 2021-06-28 10:53:08 +02:00
parent ed42ca0aa9
commit 70690a57d0

View File

@ -52,11 +52,11 @@ def get_infos(file):
duration = subprocess.getoutput(f"ffprobe -v quiet -print_format json -show_format {file}") duration = subprocess.getoutput(f"ffprobe -v quiet -print_format json -show_format {file}")
duration = json.loads(duration) duration = json.loads(duration)
duration = float(duration['format']['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) 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: 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'}) 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}
logging.debug("Informations du film : \n" + json.dumps(infos, indent=True)) 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 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) 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" 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] 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): def convert_audio(file, track, volume_adj, channels, channel_layout, language, title):
bitrate = f'{64*channels}k' bitrate = f'{64*channels}k'
if channel_layout == "5.1(side)":
channel_layout = "5.1"
codec = 'libopus' codec = 'libopus'
metadatas = f'-metadata language="{language}" -metadata title="{title}"' 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} -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): 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'] fmt = infos['video']['pix_fmt']
track = infos['video']['index'] track = infos['video']['index']
codec = 'libx265 -crf {crf} -preset slower' codec = 'libx265 -preset slow'
hdr = ''
if 'side_data_list' in infos['video'].keys(): 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']}" try:
color_primaries = infos['video']['color_primaries'] light_level = f"{infos['video']['side_data_list'][1]['max_content']},{infos['video']['side_data_list'][1]['max_average']}"
color_transfer = infos['video']['color_transfer'] color_primaries = infos['video']['color_primaries']
color_space = infos['video']['color_space'] color_transfer = infos['video']['color_transfer']
green_x = infos['video']['side_data_list'][0]['green_x'].split('/') color_space = infos['video']['color_space']
green_x = int(int(green_x[0])*(int(green_x[1])/50000)) green_x = infos['video']['side_data_list'][0]['green_x'].split('/')
green_y = infos['video']['side_data_list'][0]['green_y'].split('/') green_x = int(int(green_x[0])*(int(green_x[1])/50000))
green_y = int(int(green_y[0])*(int(green_y[1])/50000)) green_y = infos['video']['side_data_list'][0]['green_y'].split('/')
green = f'G({green_x},{green_y})' green_y = int(int(green_y[0])*(int(green_y[1])/50000))
blue_x = infos['video']['side_data_list'][0]['blue_x'].split('/') green = f'G\({green_x},{green_y}\)'
blue_x = int(int(blue_x[0])*(int(blue_x[1])/50000)) blue_x = infos['video']['side_data_list'][0]['blue_x'].split('/')
blue_y = infos['video']['side_data_list'][0]['blue_y'].split('/') blue_x = int(int(blue_x[0])*(int(blue_x[1])/50000))
blue_y = int(int(blue_y[0])*(int(blue_y[1])/50000)) blue_y = infos['video']['side_data_list'][0]['blue_y'].split('/')
blue = f'B({blue_x},{blue_y})' blue_y = int(int(blue_y[0])*(int(blue_y[1])/50000))
red_x = infos['video']['side_data_list'][0]['red_x'].split('/') blue = f'B\({blue_x},{blue_y}\)'
red_x = int(int(red_x[0])*(int(red_x[1])/50000)) red_x = infos['video']['side_data_list'][0]['red_x'].split('/')
red_y = infos['video']['side_data_list'][0]['red_y'].split('/') red_x = int(int(red_x[0])*(int(red_x[1])/50000))
red_y = int(int(red_y[0])*(int(red_y[1])/50000)) red_y = infos['video']['side_data_list'][0]['red_y'].split('/')
red = f'R({red_x},{red_y})' red_y = int(int(red_y[0])*(int(red_y[1])/50000))
white_point_x = infos['video']['side_data_list'][0]['white_point_x'].split('/') red = f'R\({red_x},{red_y}\)'
white_point_x = int(int(white_point_x[0])*(int(white_point_x[1])/50000)) white_point_x = infos['video']['side_data_list'][0]['white_point_x'].split('/')
white_point_y = infos['video']['side_data_list'][0]['white_point_y'].split('/') white_point_x = int(int(white_point_x[0])*(int(white_point_x[1])/50000))
white_point_y = int(int(white_point_y[0])*(int(white_point_y[1])/50000)) white_point_y = infos['video']['side_data_list'][0]['white_point_y'].split('/')
white_point = f'WP({white_point_x},{white_point_y})' white_point_y = int(int(white_point_y[0])*(int(white_point_y[1])/50000))
min_luminance = infos['video']['side_data_list'][0]['min_luminance'].split('/') white_point = f'WP\({white_point_x},{white_point_y}\)'
min_luminance = int(int(min_luminance[0])*(int(min_luminance[1])/10000)) min_luminance = infos['video']['side_data_list'][0]['min_luminance'].split('/')
max_luminance = infos['video']['side_data_list'][0]['max_luminance'].split('/') min_luminance = int(int(min_luminance[0])*(int(min_luminance[1])/10000))
max_luminance = int(int(max_luminance[0])*(int(max_luminance[1])/10000)) max_luminance = infos['video']['side_data_list'][0]['max_luminance'].split('/')
luminance = f'L({max_luminance},{min_luminance})' max_luminance = int(int(max_luminance[0])*(int(max_luminance[1])/10000))
master_display = green + blue + red + white_point + luminance luminance = f'L\({max_luminance},{min_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}' master_display = green + blue + red + white_point + luminance
else: 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}'
hdr = "" 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}' 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) logging.debug(command)
import pdb; pdb.set_trace()
result = subprocess.getoutput(command) result = subprocess.getoutput(command)
logging.info(result) logging.info(result)
@ -189,6 +194,7 @@ if __name__ == '__main__':
parser.add_argument("f_input") parser.add_argument("f_input")
parser.add_argument("-d", "--debug", dest="debug", action="store_true") parser.add_argument("-d", "--debug", dest="debug", action="store_true")
parser.add_argument("-s", "--stabilise", dest="stab", action="store_true") parser.add_argument("-s", "--stabilise", dest="stab", action="store_true")
parser.add_argument("-t", "--starttime", dest="starttime")
args = parser.parse_args() args = parser.parse_args()
if args.debug: if args.debug:
logging.basicConfig(format='[%(asctime)s]\n%(message)s', level=logging.DEBUG, datefmt='%d/%m/%Y %H:%M:%S') 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') logging.basicConfig(format='[%(asctime)s]\n%(message)s', level=logging.INFO, datefmt='%d/%m/%Y %H:%M:%S')
file = args.f_input file = args.f_input
infos = get_infos(file) infos = get_infos(file)
interlaced = is_interlaced(file, infos) # interlaced = is_interlaced(file, infos)
cropsize = cropping(file, infos) cropsize = cropping(file, infos)
volumes = volume_audio(file, infos) volumes = volume_audio(file, infos)
if args.stab: if args.stab:
stabilization(file) stabilization(file)
for track in infos['audio']: if not args.starttime:
convert_audio(file, track['index'], volumes[track['index']], track['channels'], track['channel_layout'], track['language'], track['title']) for track in infos['audio']:
vid_part_time = 0 convert_audio(file, track['index'], volumes[track['index']], track['channels'], track['channel_layout'], track['language'], track['title'])
crf = 20 if args.starttime:
vid_part_time = args.starttime
else:
vid_part_time = 0
while vid_part_time < infos['duration']: while vid_part_time < infos['duration']:
crf = 20
convert_video(file, infos, vid_part_time, cropsize, crf) convert_video(file, infos, vid_part_time, cropsize, crf)
vid_part_time += 300 vid_part_time += 300