File size: 1,700 Bytes
f7e17ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np
import tempfile
import os
from moviepy import VideoFileClip


def enhance_video(video_file, kernel):
    # Сохранение во временный файл
    with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_input:
        temp_input.write(video_file.getbuffer())
        input_path = temp_input.name

    # Обработка видео
    cap = cv2.VideoCapture(input_path)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = cap.get(cv2.CAP_PROP_FPS)

    # Подготовка выходного файла
    with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_output:
        output_path = temp_output.name
        out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))

        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                break
            processed_frame = cv2.filter2D(frame, -1, kernel)
            out.write(processed_frame)

        cap.release()
        out.release()

    # Микширование аудио
    final_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
    video_clip = VideoFileClip(output_path)
    audio_clip = VideoFileClip(input_path).audio

    if audio_clip:
        video_clip.audio = audio_clip

    video_clip.write_videofile(
        final_output,
        codec='libx264',
        audio_codec='aac',
        logger=None
    )

    # Очистка временных файлов
    for path in [input_path, output_path]:
        if os.path.exists(path):
            os.remove(path)

    return final_output