Création de la fonction pour créer le mkv final. Corrections de bugs

This commit is contained in:
Antoine Van Elstraete 2021-12-04 21:27:33 +01:00
parent 6cf84069e5
commit 16dc756571

View File

@ -4,7 +4,7 @@
import json
import subprocess
import logging
from os import listdir, remove
def get_infos(file):
'''
@ -56,7 +56,7 @@ def get_infos(file):
for s_stream in full_s_infos['streams']:
s_stream_infos = {
'index': s_stream['index'],
'language': s_stream['tagsd']['language']}
'language': s_stream['tags']['language']}
s_infos.append(s_stream_infos)
duration = subprocess.getoutput(f"ffprobe -v quiet -print_format json -show_format {file}")
duration = json.loads(duration)
@ -102,9 +102,10 @@ def cropping(file, infos):
'''
Cette fonction detecte les bandes inutiles de la vidéo
'''
logging.debug("Détection de la taille de l'image...")
logging.info("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"
logging.debug(command)
cropsize = subprocess.getoutput(command).splitlines()[-3].split()[-1]
logging.debug(f"Paramètre de découpe : {cropsize}")
return cropsize
@ -118,6 +119,8 @@ def volume_audio(file, infos):
for piste_audio in infos['audio']:
piste = piste_audio['index']
command = f"ffmpeg -loglevel info -i {file} -map 0:{piste} -af volumedetect -f null -y /dev/null"
logging.info(f"Détection du volume de la piste {piste}...")
logging.debug(command)
volumedetect = subprocess.getoutput(command)
for line in volumedetect.splitlines():
if "max_volume" in line:
@ -138,9 +141,11 @@ def stabilization(file):
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'
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.info(f"Extractio du sous-titre {track}, langue : {lang}...")
logging.debug(command)
result = subprocess.getoutput(command)
if result != "":
logging.info(result)
@ -150,7 +155,7 @@ def convert_audio(file, track, volume_adj, channels, channel_layout, language, t
channel_layout = "5.1"
codec = 'libopus'
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} {metadata} -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} {metadatas} -y {file}_audio_{track}_{language}.mka'
logging.debug(command)
result = subprocess.getoutput(command)
logging.info(result)
@ -160,6 +165,8 @@ def convert_video(file, infos, start, crop, crf):
str_start = "{:05d}".format(start)
output = f'{file}_video_t{str_start}.mkv'
fmt = infos['video']['pix_fmt']
if fmt == "yuv420p":
fmt = "yuv420p10le"
track = infos['video']['index']
codec = 'libx265 -preset slow'
hdr = ''
@ -204,6 +211,88 @@ def convert_video(file, infos, start, crop, crf):
logging.info(result)
def create_mkv(filename):
json_data = []
json_data.append("--output")
json_data.append(f"{filename}_FINAL.mkv")
for file in listdir():
if f"{filename}_video_t" in file:
json_data.append("--no-track-tags")
json_data.append("--no-global-tags")
json_data.append("--no-chapters")
if not "t00000" in file:
json_data.append("+")
json_data.append("(")
json_data.append(file)
json_data.append(")")
for file in listdir():
if f"{filename}_audio" in file:
json_data.append("--no-track-tags")
json_data.append("--no-global-tags")
json_data.append("--no-chapters")
if "fra" in file:
json_data.append("--language")
json_data.append("0:fr")
elif "eng" in file:
json_data.append("--language")
json_data.append("0:en")
elif "deu" in file:
json_data.append("--language")
json_data.append("0:de")
elif "nld" in file:
json_data.append("--language")
json_data.append("0:nl")
elif "spa" in file:
json_data.append("--language")
json_data.append("0:es")
elif "ita" in file:
json_data.append("--language")
json_data.append("0:it")
json_data.append("(")
json_data.append(file)
json_data.append(")")
for file in listdir():
if f"{filename}_subtitle" in file:
json_data.append("--no-track-tags")
json_data.append("--no-global-tags")
json_data.append("--no-chapters")
if "fra" in file:
json_data.append("--language")
json_data.append("0:fr")
elif "eng" in file:
json_data.append("--language")
json_data.append("0:en")
elif "deu" in file:
json_data.append("--language")
json_data.append("0:de")
elif "nld" in file:
json_data.append("--language")
json_data.append("0:nl")
elif "spa" in file:
json_data.append("--language")
json_data.append("0:es")
elif "ita" in file:
json_data.append("--language")
json_data.append("0:it")
json_data.append("(")
json_data.append(file)
json_data.append(")")
with open(f"/tmp/{filename}.json", "w") as mkvmerge_options:
mkvmerge_options.write(json.dumps(json_data))
command = f"mkvmerge -v @/tmp/{filename}.json"
logging.debug(command)
result = subprocess.getoutput(command)
logging.info(result)
remove(f"/tmp/{filename}.json")
for file in listdir():
if f"{filename}_video" in file:
remove(file)
if f"{filename}_audio" in file:
remove(file)
if f"{filename}_subtitle" in file:
remove(file)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
@ -236,7 +325,4 @@ 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
create_mkv(file)