Spaces:
Running
Running
Create auto_editor.py
Browse files- utils/auto_editor.py +112 -0
utils/auto_editor.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
auto_editor.py
|
| 3 |
+
---------------------------------------
|
| 4 |
+
Automatic video pacing editor
|
| 5 |
+
|
| 6 |
+
Features:
|
| 7 |
+
- Removes silence automatically
|
| 8 |
+
- Speeds up slow segments
|
| 9 |
+
- Keeps speech natural
|
| 10 |
+
- CPU optimized (HuggingFace FREE tier safe)
|
| 11 |
+
|
| 12 |
+
Input:
|
| 13 |
+
input.mp4
|
| 14 |
+
|
| 15 |
+
Output:
|
| 16 |
+
edited.mp4
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
import subprocess
|
| 20 |
+
import os
|
| 21 |
+
import sys
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
|
| 24 |
+
INPUT_VIDEO = "input.mp4"
|
| 25 |
+
OUTPUT_VIDEO = "edited.mp4"
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def run_command(cmd):
|
| 29 |
+
"""Run shell command safely"""
|
| 30 |
+
try:
|
| 31 |
+
subprocess.run(
|
| 32 |
+
cmd,
|
| 33 |
+
shell=True,
|
| 34 |
+
check=True
|
| 35 |
+
)
|
| 36 |
+
except subprocess.CalledProcessError as e:
|
| 37 |
+
print("Command failed:", e)
|
| 38 |
+
sys.exit(1)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def check_input():
|
| 42 |
+
if not os.path.exists(INPUT_VIDEO):
|
| 43 |
+
print(f"❌ Missing file: {INPUT_VIDEO}")
|
| 44 |
+
sys.exit(1)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def install_auto_editor():
|
| 48 |
+
"""
|
| 49 |
+
Ensures auto-editor exists.
|
| 50 |
+
Required because HF containers reset.
|
| 51 |
+
"""
|
| 52 |
+
print("Installing auto-editor...")
|
| 53 |
+
run_command("pip install --no-cache-dir auto-editor")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def auto_edit():
|
| 57 |
+
"""
|
| 58 |
+
Main editing step.
|
| 59 |
+
Removes silence + improves pacing.
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
cmd = f"""
|
| 63 |
+
auto-editor "{INPUT_VIDEO}"
|
| 64 |
+
--margin 0.2s
|
| 65 |
+
--silent-speed 99999
|
| 66 |
+
--video-speed 1
|
| 67 |
+
--audio-normalize peak
|
| 68 |
+
--export mp4
|
| 69 |
+
--output "{OUTPUT_VIDEO}"
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
print("Running auto-editor...")
|
| 73 |
+
run_command(cmd)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def optimize_output():
|
| 77 |
+
"""
|
| 78 |
+
Re-encode for social media compatibility.
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
temp = "optimized.mp4"
|
| 82 |
+
|
| 83 |
+
cmd = f"""
|
| 84 |
+
ffmpeg -y -i "{OUTPUT_VIDEO}"
|
| 85 |
+
-vf scale=1080:1920:force_original_aspect_ratio=decrease,pad=1080:1920:(ow-iw)/2:(oh-ih)/2
|
| 86 |
+
-c:v libx264
|
| 87 |
+
-preset veryfast
|
| 88 |
+
-crf 23
|
| 89 |
+
-c:a aac
|
| 90 |
+
-b:a 128k
|
| 91 |
+
"{temp}"
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
print("Optimizing output...")
|
| 95 |
+
run_command(cmd)
|
| 96 |
+
|
| 97 |
+
os.replace(temp, OUTPUT_VIDEO)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def main():
|
| 101 |
+
print("===== AUTO EDITOR START =====")
|
| 102 |
+
|
| 103 |
+
check_input()
|
| 104 |
+
install_auto_editor()
|
| 105 |
+
auto_edit()
|
| 106 |
+
optimize_output()
|
| 107 |
+
|
| 108 |
+
print("✅ Editing complete:", OUTPUT_VIDEO)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
if __name__ == "__main__":
|
| 112 |
+
main()
|