Whisper / utils /auto_editor.py
basyx's picture
Create auto_editor.py
3e45338 verified
Raw
History Blame
2.09 kB
"""
auto_editor.py
---------------------------------------
Automatic video pacing editor
Features:
- Removes silence automatically
- Speeds up slow segments
- Keeps speech natural
- CPU optimized (HuggingFace FREE tier safe)
Input:
input.mp4
Output:
edited.mp4
"""
import subprocess
import os
import sys
from pathlib import Path
INPUT_VIDEO = "input.mp4"
OUTPUT_VIDEO = "edited.mp4"
def run_command(cmd):
"""Run shell command safely"""
try:
subprocess.run(
cmd,
shell=True,
check=True
)
except subprocess.CalledProcessError as e:
print("Command failed:", e)
sys.exit(1)
def check_input():
if not os.path.exists(INPUT_VIDEO):
print(f"❌ Missing file: {INPUT_VIDEO}")
sys.exit(1)
def install_auto_editor():
"""
Ensures auto-editor exists.
Required because HF containers reset.
"""
print("Installing auto-editor...")
run_command("pip install --no-cache-dir auto-editor")
def auto_edit():
"""
Main editing step.
Removes silence + improves pacing.
"""
cmd = f"""
auto-editor "{INPUT_VIDEO}"
--margin 0.2s
--silent-speed 99999
--video-speed 1
--audio-normalize peak
--export mp4
--output "{OUTPUT_VIDEO}"
"""
print("Running auto-editor...")
run_command(cmd)
def optimize_output():
"""
Re-encode for social media compatibility.
"""
temp = "optimized.mp4"
cmd = f"""
ffmpeg -y -i "{OUTPUT_VIDEO}"
-vf scale=1080:1920:force_original_aspect_ratio=decrease,pad=1080:1920:(ow-iw)/2:(oh-ih)/2
-c:v libx264
-preset veryfast
-crf 23
-c:a aac
-b:a 128k
"{temp}"
"""
print("Optimizing output...")
run_command(cmd)
os.replace(temp, OUTPUT_VIDEO)
def main():
print("===== AUTO EDITOR START =====")
check_input()
install_auto_editor()
auto_edit()
optimize_output()
print("✅ Editing complete:", OUTPUT_VIDEO)
if __name__ == "__main__":
main()