-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
92 lines (71 loc) · 2.89 KB
/
app.py
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
from flask import Flask, request, render_template, send_file, jsonify
import os
import threading
from moviepy.editor import VideoFileClip, AudioFileClip
import librosa
import soundfile as sf
import noisereduce as nr
from scipy.io import wavfile
import numpy as np
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['PROCESSED_FOLDER'] = 'processed'
progress_data = {}
processing_lock = threading.Lock()
# Ensure upload and processed folders exist
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['PROCESSED_FOLDER'], exist_ok=True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload():
if 'video' not in request.files:
return jsonify({"error": "No file uploaded"}), 400
file = request.files['video']
if file.filename == '':
return jsonify({"error": "No file selected"}), 400
# Save the uploaded file
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(file_path)
# Process the video
output_path = os.path.join(app.config['PROCESSED_FOLDER'], f"cleaned_{file.filename}")
try:
process_video(file_path, output_path)
except Exception as e:
return jsonify({"error": f"Processing failed: {str(e)}"}), 500
return jsonify({"message": "Video processed successfully", "file": f"cleaned_{file.filename}"})
@app.route('/download/<filename>')
def download(filename):
return send_file(os.path.join(app.config['PROCESSED_FOLDER'], filename), as_attachment=True)
def process_video(input_path, output_path):
try:
# Load the video safely
video = VideoFileClip(input_path, fps_source="fps")
audio = video.audio
# Extract audio
temp_audio = "temp_audio.wav"
audio.write_audiofile(temp_audio, fps=44100)
# Load the audio file for noise reduction
rate, data = wavfile.read(temp_audio)
# Convert data to float32 for noise reduction
if data.dtype != np.float32:
data = data.astype(np.float32) / np.iinfo(data.dtype).max
# Perform noise reduction
reduced_noise = nr.reduce_noise(y=data, sr=rate)
# Save the cleaned audio
cleaned_audio_path = "cleaned_audio.wav"
wavfile.write(cleaned_audio_path, rate, (reduced_noise * np.iinfo(np.int16).max).astype(np.int16))
# Merge the cleaned audio with the video
cleaned_audio = AudioFileClip(cleaned_audio_path)
final_video = video.set_audio(cleaned_audio)
# Write output
final_video.write_videofile(output_path, codec='libx264', audio_codec='aac')
finally:
# Clean up temporary files
if os.path.exists(temp_audio):
os.remove(temp_audio)
if os.path.exists(cleaned_audio_path):
os.remove(cleaned_audio_path)
if __name__ == '__main__':
app.run(debug=True)