import os import requests import shutil import ffmpeg import logging import subprocess import json logging.basicConfig(level=logging.INFO) # Define API parameters API_URL = 'https://catbox.moe/user/api.php' USER_HASH = '' # Set user hash here if required def upload_file_to_catbox(file_path, file_extension, user_hash): try: with open(file_path, 'rb') as f: files = { 'fileToUpload': (f.name, f.read(), file_extension), } data = { 'reqtype': 'fileupload', 'userhash': user_hash, } response = requests.post(API_URL, files=files, data=data) response.raise_for_status() # The uploaded filename is the last part of the URL uploaded_filename = response.text.split('/')[-1] logging.info(f'Successfully uploaded {file_path} as {uploaded_filename}') return uploaded_filename except Exception as e: logging.error(f'Failed to upload {file_path} due to {str(e)}') return None def get_audio_codec(file_path): command = [ 'ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_streams', '-select_streams', 'a', file_path ] output = subprocess.check_output(command).decode('utf-8') info = json.loads(output) return info['streams'][0]['codec_name'] def process_file(file_path, user_hash=USER_HASH): if file_path.endswith('.webm'): try: logging.info(f'Processing {file_path}...') audio_codec = get_audio_codec(file_path) # Map from common audio codec names to file extensions codec_to_extension = { 'vorbis': '.ogg', 'opus': '.opus', 'aac': '.aac', 'mp3': '.mp3', } audio_extension = codec_to_extension.get(audio_codec, '.unknown') output_filename = file_path.rsplit('.', 1)[0] + audio_extension # Extract audio subprocess.run(['ffmpeg', '-i', file_path, '-vn', '-c:a', 'copy', output_filename], check=True) # If the audio is opus, put it into an ogg container if audio_codec == 'opus': audio_extension = '.ogg' ogg_output_filename = output_filename.rsplit('.', 1)[0] + audio_extension subprocess.run(['ffmpeg', '-i', output_filename, '-vn', '-c:a', 'copy', '-f', 'ogg', ogg_output_filename], check=True) output_filename = ogg_output_filename # Upload audio file uploaded_filename = upload_file_to_catbox(output_filename, audio_extension, user_hash) if uploaded_filename: new_filename = file_path.rsplit('.', 1)[0] + f' [sound=files.catbox.moe%2F{uploaded_filename}]' + '.webm' # Create a new copy of the webm without audio subprocess.run(['ffmpeg', '-i', file_path, '-c:v', 'copy', '-an', new_filename], check=True) logging.info(f'Successfully processed {file_path} and created {new_filename}') except Exception as e: logging.error(f'Failed to process {file_path} due to {str(e)}') def process_directory(directory_path, user_hash=USER_HASH): for dirpath, _, filenames in os.walk(directory_path): for filename in filenames: full_path = os.path.join(dirpath, filename) process_file(full_path, user_hash) if __name__ == '__main__': PATH = input("Please provide the directory or file path: ") if os.path.isdir(PATH): process_directory(PATH) elif os.path.isfile(PATH): process_file(PATH) else: logging.error("Invalid path. Please provide a valid directory or file path.")