File size: 6,477 Bytes
ddb9253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python2.7
# coding=utf-8

import os

import json

import subprocess


class Stream(object):
    def __init__(self):
        self.cmd = ""
        self.out_file = ""
        self.vcode_type = ""
        self.input_file = ""
        self.word_list_str = ""
        self.subbtitle_file = ""

        self.cmd = []
        self.img_file = []
        self.word_list = []
        self.img_dynamic_list = []

        self.image_list = {}
        self.dynamic_list = {}

    # 输入文件
    def input(self, file):
        self.input_file = file

    # 添加图片
    def img(self, img, x="0", y="0", str_time="0", end_time="0"):
        if img == "":
            return False
        input_info = self.video_info()

        if end_time == "0":
            end_time = float(input_info["format"]["duration"]) + 10.0

        img_data = {
            "img": img,
            "x": str(x),
            "y": str(y),
            "str_time": str(str_time),
            "end_time": str(end_time)
        }

        self.img_file.append(img_data)

        img_input = []
        img_overlay = []

        for val in self.img_file:
            img_input.append(" -i %s" % val["img"])
            img_overlay.append(" overlay=x=%s:y=%s:enable='if(gt(t,%s),lt(t,%s))" % (
                    val["x"],
                    val["y"],
                    val["str_time"],
                    val["end_time"]
                )
            )

        img_input_str = " ".join(img_input)
        img_overlay_str = ",".join(img_overlay)

        self.image_list = {
            "input": img_input_str,
            "overlay": img_overlay_str
        }

    # 添加动态图片 gif apng 等
    def img_dynamic(self, file, x="0", y="0", str_time="0", end_time="0"):
        input_info = self.video_info()
        if file == "":
            return False
        if end_time == "":
            end_time = float(input_info["format"]["duration"]) + 10.0

        apng = {
            "input": " -ignore_loop 0 -i %s" % file,
            "x": str(x),
            "y": str(y),
            "str_time": str(str_time),
            "end_time": str(end_time)
        }
        self.img_dynamic_list.append(apng)

        img_dy_input = []
        img_dy_overlay = []
        for val in self.img_dynamic_list:
            img_dy_input.append(val["input"])
            img_dy_overlay.append(" overlay=x=%s:y=%s:shortest=1:enable='if(gt(t,%s), lt(t,%s))'" % (
                    val["x"],
                    val["y"],
                    val["str_time"],
                    val["end_time"]
                )
            )
        img_dy_input_str = " ".join(img_dy_input)
        img_dy_overlay_str = ",".join(img_dy_overlay)

        self.dynamic_list = {
            "input": img_dy_input_str,
            "overlay": img_dy_overlay_str
        }

    # 添加文字水印
    def word_water_mark(self, c, x="0", y="0", str_time="0", end_time="0", font="", color="white"):
        if font == "":
            return False
        input_info = self.video_info()
        if c == "":
            return False
        if end_time == "0":
            end_time = float(input_info["format"]["duration"]) + 10.0

        text = " drawtext=text='%s':x=%s:y=%s:enable='if(gt(t,%s),lt(t,%s))':fontfile=%s:" \
               "fontcolor=%s" % (c, str(x), str(y), str(str_time), str(end_time), str(font), str(color))
        self.word_list.append(text)

        self.word_list_str = ",".join(self.word_list)

    # 添加字幕文件 subtitles=txt.srt
    def subbtitle(self, file):
        self.subbtitle_file = " subtitles=%s" % file

    # 编码方式 -vcodec
    def vcode(self, code):
        if code == "":
            return False
        self.vcode_type = " -vcodec %s" % code

    # 输出文件
    def out(self, file):
        if file == "":
            return False
        self.out_file = "%s" % file

    # 执行脚本
    def run(self):
        if self.input_file == "":
            return False
        im = "ffmpeg -i %s" % self.input_file
        ov = ""

        if len(self.dynamic_list) > 0 and self.dynamic_list["input"] != "":
            im = "%s %s" % (im, self.dynamic_list["input"])

            if ov != "":
                ov = "%s,%s" % (ov, self.dynamic_list["overlay"])
            else:
                ov = self.dynamic_list["overlay"]
        if len(self.image_list) > 0:
            im = "%s %s" % (im, self.image_list["input"])

            if ov != "":
                ov = "%s,%s" % (ov, self.dynamic_list["overlay"])
            else:
                ov = self.dynamic_list["overlay"]

        # 文字水印
        if self.word_list_str != "":
            if ov != "":
                ov = "%s,%s" % (ov, self.word_list_str)
            else:
                ov = self.word_list_str

        # 字幕
        if self.subbtitle_file != "":
            if ov != "":
                ov = "%s,%s" % (ov, self.subbtitle_file)
            else:
                ov = self.subbtitle_file

        if self.vcode_type != "":
            self.cmd = "%s -filter_complex \"%s\" -y %s %s" % (im, ov, self.vcode_type, self.out_file)
        else:
            self.cmd = "%s -filter_complex \"%s\" -y %s" % (im, ov, self.out_file)

        self.do()

    # 获取视频的相关时长信息
    def video_info(self):
        result = {}
        if os.path.isfile(self.input_file) is False:
            return result

        cmd = ['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', self.input_file]
        returned_data = subprocess.check_output(cmd)
        return json.loads(returned_data.decode('utf-8'))

    # 执行命令
    def do(self):
        if self.cmd == "":
            return False
        res = subprocess.call(self.cmd, shell=True)
        if res != 0:
            return False
        return True


if __name__ == '__main__':
    stream = Stream()
    stream.input("face.mp4")
    stream.img("t1.png")
    stream.img("t2.png", "10", y=10, str_time=5, end_time=10)
    stream.img_dynamic("t1.apng", x=10, y=10, str_time=5, end_time=10)
    stream.img_dynamic("t2.apng", x=10, y=10, str_time=5, end_time=9)
    stream.word_water_mark("测试文字水印1", x="10", y="10", str_time="0", end_time="20", font="ttf.ttf", color="white")
    stream.word_water_mark("测试文字水印2", x="10", y="10", str_time="0", end_time="20", font="ttf.ttf", color="white")
    stream.subbtitle("srt.srt")
    stream.out("out.mp4")
    stream.run()