File size: 2,091 Bytes
1425afc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
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()