Files
personal_development/video_merge/merge_videos.py
T
2026-01-14 17:59:09 +00:00

100 lines
3.3 KiB
Python

import os
import sys
def get_file_paths():
"""Prompts the user for video file paths."""
file_paths = []
print("Enter the paths to the video files you want to merge.")
print("Type 'done' when you are finished.")
while True:
user_input = input(f"File path #{len(file_paths) + 1}: ").strip()
if user_input.lower() == 'done':
if len(file_paths) < 2:
print("Please provide at least two video files to merge.")
continue
break
if not user_input:
continue
# Remove quotes if the user pasted a path with quotes
user_input = user_input.strip('"').strip("'")
if os.path.exists(user_input) and os.path.isfile(user_input):
file_paths.append(user_input)
print(f"Added: {user_input}")
else:
print(f"Error: File not found at '{user_input}'. Please try again.")
return file_paths
def main():
print("--- Video Merger Tool ---")
try:
# Check if moviepy is installed
try:
try:
from moviepy import VideoFileClip, concatenate_videoclips
except ImportError:
from moviepy.editor import VideoFileClip, concatenate_videoclips
except ImportError:
print("Error: 'moviepy' library is not installed.")
print("Please run: pip install -r requirements.txt")
return
video_files = get_file_paths()
output_filename = input("Enter the output file name (e.g., merged_video.mp4): ").strip()
if not output_filename:
output_filename = "merged_output.mp4"
print(f"No name provided. Using default: {output_filename}")
if not output_filename.lower().endswith(".mp4"):
output_filename += ".mp4"
print(f"Added extension. Output will be: {output_filename}")
print("\nProcessing videos... This may take a while depending on file size.")
clips = []
try:
for path in video_files:
print(f"Loading {path}...")
clip = VideoFileClip(path)
clips.append(clip)
print("Concatenating clips...")
final_clip = concatenate_videoclips(clips)
print(f"Writing to {output_filename} (H.264 / AAC)...")
final_clip.write_videofile(
output_filename,
codec='libx264',
audio_codec='aac',
temp_audiofile='temp-audio.m4a',
remove_temp=True,
logger='bar'
)
print(f"\nSuccess! Merged video saved to: {os.path.abspath(output_filename)}")
except Exception as e:
print(f"\nAn error occurred during processing: {e}")
finally:
# Clean up resources
for clip in clips:
try:
clip.close()
except:
pass
except KeyboardInterrupt:
print("\nOperation cancelled by user.")
except Exception as e:
print(f"\nUnexpected error: {e}")
if __name__ == "__main__":
main()