Files
ffmpeg_collection/long_path_test.py
2022-09-09 20:58:30 +02:00

89 lines
3.2 KiB
Python

import re
import subprocess
import sys
from pathlib import Path
import os
output_folder_name = "/out_sub_audio/"
regex_subtitle = re.compile("Stream #0:\d+(\(\w*\))?: Subtitle:.*")
regex_audio = re.compile("Stream #0:\d(\(\w*\))?: Audio:.*")
nr_files = 0
files_processed = []
droppedFile = sys.argv[1]
droppedName = Path(droppedFile).name
originalfiles = sys.orig_argv[2:]
# To fix I need to create symlinks to files with long paths and use the links instead. And then remove said symlinks once
#droppedFile= ['X:\\test\\longpathtest\\longerpathlongerpathlongerpathlongerpathlongerpathlongerpathlongerpath\\longpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpath\\fourteen\\[Kira-Fansub]_Choujuushin Gravion_Episode 06_(BD 1280x960 h264 JP AAC EN AAC) [E7D92968].mkv', '', ' X:\\test\\longpathtest\\longerpathlongerpathlongerpathlongerpathlongerpathlongerpathlongerpath\\longpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpath\\fourteen\\[Kira-Fansub]_Choujuushin Gravion_Episode 09_(BD 1280x960 h264 JP AAC EN AAC) [A2727604].mkv']
#originalfiles = droppedFile
print(droppedFile)
print()
print()
print("printing original: ", originalfiles)
joined = (' '.join(originalfiles))
print("printing joined: ", joined)
re.sub (' +', ' ', joined)
print("duble spaces removed? :", joined)
listed = re.split(r'([A-Z]:.+?)(?=[A-Z]:)', joined)
listed = list(filter(None, listed))
print("printing listed: ", listed)
#get path of script location and remove the tail
head_tail = os.path.split(sys.argv[0])
output_path = head_tail[0]
#check if output folder exists, otherwise create it
isExist = os.path.exists(output_path+output_folder_name)
if not isExist:
os.makedirs(output_path+output_folder_name)
#droppedFile = "test.mkv"
for i, file in enumerate(listed):
arg_list = '.\\ffprobe.exe -i "{file}"'.format(file=file)
print(arg_list)
cmd = subprocess.Popen(["powershell.exe", arg_list], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='UTF-8')
subprocess_err = cmd.communicate()[1]
print(subprocess_err)
print("file", i+1,":", Path(file).name)
for r, match in enumerate(regex_audio.finditer(subprocess_err)):
print("Audio Track", r,": ", match.group(0))
for r, match in enumerate(regex_subtitle.finditer(subprocess_err)):
print("Subtitle Track", r,": ", match.group(0))
print('\n')
x = (int(x) for x in input ("Enter audio tracks to keep: ").split())
audio_track = "".join(" -map 0:a:{}".format(y) for y in x)
z = (int(z) for z in input ("Enter subtitle tracks to keep: ").split())
subtitle_track = "".join(" -map 0:s:{}".format(y) for y in z)
for file in listed:
basename = os.path.basename(file)
arg_list = 'ffmpeg.exe -i "{file}" -map 0:v {audio_track} {subtitle_track} -c copy "{output_path}{output_folder_name}{basename}"'.format(file=file, audio_track=audio_track, subtitle_track=subtitle_track, output_path=output_path, output_folder_name=output_folder_name, basename=basename)
print(arg_list)
subprocess.run(arg_list)
nr_files = nr_files+1
files_processed.append(file)
print("Number of files processed: ", nr_files)
print(*files_processed, sep='\n')
input("press enter to exit")