-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
169 lines (142 loc) · 5.55 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import webview
import threading
import subprocess
from flask import Flask, request, render_template, send_file, jsonify
from flask_cors import CORS
from ffmpeg_utils import compress_media, get_file_size, format_size, estimate_compressed_size, convert_video, convert_image
import yt_dlp as youtube_dl
import zipfile
import mimetypes
app = Flask(__name__)
CORS(app)
def clean_youtube_url(url):
return url.split('&')[0]
@app.route('/')
def index():
return render_template('index.html')
@app.route('/compress', methods=['POST'])
def compress():
file = request.files['file']
crf = int(request.form['crf'])
filename = file.filename
filepath = os.path.join('Загрузки', filename)
file.save(filepath)
output_path = os.path.join('Сжатые', filename)
compress_media(filepath, output_path, crf)
os.remove(filepath) # Удаление исходного файла
open_folder(output_path)
return send_file(output_path, as_attachment=True)
@app.route('/estimate', methods=['POST'])
def estimate():
file = request.files['file']
crf = int(request.form['crf'])
filename = file.filename
filepath = os.path.join('Загрузки', filename)
file.save(filepath)
estimated_size = estimate_compressed_size(filepath, crf)
return jsonify({'estimated_size': format_size(estimated_size)})
@app.route('/convert', methods=['POST'])
def convert():
file = request.files['file']
output_format = request.form['format']
filename = file.filename
filepath = os.path.join('Загрузки', filename)
file.save(filepath)
if output_format in ['gif', 'mp3', 'mp4', 'avi', 'mov', 'webm']:
output_path = convert_video(filepath, output_format)
else:
output_path = convert_image(filepath, output_format)
os.remove(filepath) # Удаление исходного файла
open_folder(output_path)
return send_file(output_path, as_attachment=True)
@app.route('/file_info', methods=['POST'])
def file_info():
file = request.files['file']
filename = file.filename
filepath = os.path.join('Загрузки', filename)
file.save(filepath)
mime_type, _ = mimetypes.guess_type(filepath)
if mime_type.startswith('video/'):
file_type = 'video'
formats = ['gif', 'mp3', 'mp4', 'avi', 'mov', 'webm']
elif mime_type.startswith('image/'):
file_type = 'image'
formats = ['png', 'jpg', 'jpeg']
else:
file_type = 'unknown'
formats = []
return jsonify({'file_type': file_type, 'formats': formats, 'filename': filename, 'size': format_size(get_file_size(filepath))})
@app.route('/preview', methods=['GET'])
def preview():
url = request.args.get('url')
if not url:
return jsonify({'error': 'URL is required'}), 400
clean_url = clean_youtube_url(url)
ydl_opts = {
'quiet': True,
'skip_download': True,
'force_generic_extractor': True,
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
try:
info = ydl.extract_info(clean_url, download=False)
return jsonify({
'title': info.get('title'),
'thumbnail': info.get('thumbnail')
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/download', methods=['POST'])
def download():
data = request.get_json()
links = data.get('links', [])
format = data.get('format', 'mp4')
ydl_opts = {
'format': 'bestvideo+bestaudio/best',
'outtmpl': 'Загрузки/%(title)s.%(ext)s',
'quiet': True,
'merge_output_format': 'mp4',
}
download_paths = []
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
for link in links:
clean_link = clean_youtube_url(link)
try:
result = ydl.extract_info(clean_link)
download_paths.append(ydl.prepare_filename(result))
except Exception as e:
return jsonify({'error': str(e)}), 500
zip_path = 'Загрузки/videos.zip'
with zipfile.ZipFile(zip_path, 'w') as zipf:
for path in download_paths:
if format == 'mp3':
mp3_path = path.replace('.mp4', '.mp3')
ffmpeg_command = f'ffmpeg -i "{path}" -q:a 0 -map a "{mp3_path}"'
subprocess.run(ffmpeg_command, shell=True, check=True)
path = mp3_path
if os.path.exists(path):
zipf.write(path, os.path.basename(path))
else:
return jsonify({'error': f'File not found: {path}'}), 500
open_folder(zip_path)
return send_file(zip_path, as_attachment=True)
def open_folder(path):
folder_path = os.path.dirname(path)
if os.name == 'nt':
os.startfile(folder_path)
elif os.name == 'posix':
subprocess.Popen(['xdg-open', folder_path])
else:
raise OSError(f'Unsupported OS: {os.name}')
def start_flask():
os.makedirs('Загрузки', exist_ok=True)
os.makedirs('Сжатые', exist_ok=True)
os.makedirs('Обработанные', exist_ok=True)
app.run(debug=False, use_reloader=False)
if __name__ == '__main__':
flask_thread = threading.Thread(target=start_flask)
flask_thread.daemon = True
flask_thread.start()
webview.create_window('Компрессор медиафайлов', 'http://127.0.0.1:5000', width=1000, height=800)
webview.start()