Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,9 @@ import os
|
|
| 4 |
import logging
|
| 5 |
import threading
|
| 6 |
import time
|
| 7 |
-
import uuid
|
| 8 |
-
import random
|
| 9 |
from datetime import datetime
|
| 10 |
from huggingface_hub import HfApi, hf_hub_download
|
| 11 |
from werkzeug.utils import secure_filename
|
| 12 |
-
import ffmpeg
|
| 13 |
|
| 14 |
app = Flask(__name__)
|
| 15 |
app.secret_key = 'supersecretkey' # Замените на безопасный ключ в продакшене
|
|
@@ -85,26 +82,6 @@ def periodic_backup():
|
|
| 85 |
upload_db_to_hf()
|
| 86 |
time.sleep(15)
|
| 87 |
|
| 88 |
-
# Генерация случайного кадра из видео
|
| 89 |
-
def generate_thumbnail(video_path):
|
| 90 |
-
try:
|
| 91 |
-
probe = ffmpeg.probe(video_path)
|
| 92 |
-
duration = float(probe['format']['duration'])
|
| 93 |
-
random_time = random.uniform(0, duration) # Случайное время в пределах длительности видео
|
| 94 |
-
thumbnail_filename = f"{uuid.uuid4()}.jpg"
|
| 95 |
-
thumbnail_path = os.path.join('uploads', thumbnail_filename)
|
| 96 |
-
(
|
| 97 |
-
ffmpeg
|
| 98 |
-
.input(video_path, ss=random_time)
|
| 99 |
-
.output(thumbnail_path, vframes=1, format='image2', q:v=2)
|
| 100 |
-
.overwrite_output()
|
| 101 |
-
.run(quiet=True)
|
| 102 |
-
)
|
| 103 |
-
return thumbnail_path
|
| 104 |
-
except Exception as e:
|
| 105 |
-
logging.error(f"Ошибка генерации превью: {e}")
|
| 106 |
-
return None
|
| 107 |
-
|
| 108 |
# Регистрация пользователя
|
| 109 |
@app.route('/register', methods=['GET', 'POST'])
|
| 110 |
def register():
|
|
@@ -294,8 +271,8 @@ def video_feed():
|
|
| 294 |
<h1>Лента видео</h1>
|
| 295 |
<div class="video-grid">
|
| 296 |
{% for video in videos %}
|
| 297 |
-
<a href="{{ url_for('video_page',
|
| 298 |
-
<img src="https://
|
| 299 |
<h2>{{ video['title'] }}</h2>
|
| 300 |
<p>{{ video['description'] }}</p>
|
| 301 |
<p>Загрузил: {{ video['uploader'] }} | {{ video['upload_date'] }}</p>
|
|
@@ -311,13 +288,13 @@ def video_feed():
|
|
| 311 |
</body>
|
| 312 |
</html>
|
| 313 |
'''
|
| 314 |
-
return render_template_string(html, videos=videos, is_authenticated=is_authenticated, username=username
|
| 315 |
|
| 316 |
# Страница отдельного видео (доступна всем)
|
| 317 |
-
@app.route('/video/<
|
| 318 |
-
def video_page(
|
| 319 |
data = load_data()
|
| 320 |
-
video = next((v for v in data['videos'] if v['
|
| 321 |
if not video:
|
| 322 |
return "Видео не найдено", 404
|
| 323 |
|
|
@@ -454,7 +431,7 @@ def profile():
|
|
| 454 |
{% if user_videos %}
|
| 455 |
{% for video in user_videos %}
|
| 456 |
<div class="video-item">
|
| 457 |
-
<img src="https://
|
| 458 |
<h3>{{ video['title'] }}</h3>
|
| 459 |
<p>{{ video['description'] }}</p>
|
| 460 |
<p>{{ video['upload_date'] }}</p>
|
|
@@ -477,7 +454,7 @@ def profile():
|
|
| 477 |
</body>
|
| 478 |
</html>
|
| 479 |
'''
|
| 480 |
-
return render_template_string(html, username=username, user_videos=user_videos
|
| 481 |
|
| 482 |
# Страница загрузки видео (только для авторизованных)
|
| 483 |
@app.route('/upload', methods=['GET', 'POST'])
|
|
@@ -500,24 +477,7 @@ def upload_video():
|
|
| 500 |
os.makedirs('uploads', exist_ok=True)
|
| 501 |
video_file.save(temp_path)
|
| 502 |
|
| 503 |
-
#
|
| 504 |
-
thumbnail_path = generate_thumbnail(temp_path)
|
| 505 |
-
if thumbnail_path:
|
| 506 |
-
thumbnail_filename = os.path.basename(thumbnail_path)
|
| 507 |
-
api = HfApi()
|
| 508 |
-
api.upload_file(
|
| 509 |
-
path_or_fileobj=thumbnail_path,
|
| 510 |
-
path_in_repo=f"thumbnails/{thumbnail_filename}",
|
| 511 |
-
repo_id=REPO_ID,
|
| 512 |
-
repo_type="dataset",
|
| 513 |
-
token=HF_TOKEN_WRITE,
|
| 514 |
-
commit_message=f"Добавлено превью для видео: {title}"
|
| 515 |
-
)
|
| 516 |
-
os.remove(thumbnail_path)
|
| 517 |
-
else:
|
| 518 |
-
thumbnail_filename = "default_thumb.jpg" # Запасной вариант
|
| 519 |
-
|
| 520 |
-
# Загрузка видео на Hugging Face
|
| 521 |
api = HfApi()
|
| 522 |
api.upload_file(
|
| 523 |
path_or_fileobj=temp_path,
|
|
@@ -528,20 +488,19 @@ def upload_video():
|
|
| 528 |
commit_message=f"Добавлено видео: {title} пользователем {uploader}"
|
| 529 |
)
|
| 530 |
|
| 531 |
-
# Обновление базы данных
|
| 532 |
data = load_data()
|
| 533 |
-
video_id =
|
| 534 |
data['videos'].append({
|
| 535 |
'id': video_id,
|
| 536 |
'title': title,
|
| 537 |
'description': description,
|
| 538 |
'filename': filename,
|
| 539 |
-
'thumbnail': thumbnail_filename,
|
| 540 |
'uploader': uploader,
|
| 541 |
'upload_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 542 |
})
|
| 543 |
save_data(data)
|
| 544 |
-
logging.info(f"Видео {title}
|
| 545 |
|
| 546 |
if os.path.exists(temp_path):
|
| 547 |
os.remove(temp_path)
|
|
|
|
| 4 |
import logging
|
| 5 |
import threading
|
| 6 |
import time
|
|
|
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
from huggingface_hub import HfApi, hf_hub_download
|
| 9 |
from werkzeug.utils import secure_filename
|
|
|
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
app.secret_key = 'supersecretkey' # Замените на безопасный ключ в продакшене
|
|
|
|
| 82 |
upload_db_to_hf()
|
| 83 |
time.sleep(15)
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
# Регистрация пользователя
|
| 86 |
@app.route('/register', methods=['GET', 'POST'])
|
| 87 |
def register():
|
|
|
|
| 271 |
<h1>Лента видео</h1>
|
| 272 |
<div class="video-grid">
|
| 273 |
{% for video in videos %}
|
| 274 |
+
<a href="{{ url_for('video_page', title=video['title']) }}" class="video-item">
|
| 275 |
+
<img src="https://via.placeholder.com/300x200?text={{ video['title'] }}" alt="{{ video['title'] }}">
|
| 276 |
<h2>{{ video['title'] }}</h2>
|
| 277 |
<p>{{ video['description'] }}</p>
|
| 278 |
<p>Загрузил: {{ video['uploader'] }} | {{ video['upload_date'] }}</p>
|
|
|
|
| 288 |
</body>
|
| 289 |
</html>
|
| 290 |
'''
|
| 291 |
+
return render_template_string(html, videos=videos, is_authenticated=is_authenticated, username=username)
|
| 292 |
|
| 293 |
# Страница отдельного видео (доступна всем)
|
| 294 |
+
@app.route('/video/<title>')
|
| 295 |
+
def video_page(title):
|
| 296 |
data = load_data()
|
| 297 |
+
video = next((v for v in data['videos'] if v['title'] == title), None)
|
| 298 |
if not video:
|
| 299 |
return "Видео не найдено", 404
|
| 300 |
|
|
|
|
| 431 |
{% if user_videos %}
|
| 432 |
{% for video in user_videos %}
|
| 433 |
<div class="video-item">
|
| 434 |
+
<img src="https://via.placeholder.com/300x200?text={{ video['title'] }}" alt="{{ video['title'] }}">
|
| 435 |
<h3>{{ video['title'] }}</h3>
|
| 436 |
<p>{{ video['description'] }}</p>
|
| 437 |
<p>{{ video['upload_date'] }}</p>
|
|
|
|
| 454 |
</body>
|
| 455 |
</html>
|
| 456 |
'''
|
| 457 |
+
return render_template_string(html, username=username, user_videos=user_videos)
|
| 458 |
|
| 459 |
# Страница загрузки видео (только для авторизованных)
|
| 460 |
@app.route('/upload', methods=['GET', 'POST'])
|
|
|
|
| 477 |
os.makedirs('uploads', exist_ok=True)
|
| 478 |
video_file.save(temp_path)
|
| 479 |
|
| 480 |
+
# Загрузка на Hugging Face
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
api = HfApi()
|
| 482 |
api.upload_file(
|
| 483 |
path_or_fileobj=temp_path,
|
|
|
|
| 488 |
commit_message=f"Добавлено видео: {title} пользователем {uploader}"
|
| 489 |
)
|
| 490 |
|
| 491 |
+
# Обновление базы данных
|
| 492 |
data = load_data()
|
| 493 |
+
video_id = f"video_{len(data['videos']) + 1}"
|
| 494 |
data['videos'].append({
|
| 495 |
'id': video_id,
|
| 496 |
'title': title,
|
| 497 |
'description': description,
|
| 498 |
'filename': filename,
|
|
|
|
| 499 |
'uploader': uploader,
|
| 500 |
'upload_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 501 |
})
|
| 502 |
save_data(data)
|
| 503 |
+
logging.info(f"Видео {title} загружено пользователем {uploader}")
|
| 504 |
|
| 505 |
if os.path.exists(temp_path):
|
| 506 |
os.remove(temp_path)
|