File size: 3,737 Bytes
8ac2232
a83f70a
 
 
8ac2232
a83f70a
8ac2232
a83f70a
 
 
 
e08b1c1
a83f70a
 
 
8ac2232
 
a83f70a
8ac2232
 
 
a83f70a
8ac2232
 
a83f70a
8ac2232
 
 
 
 
a83f70a
8ac2232
 
 
 
 
 
 
 
 
a83f70a
8ac2232
 
 
 
 
 
 
2b5095f
8ac2232
 
 
 
 
 
 
 
 
 
 
 
 
e08b1c1
8ac2232
 
 
 
 
 
 
 
 
 
 
a83f70a
8ac2232
 
 
 
 
a83f70a
 
e08b1c1
 
 
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
from flask import Flask, render_template, request, send_file
import os
import tempfile
import subprocess
import zipfile

# Initialize the Flask app
app = Flask(__name__, template_folder='.')

@app.route('/')
def home():
    return render_template('html.html')  # Loads the HTML interface from the same folder

@app.route('/process', methods=['POST'])
def process_video():
    video_file = request.files['video']
    action = request.form['action']

    # Save the uploaded video to a temporary file
    temp_file = tempfile.NamedTemporaryFile(delete=False)
    video_file.save(temp_file.name)

    # Determine the output path based on selected action
    output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name  # Default output format is mp4

    # Process video based on action
    if action == 'Convert Format':
        # Get the selected format from the form
        selected_format = request.form['format']
        output_path = tempfile.NamedTemporaryFile(delete=False, suffix=f'.{selected_format}').name

        # Check if "Copy Streams" option is enabled
        if 'copy_streams' in request.form:
            # Attempt to copy streams
            result = subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-c', 'copy', output_path])
            if result.returncode != 0:  # If copying fails, re-encode without specifying a codec
                subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, output_path])
        else:
            # Re-encode without specifying a codec
            subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, output_path])

    elif action == 'Trim Video':
        start_time = request.form['start_time']
        duration = request.form['duration']
        if 'copy_streams' in request.form:
            result = subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-ss', start_time, '-t', duration, '-c', 'copy', output_path])
            if result.returncode != 0:
                subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-ss', start_time, '-t', duration, output_path])
        else:
            subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-ss', start_time, '-t', duration, output_path])

    elif action == 'Resize Video':
        width = request.form['width']
        height = request.form['height']
        subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-vf', f'scale={width}:{height}', output_path])

    elif action == 'Extract Audio':
        output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3').name
        subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-vn', '-acodec', 'mp3', output_path])

    elif action == 'Extract Frames':
        frames_folder = tempfile.mkdtemp()
        subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-vf', 'fps=1', f'{frames_folder}/frame_%04d.png'])

        # Create a zip file for the frames
        output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.zip').name
        with zipfile.ZipFile(output_path, 'w') as zipf:
            for root, _, files in os.walk(frames_folder):
                for file in files:
                    zipf.write(os.path.join(root, file), arcname=file)

    elif action == 'Change Video Speed':
        speed_factor = request.form['speed_factor']
        subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-filter:v', f'setpts={1/float(speed_factor)}*PTS', output_path])

    # Send the processed file back if successful
    if os.path.exists(output_path):
        return send_file(output_path, as_attachment=True)
    else:
        return "Error: Processing failed", 500

if __name__ == '__main__':
    from waitress import serve
    # Serving the app using waitress
    serve(app, host='0.0.0.0', port=8080)