Spaces:
Running
Running
File size: 8,471 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
#!/usr/local/bin/python3
# module sys
import subprocess
def ins_img(input_file, img_data, out_file):
try:
if len(img_data) <= 0:
return False
img_list = []
img_list_str = " -i "
png_complex = []
complex_png_str = ","
for img in img_data:
if len(img["x"]) == 0:
img["x"] = "0"
if len(img["y"]) == 0:
img["y"] = "0"
img_list.append(img["img"])
if len(img["str_time"]) > 0:
if len(img["end_time"]) > 0:
cmp_str = "overlay=x=%s:y=%s:enable='if(gt(t,%s),lt(t,%s))'" % (img["x"], img["y"], img["str_time"], img["end_time"])
else:
cmp_str = "overlay=x=%s:y=%s:enable='if(gt(t,%s))'" % (img["x"], img["y"], img["str_time"])
else:
cmp_str = "overlay=x=%s:y=%s" % (img["x"], img["y"])
png_complex.append(cmp_str)
img_str_list = img_list_str.join(img_list)
complex_png_str = complex_png_str.join(png_complex)
cmd = "ffmpeg -i %s -i %s -filter_complex \"%s\" -y %s" % (input_file, img_str_list, complex_png_str, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 视频添加动图 gif apng
def ins_dynamic_img(input_file, img_data, out_file):
try:
if img_data["img"] == "":
return False
if img_data["x"] == "":
img_data["x"] = 0
if img_data["y"] == "":
img_data["y"] = 0
if img_data["str_time"] != "":
if img_data["end_time"] != "":
comp = "overlay=x=%s:y=%s:shortest=1:enable='if(gt(t,%s), lt(t,%s))'" % (img_data["x"], img_data["y"],
img_data["str_time"],
img_data["end_time"])
else:
comp = "overlay=x=%s:y=%s:shortest=1:enable='if(gt(t,%s)'" % (img_data["x"], img_data["y"],
img_data["str_time"])
else:
comp = "overlay=x=%s:y=%s:shortest=1"
cmd = "ffmpeg -i %s -ignore_loop 0 -i %s -filter_complex \"%s\" -y %s" % (input_file, img_data["img"], comp,
out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 视频静音 分离音频流
def separate_audio(input_file, out_file):
try:
cmd = "ffmpeg -y -i %s -vcodec copy -an %s" % (input_file, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 视频静音 使用静音帧 为视频静音
def video_ins_mute_audio(input_file, mute_mp3_file, out_file):
try:
cmd = "ffmpeg -y -i %s -filter_complex '[1:0]apad' -shortest %s" % (input_file, mute_mp3_file, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 视频设置分辨率 及 码率
def trans_code(input_file, width, height, rate, out_file):
try:
cmd = "ffmpeg -y -i %s -s %sx%s -b %sk -acodec copy %s" % (input_file, width, height, rate, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 视频添加弹幕
def ins_barrage(input_file, barrage, out_file):
try:
if len(barrage) == 0:
return False
bag = []
bag_str = ", "
vf_str = ""
for val in barrage:
if val["fontsize"] == "":
val["fontsize"] = 40
if val["fontcolor"] == "":
val["fontcolor"] = "white"
if val["y"] == "":
val["y"] = "100"
if val["str_time"] == "":
val["str_time"] = 0
else:
val["str_time"] = int(val["str_time"])
if val["speet"] == "":
val["speet"] = 150
else:
val["speet"] = int(val["speet"])
txt = "drawtext=text='%s':fontcolor=%s:fontsize=%s:fontfile=%s:y=%s:x=w-(t-%d)*%d:enable='gte(t,%d)'" % (
val["context"],
val["fontcolor"],
val["fontsize"],
val["fontfile"],
val["y"],
val["str_time"],
val["speet"],
val["str_time"]
)
bag.append(txt)
vf_str = bag_str.join(bag)
cmd = "ffmpeg -y -i %s -vf \"%s\" %s" % (input_file, vf_str, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 调整视频速率 speed 小于 1 减速,大于 1 加速 1 等速
def playback_speed(input_file, speed, out_file):
try:
if speed == "":
speed = "1"
cmd = "ffmpeg -y -i %s -filter_complex \"setpts=PTS/%s\" %s" % (input_file, speed, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 视频倒放 ( 视频 + 音频 )
def a_v_reverse(input_file, out_file):
try:
cmd = "ffmpeg -y -i %s -vf vf reverse -af areverse %s " % (input_file, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 视频倒放 (视频)
def v_reverse(input_file, out_file):
try:
cmd = "ffmpeg -y -i %s -vf vf reverse %s " % (input_file, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 视频截取 截取 duration 时长的视频 从 str_second 开始截取
def v_intercept(input_file, str_second, duration, out_file):
try:
cmd = "ffmpeg -y -i %s -ss %s -t %s -f mp4 %s" % (input_file, str_second, duration, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 视频合并 严格模式 文件协议合并
def strict_v_merge(input_file, out_file):
try:
cmd = "ffmpeg -y -f concat -safe 0 -i %s -acodec copy %s" % (input_file, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 视频合并 有损模式 input_file_list = ["1.mp4", "2.ts", "3.flv"]
def damage_v_merge(input_file_list, out_file):
try:
if len(input_file_list) < 2:
return False
video = []
video_n = len(input_file_list)
video_str = " -i "
comp_list = []
comp_str = " "
i = 0
for val in input_file_list:
video.append(val)
v_str = "[%s:a][%s:v]" % (i, i)
comp_list.append(v_str)
i += 1
video_list = video_str.join(video)
com_list_str = comp_str.join(comp_list)
cmd = "ffmpeg -y -i %s -filter_complex \"%s concat=n=%d:v=1:a=1\" -vcodec h264_nvenc %s" % (
video_list,
com_list_str,
video_n,
out_file
)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 视频转 图片
def video_trans_img(input_file, out_path, img_prefix, category="png"):
try:
out_path = out_path.rstrip("/")
img = img_prefix + "_%d"
out_img = "%s/%s.%s" % (out_path, img, category)
cmd = "ffmpeg -i %s -f image2 %s" % (input_file, out_img)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
|