import os import subprocess import shutil import sys from time import time HEADER = r""" # ███████████ █████ █████ █████ █████ █████████ ██████ █████ # ░░███░░░░░███ ░░███ ░░███ ░░███ ░░███ ███░░░░░███ ░░██████ ░░███ # ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███ ░███░███ ░███ # ░██████████ ░███ ░███ ░███ ░███ ░███████████ ░███░░███░███ # ░███░░░░░███ ░███ ░░███ ███ ░███ ░███░░░░░███ ░███ ░░██████ # ░███ ░███ ░███ ░░░█████░ ░███ ░███ ░███ ░███ ░░█████ # █████ █████ █████ ░░███ █████ █████ █████ █████ ░░█████ # ░░░░░ ░░░░░ ░░░░░ ░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░ ############################################################### ### Rivian - DME Automation & Controls Engineering ### ### Contact: ### ### lfregososoto@rivian.com ### ### jaltamirano@rivian.com ### ### prashamsoni@rivian.com ### ### isaacsoto@rivian.com ### ############################################################### """ base_path = r"C:\Project\code\temp_video_converter" ffmpeg_local_path = os.path.join(base_path, "ffmpeg", "ffmpeg.exe") ffmpeg_path = shutil.which("ffmpeg") if not ffmpeg_path: if os.path.exists(ffmpeg_local_path): ffmpeg_path = ffmpeg_local_path print(f"FFmpeg is not in the Path but it was found in: {ffmpeg_local_path}") else: print("I couldn't find FFmpeg in the path.") print("Make sure to place 'ffmpeg.exe' in the path:") print(f" {ffmpeg_local_path}") sys.exit(1) else: print(f"FFmpeg found in the system: {ffmpeg_path}") supported_extensions = ('.mp4', '.mkv', '.avi', '.mov', '.flv', '.wmv', '.mpeg', '.mpg', '.webm') cut_video = input("Do you want to trim the video to a specific duration? (y/n): ").strip().lower() == 'y' cut_duration = "10" if cut_video: cut_duration = input("How many seconds do you want to keep from the start of the video?: ").strip() if not cut_duration.isdigit(): print("Invalid duration.") sys.exit(1) remove_audio = input("Do you want to remove the audio from the video? (y/n): ").strip().lower() == 'y' video_files = [f for f in os.listdir(base_path) if f.lower().endswith(supported_extensions)] if not video_files: print("No compatible video files found in the folder.") sys.exit(1) print (HEADER) print("\nVideos found:") for idx, file in enumerate(video_files, 1): print(f" [{idx}] {file}") print(" [0] Convert all") selection = input("\nSelect the files to convert (e.g., 1 3 5 or 0 for all): ").strip() if selection == "0": selected_files = video_files else: try: indices = [int(i) for i in selection.split()] selected_files = [video_files[i - 1] for i in indices if 1 <= i <= len(video_files)] except Exception as e: print(f"Invalid input: {e}") sys.exit(1) if not selected_files: print("No valid files selected.") sys.exit(1) for filename in selected_files: input_path = os.path.join(base_path, filename) name, _ = os.path.splitext(filename) output_path = os.path.join(base_path, f"{name}.webm") print(f"\nConverting:\nInput: {input_path}\nOutput: {output_path}") start = time() command = [ffmpeg_path, "-i", input_path] if cut_video: command.extend(["-t", cut_duration]) command.extend(["-c:v", "libvpx", "-b:v", "1M"]) if remove_audio: command.append("-an") else: command.extend(["-c:a", "libvorbis"]) command.extend(["-threads", "4", output_path]) result = subprocess.run(command) duration = time() - start if result.returncode == 0: print(f"Conversion completed in {duration:.2f} seconds.") else: print(f"Error converting {filename}.")