Created
March 20, 2025 17:19
-
-
Save bigsnarfdude/3247df77b7cfe15f096414ae5d9bb249 to your computer and use it in GitHub Desktop.
title_editor.py
This file contains 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 subprocess | |
import sys | |
from pathlib import Path | |
def process_video(input_file, output_file): | |
"""Process a video file to remove seconds 4-15.""" | |
cmd = [ | |
'ffmpeg', | |
'-i', input_file, | |
'-filter_complex', | |
"[0:v]trim=0:4,setpts=PTS-STARTPTS[v1];" | |
"[0:v]trim=15,setpts=PTS-STARTPTS[v2];" | |
"[0:a]atrim=0:4,asetpts=PTS-STARTPTS[a1];" | |
"[0:a]atrim=15,asetpts=PTS-STARTPTS[a2];" | |
"[v1][a1][v2][a2]concat=n=2:v=1:a=1[outv][outa]", | |
'-map', "[outv]", | |
'-map', "[outa]", | |
output_file | |
] | |
print(f"Processing: {input_file}") | |
result = subprocess.run(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE) | |
if result.returncode != 0: | |
print(f"Error processing {input_file}:") | |
print(result.stderr.decode()) | |
return False | |
print(f"Successfully created: {output_file}") | |
return True | |
def process_directory(input_dir, output_dir=None): | |
"""Process all video files in a directory.""" | |
input_path = Path(input_dir) | |
# If no output directory is specified, create "edited" subdirectory | |
if output_dir is None: | |
output_path = input_path / "edited" | |
else: | |
output_path = Path(output_dir) | |
# Create output directory if it doesn't exist | |
output_path.mkdir(exist_ok=True) | |
# Get all video files (mp4, mov, etc.) | |
video_extensions = ['.mp4', '.mov', '.avi', '.mkv'] | |
video_files = [f for f in input_path.iterdir() | |
if f.is_file() and f.suffix.lower() in video_extensions] | |
if not video_files: | |
print(f"No video files found in {input_dir}") | |
return | |
print(f"Found {len(video_files)} video files to process") | |
# Process each video | |
successful = 0 | |
for video_file in video_files: | |
output_file = output_path / f"{video_file.stem}_edited{video_file.suffix}" | |
if process_video(str(video_file), str(output_file)): | |
successful += 1 | |
print(f"Processed {successful} out of {len(video_files)} files successfully") | |
if __name__ == "__main__": | |
# If directory is provided as command line argument, use it | |
if len(sys.argv) > 1: | |
input_dir = sys.argv[1] | |
output_dir = sys.argv[2] if len(sys.argv) > 2 else None | |
process_directory(input_dir, output_dir) | |
else: | |
# Otherwise use current directory | |
print("Usage: python video_processor.py [input_directory] [output_directory (optional)]") | |
print("Processing videos in current directory...") | |
process_directory(".") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment