commit 1563d333bbe00d42dded970a0f4043f9fba7f973 Author: Thaloria Date: Mon Jun 28 06:42:01 2021 +0200 Initial diff --git a/StreamDesc.py b/StreamDesc.py new file mode 100644 index 0000000..cad3753 --- /dev/null +++ b/StreamDesc.py @@ -0,0 +1,21 @@ +from dataclasses import dataclass + + +@dataclass +class StreamDesc: + stream_no: int + stream_lang: str + stream_type: str + stream_input_no: int + + def __init__(self, stream_line, s_type, i_stream_in): + out = stream_line.split("|") + self.stream_no = int(out[0]) + if "en" == out[1]: + self.stream_lang = "eng" + elif "de" == out[1]: + self.stream_lang = "ger" + else: + self.stream_lang = out[1] + self.stream_type = s_type + self.stream_input_no = i_stream_in diff --git a/Utils.py b/Utils.py new file mode 100644 index 0000000..231567d --- /dev/null +++ b/Utils.py @@ -0,0 +1,89 @@ +from StreamDesc import StreamDesc +import subprocess + +# ffprobe_a = r"C:\Users\Thaloria\Downloads\ffmpeg-4.4-full_build\bin\ffprobe.exe -i S01e01.mkv -show_entries stream=index:stream_tags=language -select_streams a -v 0 -of compact=p=0:nk=1" +# ffprobe_s = r"C:\Users\Thaloria\Downloads\ffmpeg-4.4-full_build\bin\ffprobe.exe -i S01e01.mkv -show_entries stream=index:stream_tags=language -select_streams s -v 0 -of compact=p=0:nk=1" +# -i "E:\tmp\S01e01.mkv" -i "S01e01_1.88x_1920x1080_dtvs-2.mp4" -map 1:0 -map 0:1 -map 0:2 -map 0:4 -map 0:6 -map 0:11 -c:v:0 copy -c:s:0 copy -c:s:1 copy -c:s:2 copy -c:a:0 aac -b:a:0 384k -c:a:1 aac -b:a:1 384k -vn -y "E:\tmp\out.mp4" + +FFEXE_STATIC = r"C:\Users\Thaloria\Downloads\ffmpeg-4.4-full_build\bin\ffmpeg.exe" +FFPROBE_STATIC = r"C:\Users\Thaloria\Downloads\ffmpeg-4.4-full_build\bin\ffprobe.exe" + + +def stream_selector(input_file_path, input_no): + cmd_a = FFPROBE_STATIC + " -i " + input_file_path + " -show_entries stream=index:stream_tags=language -select_streams a -v 0 -of compact=p=0:nk=1" + output = subprocess.check_output(cmd_a, shell=True) + str_out = str(output) + list_out = format_output(str_out) + + desc_list = [] + + for stream_item in list_out: + if 'de' in stream_item or 'en' in stream_item: + desc = StreamDesc(stream_item, "a", input_no) + desc_list.append(desc) + + cmd_s = FFPROBE_STATIC + " -i " + input_file_path + " -show_entries stream=index:stream_tags=language -select_streams s -v 0 -of compact=p=0:nk=1" + output = subprocess.check_output(cmd_s, shell=True) + str_out = str(output) + list_out = format_output(str_out) + + for stream_item in list_out: + if 'de' in stream_item or 'en' in stream_item: + desc = StreamDesc(stream_item, "s", input_no) + desc_list.append(desc) + + return desc_list + + +def select_video_stream(i_streams, in_stream_no): + desc = StreamDesc("0|de", "v", in_stream_no) + cp_str = i_streams + cp_str.append(desc) + return cp_str + + +def format_output(data): + if data.startswith(r"b'"): + data = data[2:] + new_list = data.split(r"\r\n") + + try: + while True: + # new_list.remove() ("",) + new_list.remove('\x0c') + except ValueError: + pass + return new_list + + +def add_input(line, input_path): + line1 = line + " -i " + input_path + return line1 + + +def add_stream_selection(line, stream_descs): + out_line = line + for stream in stream_descs: + out_line = out_line + " -map " + str(stream.stream_input_no) + ":" + str(stream.stream_no) + + v_no = 0 + a_no = 0 + s_no = 0 + for stream in stream_descs: + if "v" == stream.stream_type: + out_line = out_line + " -c:" + stream.stream_type + ":" + str(v_no) + " copy" + elif "s" == stream.stream_type: + out_line = out_line + " -c:" + stream.stream_type + ":" + str(s_no) + " copy" + out_line = out_line + " -metadata:s:s:" + str(s_no) + " language=" + stream.stream_lang + s_no = s_no + 1 + elif "a" == stream.stream_type: + out_line = out_line + " -c:" + stream.stream_type + ":" + str(a_no) + " aac -b:a:" + str(a_no) + " 384k" + # -metadata:s:a:0 title="One" -metadata:s:a:1 title="Two" -metadata:s:a:0 language=eng + out_line = out_line + " -metadata:s:a:" + str(a_no) + " language=" + stream.stream_lang + a_no = a_no + 1 + return out_line + + +def add_output(line, out_path): + line1 = line + " -y " + out_path + return line1 diff --git a/main.py b/main.py new file mode 100644 index 0000000..a983ecc --- /dev/null +++ b/main.py @@ -0,0 +1,63 @@ +import os +import shutil + +# startdir = os.path.join("E:", "tmp", "input") + + +season = r"E:\tmp\Season2\\" +inputdir = season + "input" +audiodir = season + "audio" +videodir = season + "video" +rescaleddir = season + "rescaled" +outputdir = season + "output" +networkdir = r"\\nas451\Video\serieUR\Kalkofes Mattscheibe - Premiere Klassiker\Season 2" +networkdir2 = r"\\nas451\Inbox\Kalkofe\Scale1080p\Season2\rescaled" + +ffexe = r"C:\Users\Thaloria\Downloads\ffmpeg-4.4-full_build\bin\ffmpeg.exe" + +# -i S01E01.mkv -map 0:a S01E01.m4a +# ffmpeg -i input.mp4 -map 0 -c copy -metadata:s:a:0 title="One" -metadata:s:a:1 title="Two" -metadata:s:a:0 language=eng -metadata:s:a:1 language=spa output.mp4 + +os.chdir(inputdir) +for file in os.listdir(inputdir): + ifilename = inputdir + "\\" + file + audiofilename = audiodir + "\\" + file[:6] + ".m4a" + cmd = ffexe + " -i " + ifilename + " -map 0:a -metadata:s:a:0 title='Original' -metadata:s:a:1 title='Audiokommentar' -metadata:s:a:0 language=ger -metadata:s:a:1 language=ger " + audiofilename + if not os.path.isfile(audiofilename): + os.system(cmd) + +# ffmpeg -i input.mp4 -map 0:v output.mp4 +os.chdir(rescaleddir) +for file in os.listdir(rescaleddir): + ifilename = rescaleddir + "\\" + file + videofile = videodir + "\\" + file[:6] + ".mp4" + cmd = ffexe + " -i " + ifilename + " -map 0:v -c copy " + videofile + if not os.path.isfile(videofile): + os.system(cmd) + +# ffmpeg -i video.mp4 -i audio.m4a -map 0:v -map 0:a output.mkv +os.chdir(videodir) +for file in os.listdir(videodir): + videofile = videodir + "\\" + file + audiofilename = audiodir + "\\" + file[:6] + ".m4a" + outputfilename = outputdir + "\\" + file[:6] + ".mp4" + cmd = ffexe + " -i " + videofile + " -i " + audiofilename + " -map 0:v -map 1:a -metadata:s:a:0 title='Original' -metadata:s:a:1 title='Audiokommentar' -c copy " + outputfilename + if not os.path.isfile(outputfilename): + os.system(cmd) + +os.chdir(outputdir) +for file in os.listdir(outputdir): + outputfilename = outputdir + "\\" + file + networkfilename = networkdir + "\\" + file + if not os.path.isfile(networkfilename): + shutil.copyfile(outputfilename, networkfilename) + +os.chdir(rescaleddir) +for file in os.listdir(rescaleddir): + networkfilename = networkdir + "\\" + file[:6] + ".mp4" + if os.path.isfile(networkfilename): + os.remove(audiodir + "\\" + file[:6] + ".m4a") + os.remove(videodir + "\\" + file[:6] + ".mp4") + os.remove(outputdir + "\\" + file[:6] + ".mp4") + os.remove(inputdir + "\\" + file[:6] + ".mkv") + os.remove(rescaleddir + "\\" + file) diff --git a/main2.py b/main2.py new file mode 100644 index 0000000..d5bf883 --- /dev/null +++ b/main2.py @@ -0,0 +1,18 @@ +import os +from Utils import FFEXE_STATIC, select_video_stream, add_stream_selection, stream_selector, add_output, format_output, \ + add_input + +input_0 = r"E:\tmp\S01e01.mkv" +input_1 = r"E:\tmp\S01e01_1.88x_1920x1080_dtvs-2.mp4" +output = r"E:\tmp\out.mp4" + +streams = stream_selector(input_0, 0) +streams = select_video_stream(streams) + +cmd = add_input(FFEXE_STATIC, input_0) +cmd = add_input(cmd, input_1) +cmd = add_stream_selection(cmd, streams) +cmd = add_output(cmd, output) + +print(cmd) +os.system(cmd)