Changement des codecs, correction d'un bug en cas de champ vide sur les titres

This commit is contained in:
Antoine Van Elstraete 2022-07-01 20:41:17 +02:00
parent dd43acb829
commit 805d4f17ab
Signed by: AntoineVe
GPG Key ID: E36069A977E2A9ED

View File

@ -49,8 +49,12 @@ def get_infos(file):
'index': a_stream['index'], 'index': a_stream['index'],
'channels': a_stream['channels'], 'channels': a_stream['channels'],
'channel_layout': a_stream['channel_layout'], 'channel_layout': a_stream['channel_layout'],
'language': a_stream['tags']['language'], 'language': a_stream['tags']['language']
'title': a_stream['tags']['title']} }
try:
a_stream_infos.update({'title': a_stream['tags']['title']})
except:
a_stream_infos.update({'title': 'No title'})
a_infos.append(a_stream_infos) a_infos.append(a_stream_infos)
s_infos = [] s_infos = []
for s_stream in full_s_infos['streams']: for s_stream in full_s_infos['streams']:
@ -63,7 +67,6 @@ def get_infos(file):
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)
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'})
@ -153,23 +156,25 @@ def convert_audio(file, track, volume_adj, channels, channel_layout, language, t
bitrate = f'{64*channels}k' bitrate = f'{64*channels}k'
if channel_layout == "5.1(side)": if channel_layout == "5.1(side)":
channel_layout = "5.1" channel_layout = "5.1"
codec = 'libopus' codec = 'libfdk_aac -vbr 5'
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} -map_metadata -1 -vn -sn -c:a {codec} -b:a {bitrate} -mapping_family 1 -filter:a volume={volume_adj},aformat=channel_layouts={channel_layout} {metadatas} -y {file}_audio_{track}_{language}.mka' command = f'ffmpeg -loglevel error -i {file} -map 0:{track} -map_metadata -1 -vn -sn -c:a {codec} -mapping_family 1 -filter:a volume={volume_adj},aformat=channel_layouts={channel_layout} {metadatas} -y {file}_audio_{track}_{language}.mka'
logging.debug(command) logging.debug(command)
result = subprocess.getoutput(command) result = subprocess.getoutput(command)
logging.info(result) logging.info(result)
def convert_video(file, infos, start, crop, crf): def convert_video(file, infos, start, crop, crf, animation):
str_start = "{:05d}".format(start) str_start = "{:05d}".format(start)
output = f'{file}_video_t{str_start}.mkv' output = f'{file}_video_t{str_start}.mkv'
fmt = infos['video']['pix_fmt'] fmt = "yuv420p10le" # Always 10-bits
if fmt == "yuv420p":
fmt = "yuv420p10le"
track = infos['video']['index'] track = infos['video']['index']
codec = 'libx265 -preset slow' codec = 'libx265 -preset slower -g 1' # -g 1 is for intra encoding
hdr = '' hdr = ''
if animation:
tune = "-tune animation"
else:
tune = ""
if 'side_data_list' in infos['video'].keys(): if 'side_data_list' in infos['video'].keys():
try: try:
light_level = f"{infos['video']['side_data_list'][1]['max_content']},{infos['video']['side_data_list'][1]['max_average']}" light_level = f"{infos['video']['side_data_list'][1]['max_content']},{infos['video']['side_data_list'][1]['max_average']}"
@ -205,7 +210,7 @@ def convert_video(file, infos, start, crop, crf):
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 = 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: except:
logging.debug("Aucune information HDR") 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} {tune} {hdr} -crf {crf} -pix_fmt {fmt} -filter:v {crop} -y {output}'
logging.debug(command) logging.debug(command)
result = subprocess.getoutput(command) result = subprocess.getoutput(command)
logging.info(result) logging.info(result)
@ -270,6 +275,7 @@ if __name__ == '__main__':
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") parser.add_argument("-t", "--starttime", dest="starttime")
parser.add_argument("-a", "--animation", dest="animation", action="store_true")
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')
@ -282,6 +288,10 @@ if __name__ == '__main__':
volumes = volume_audio(file, infos) volumes = volume_audio(file, infos)
if args.stab: if args.stab:
stabilization(file) stabilization(file)
if args.animation:
animation = True
else:
animation = False
if not args.starttime: if not args.starttime:
for track in infos['subtitles']: for track in infos['subtitles']:
extract_subs(file, track['index'], track['language']) extract_subs(file, track['index'], track['language'])
@ -293,6 +303,6 @@ if __name__ == '__main__':
vid_part_time = 0 vid_part_time = 0
while vid_part_time < infos['duration']: while vid_part_time < infos['duration']:
crf = 19 crf = 19
convert_video(file, infos, vid_part_time, cropsize, crf) convert_video(file, infos, vid_part_time, cropsize, crf, animation)
vid_part_time += 300 vid_part_time += 300
create_mkv(file) create_mkv(file)