RandomPersonRR commited on
Commit
b74a135
·
verified ·
1 Parent(s): e08b1c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -40
app.py CHANGED
@@ -3,83 +3,93 @@ import os
3
  import tempfile
4
  import subprocess
5
  import zipfile
 
6
 
7
- # Initialize the Flask app
8
  app = Flask(__name__, template_folder='.')
9
 
10
  @app.route('/')
11
  def home():
12
- return render_template('html.html') # Loads the HTML interface from the same folder
13
 
14
  @app.route('/process', methods=['POST'])
15
  def process_video():
16
  video_file = request.files['video']
17
  action = request.form['action']
 
18
 
19
  # Save the uploaded video to a temporary file
20
  temp_file = tempfile.NamedTemporaryFile(delete=False)
21
  video_file.save(temp_file.name)
22
 
23
- # Determine the output path based on selected action
24
- output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name # Default output format is mp4
25
 
26
  # Process video based on action
27
  if action == 'Convert Format':
28
- # Get the selected format from the form
29
  selected_format = request.form['format']
30
- output_path = tempfile.NamedTemporaryFile(delete=False, suffix=f'.{selected_format}').name
31
 
32
- # Check if "Copy Streams" option is enabled
33
- if 'copy_streams' in request.form:
34
- # Attempt to copy streams
35
- result = subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-c', 'copy', output_path])
36
- if result.returncode != 0: # If copying fails, re-encode without specifying a codec
37
- subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, output_path])
38
  else:
39
- # Re-encode without specifying a codec
40
- subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, output_path])
41
-
42
  elif action == 'Trim Video':
43
  start_time = request.form['start_time']
44
  duration = request.form['duration']
45
- if 'copy_streams' in request.form:
46
- result = subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-ss', start_time, '-t', duration, '-c', 'copy', output_path])
 
 
 
47
  if result.returncode != 0:
48
- subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-ss', start_time, '-t', duration, output_path])
 
49
  else:
50
- subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-ss', start_time, '-t', duration, output_path])
51
-
52
  elif action == 'Resize Video':
53
  width = request.form['width']
54
  height = request.form['height']
55
- subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-vf', f'scale={width}:{height}', output_path])
56
-
57
  elif action == 'Extract Audio':
58
- output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3').name
59
- subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-vn', '-acodec', 'mp3', output_path])
60
-
61
  elif action == 'Extract Frames':
62
- frames_folder = tempfile.mkdtemp()
63
- subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-vf', 'fps=1', f'{frames_folder}/frame_%04d.png'])
64
-
65
- # Create a zip file for the frames
66
- output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.zip').name
67
- with zipfile.ZipFile(output_path, 'w') as zipf:
68
- for root, _, files in os.walk(frames_folder):
69
- for file in files:
70
- zipf.write(os.path.join(root, file), arcname=file)
 
 
 
71
 
 
 
 
 
 
 
 
72
  elif action == 'Change Video Speed':
73
  speed_factor = request.form['speed_factor']
74
- subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-filter:v', f'setpts={1/float(speed_factor)}*PTS', output_path])
 
75
 
76
- # Send the processed file back if successful
77
  if os.path.exists(output_path):
78
  return send_file(output_path, as_attachment=True)
79
- else:
80
- return "Error: Processing failed", 500
81
 
82
  if __name__ == '__main__':
83
- from waitress import serve
84
- # Serving the app using waitress
85
  serve(app, host='0.0.0.0', port=8080)
 
3
  import tempfile
4
  import subprocess
5
  import zipfile
6
+ from waitress import serve
7
 
8
+ # Initialize the Flask app, tell it to look in the current directory for templates
9
  app = Flask(__name__, template_folder='.')
10
 
11
  @app.route('/')
12
  def home():
13
+ return render_template('html.html') # Flask will look in the current directory
14
 
15
  @app.route('/process', methods=['POST'])
16
  def process_video():
17
  video_file = request.files['video']
18
  action = request.form['action']
19
+ copy_streams = 'copy_streams' in request.form # Check if the user selected "copy streams"
20
 
21
  # Save the uploaded video to a temporary file
22
  temp_file = tempfile.NamedTemporaryFile(delete=False)
23
  video_file.save(temp_file.name)
24
 
25
+ output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name # Default output format
 
26
 
27
  # Process video based on action
28
  if action == 'Convert Format':
 
29
  selected_format = request.form['format']
30
+ output_path = tempfile.NamedTemporaryFile(delete=False, suffix=f'.{selected_format}').name # Adjust output format based on user selection
31
 
32
+ if copy_streams:
33
+ # If "copy streams" is selected, try to copy streams without re-encoding
34
+ result = subprocess.run(['ffmpeg', '-i', temp_file.name, '-c:v', 'copy', '-c:a', 'copy', '-y', output_path], capture_output=True)
35
+ if result.returncode != 0:
36
+ # If copying the streams failed, re-encode the video
37
+ subprocess.run(['ffmpeg', '-i', temp_file.name, '-y', output_path])
38
  else:
39
+ subprocess.run(['ffmpeg', '-i', temp_file.name, '-y', output_path])
 
 
40
  elif action == 'Trim Video':
41
  start_time = request.form['start_time']
42
  duration = request.form['duration']
43
+ output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name # Example output format
44
+
45
+ if copy_streams:
46
+ # Try to copy streams during trim operation (if selected)
47
+ result = subprocess.run(['ffmpeg', '-i', temp_file.name, '-ss', start_time, '-t', duration, '-c:v', 'copy', '-c:a', 'copy', '-y', output_path], capture_output=True)
48
  if result.returncode != 0:
49
+ # If copying the streams failed, re-encode the video
50
+ subprocess.run(['ffmpeg', '-i', temp_file.name, '-ss', start_time, '-t', duration, '-y', output_path])
51
  else:
52
+ subprocess.run(['ffmpeg', '-i', temp_file.name, '-ss', start_time, '-t', duration, '-y', output_path])
 
53
  elif action == 'Resize Video':
54
  width = request.form['width']
55
  height = request.form['height']
56
+ output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name # Example output format
57
+ subprocess.run(['ffmpeg', '-i', temp_file.name, '-vf', f'scale={width}:{height}', '-y', output_path])
58
  elif action == 'Extract Audio':
59
+ output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3').name # Output audio in MP3 format
60
+ subprocess.run(['ffmpeg', '-i', temp_file.name, '-vn', '-acodec', 'mp3', '-y', output_path])
 
61
  elif action == 'Extract Frames':
62
+ # Extract frames and save as .jpg files
63
+ frame_dir = tempfile.mkdtemp() # Create a temporary directory to store frames
64
+ output_zip = tempfile.NamedTemporaryFile(delete=False, suffix='.zip').name # Output ZIP file
65
+
66
+ # Extract frames from the video (saving as JPG files)
67
+ subprocess.run(['ffmpeg', '-i', temp_file.name, '-an', f'{frame_dir}/frame_%04d.jpg', '-y'])
68
+
69
+ # Create a ZIP file containing the frames
70
+ with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
71
+ for frame in os.listdir(frame_dir):
72
+ if frame.endswith('.jpg'):
73
+ zipf.write(os.path.join(frame_dir, frame), frame) # Write each frame into the ZIP
74
 
75
+ # Clean up the extracted frames directory
76
+ for frame in os.listdir(frame_dir):
77
+ os.remove(os.path.join(frame_dir, frame))
78
+ os.rmdir(frame_dir)
79
+
80
+ # Send the ZIP file containing the frames
81
+ return send_file(output_zip, as_attachment=True, mimetype='application/zip')
82
  elif action == 'Change Video Speed':
83
  speed_factor = request.form['speed_factor']
84
+ output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name # Example output format
85
+ subprocess.run(['ffmpeg', '-i', temp_file.name, '-filter:v', f'setpts={1/speed_factor}*PTS', '-y', output_path])
86
 
87
+ # Send the processed file back as a download (if applicable)
88
  if os.path.exists(output_path):
89
  return send_file(output_path, as_attachment=True)
90
+
91
+ return 'Error processing video', 500
92
 
93
  if __name__ == '__main__':
94
+ # Run the app using waitress to serve the HTML and video processing functionality
 
95
  serve(app, host='0.0.0.0', port=8080)