File size: 4,217 Bytes
adb363a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe6c466
 
 
 
 
 
 
 
adb363a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbcaba6
 
 
 
 
 
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
import subprocess
import ffmpeg
import imagesize

class CommonUtil:
    valid_image_exts = (".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".webp")
    valid_video_exts = (".mp4", ".mov", ".avi", ".webm")
    valid_audio_exts = (".mp3", ".wav")
    valid_template_ext = ".npz"

    valid_min_media_dim = 480  # pixels
    valid_max_media_dim = 3840

    valid_min_media_duration = 0.1  # seconds
    valid_max_media_duration = 120  # seconds

    valid_min_sample_rate = 16000
    valid_max_sample_rate = 44100

    valid_video_fps = 30  # fps

    @staticmethod
    def check_dim(width, height):
        min_d = CommonUtil.valid_min_media_dim
        max_d = CommonUtil.valid_max_media_dim
        if width < min_d or width > max_d or height < min_d or height > max_d:
            return False
        return True
    
    @staticmethod
    def check_duration(duration):
        if duration < CommonUtil.valid_min_media_duration:
            return False

        if duration > CommonUtil.valid_max_media_duration:
            return False

        return True
    
    @staticmethod
    def check_fps(fps):
        if fps != CommonUtil.valid_video_fps:
            return False
        return True
    
    @staticmethod
    def check_sample_rate(sample_rate):
        if sample_rate < CommonUtil.valid_min_sample_rate:
            return False
        if sample_rate > CommonUtil.valid_max_sample_rate:
            return False
        return True
    
    @staticmethod
    def get_audio_stream(video_path):
        probe = ffmpeg.probe(video_path)
        return next((stream for stream in probe["streams"] if stream["codec_type"] == "audio"), None)

    @staticmethod
    def get_video_stream(video_path):
        probe = ffmpeg.probe(video_path)
        return next((stream for stream in probe["streams"] if stream["codec_type"] == "video"), None)
    
    @staticmethod
    def exec_cmd(cmd):
        return subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    @staticmethod
    def get_media_properties(media):
        is_image = CommonUtil.is_image(media)
        is_video = CommonUtil.is_video(media)
        is_audio = CommonUtil.is_audio(media)

        if is_image:
            width, height = imagesize.get(media)
            return (is_image, is_video, is_audio, width, height, -1, -1)

        elif is_video:
            video_stream = CommonUtil.get_video_stream(media)
            duration = float(video_stream["duration"])
            width = int(video_stream["width"])
            height = int(video_stream["height"])
            sample_rate = video_stream["r_frame_rate"]
            if sample_rate == "30/1":
                sample_rate = int(30)
            return (is_image, is_video, is_audio, width, height, duration, sample_rate)

        elif is_audio:
            audio_stream = CommonUtil.get_audio_stream(media)
            duration = float(audio_stream["duration"])
            sample_rate = int(audio_stream["sample_rate"])
            return (is_image, is_video, is_audio, -1, -1, duration, sample_rate)
        else:
            return (is_image, is_video, is_audio, -1, -1, -1, -1)

    @staticmethod
    def is_image(file_path):
        return file_path.lower().endswith(CommonUtil.valid_image_exts)

    @staticmethod
    def is_video(file_path):
        return file_path.lower().endswith(CommonUtil.valid_video_exts)

    @staticmethod
    def is_audio(file_path):
        return file_path.lower().endswith(CommonUtil.valid_audio_exts)

    @staticmethod
    def is_template(file_path):
        if file_path.endswith(CommonUtil.valid_template_ext):
            return True
        return False

    @staticmethod
    def change_video_fps(input_file, output_file, fps=20, codec="libx264", crf=12):
        cmd = f'ffmpeg -i "{input_file}" -c:v {codec} -crf {crf} -r {fps} "{output_file}" -y'
        CommonUtil.exec_cmd(cmd)

    @staticmethod
    def change_audio_sample_rate(input_file, output_file, target_sample_rate=22050):
        stream = ffmpeg.input(input_file)
        audio = stream.audio
        output_stream = ffmpeg.output(audio, output_file, ar=target_sample_rate)
        ffmpeg.run(output_stream, overwrite_output=True)