Last active
September 28, 2025 06:13
-
-
Save PierceLBrooks/c516d208810e1c75d79784eaf4052dd3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import sys | |
| import json | |
| import shutil | |
| import mimetypes | |
| import subprocess | |
| import logging | |
| import traceback | |
| mimetypes.init() | |
| ffmpeg = None | |
| ffprobe = None | |
| try: | |
| ffmpeg = shutil.which("ffmpeg") | |
| ffprobe = shutil.which("ffprobe") | |
| except: | |
| ffmpeg = None | |
| ffprobe = None | |
| if ((ffmpeg == None) or (ffprobe == None)): | |
| sys.exit() | |
| if not (os.path.exists(ffmpeg)): | |
| sys.exit() | |
| names = [] | |
| for root, folders, files in os.walk(os.getcwd()): | |
| for name in files: | |
| if not ("." in name): | |
| continue | |
| try: | |
| type = mimetypes.guess_type(name)[0] | |
| if (sys.flags.debug): | |
| print(name) | |
| print(type) | |
| if (("audio" in type) or ("video" in type)): | |
| names.append(name) | |
| except: | |
| logging.error(traceback.format_exc()) | |
| break | |
| groups = {} | |
| for name in names: | |
| group = name[:name.index(".")] | |
| if not (group in groups): | |
| groups[group] = [] | |
| groups[group].append(name) | |
| for group in groups: | |
| path = os.path.join(os.getcwd(), group+"."+sys.argv[0]+".mp4") | |
| if (sys.flags.debug): | |
| print(path) | |
| if (os.path.exists(path)): | |
| continue | |
| group = groups[group] | |
| if (len(group) < 2): | |
| continue | |
| audio = None | |
| video = None | |
| for name in group: | |
| command = [] | |
| command.append(ffprobe) | |
| command.append("-v") | |
| command.append("quiet") | |
| command.append("-print_format") | |
| command.append("json") | |
| command.append("-show_streams") | |
| command.append("-i") | |
| command.append(os.path.join(os.getcwd(), name)) | |
| if (sys.flags.debug): | |
| print(str(command)) | |
| try: | |
| output = subprocess.check_output(command).decode() | |
| data = json.loads(output) | |
| if (sys.flags.debug): | |
| print(json.dumps(data)) | |
| streams = data["streams"] | |
| for stream in streams: | |
| if ((audio == None) and (stream["codec_type"] == "audio")): | |
| audio = name | |
| break | |
| if ((video == None) and (stream["codec_type"] == "video")): | |
| video = name | |
| break | |
| except: | |
| logging.error(traceback.format_exc()) | |
| if ((audio == None) or (video == None)): | |
| continue | |
| command = [] | |
| command.append(ffmpeg) | |
| command.append("-i") | |
| command.append(video) | |
| command.append("-i") | |
| command.append(audio) | |
| command.append("-c:v") | |
| command.append("copy") | |
| command.append("-c:a") | |
| command.append("aac") | |
| command.append("-map") | |
| command.append("0:v:0") | |
| command.append("-map") | |
| command.append("1:a:0") | |
| command.append(path) | |
| if (sys.flags.debug): | |
| print(str(command)) | |
| try: | |
| subprocess.check_output(command) | |
| except: | |
| logging.error(traceback.format_exc()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment