Adding FFMpeg

pull/4/head
sgoudham 5 years ago
parent 37ee8b3988
commit ed40a1f2ed

@ -0,0 +1,14 @@
Metadata-Version: 2.1
Name: ffmpeg
Version: 1.4
Summary: ffmpeg python package url [https://github.com/jiashaokun/ffmpeg]
Home-page: https://github.com/jiashaokun/ffmpeg
Author: SkeyJIA
Author-email: 363604236@qq.com
License: MIT Licence
Keywords: python ffmpeg
Platform: any
ffmpeg python package

@ -0,0 +1,15 @@
ffmpeg-1.4.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
ffmpeg-1.4.dist-info/METADATA,sha256=mQ--kqbm0FFb1TRADa3ROWnL4fDGQhBop6KFKC1PEq4,301
ffmpeg-1.4.dist-info/RECORD,,
ffmpeg-1.4.dist-info/WHEEL,sha256=YUYzQ6UQdoqxXjimOitTqynltBCkwY6qlTfTh2IzqQU,97
ffmpeg-1.4.dist-info/top_level.txt,sha256=z628G4f1qm1oii2XkgEn5nrm9Po8Lc5pEIqe96eodlk,7
ffmpeg/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
ffmpeg/__pycache__/__init__.cpython-36.pyc,,
ffmpeg/__pycache__/audio.cpython-36.pyc,,
ffmpeg/__pycache__/image.cpython-36.pyc,,
ffmpeg/__pycache__/stream.cpython-36.pyc,,
ffmpeg/__pycache__/video.cpython-36.pyc,,
ffmpeg/audio.py,sha256=vkIadwpzBmHCtkstUIa90ZvFHW5QmPtuUvc9Uqep-TU,1600
ffmpeg/image.py,sha256=L-pEJ-3-6Muqwu83wzIMqP5sSOqBHXViNNfsUe8ONk4,1187
ffmpeg/stream.py,sha256=t1MDGSUdVxvTCjICuPzNVXHU4Ht3mJgQpqF3a9Fnd7o,6477
ffmpeg/video.py,sha256=Gg7l4KM7_oQUvfAIWO1vrB3tMjDeiPBg2ouHwgKLG_o,8471

@ -0,0 +1,5 @@
Wheel-Version: 1.0
Generator: bdist_wheel (0.34.2)
Root-Is-Purelib: true
Tag: py3-none-any

@ -0,0 +1,60 @@
#!/usr/local/bin/python3
import subprocess
# 调整音频播放速率
def a_speed(input_file, speed, out_file):
try:
cmd = "ffmpeg -y -i %s -filter_complex \"atempo=tempo=%s\" %s" % (input_file, speed, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 音频截取 str_second 开始时间秒数 intercept 截取长度秒。从开始时间截取多少秒的音频
def a_intercept(input_file, str_second, duration, out_file):
try:
cmd = "ffmpeg -y -i %s -ss %s -t %s %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
# 音频拼接 input_file_list = ["1.mp3", "2.mp3"]
def a_split(input_file_list, out_file):
try:
if len(input_file_list) < 2:
return False
split_str = "|"
a_list = split_str.join(input_file_list)
cmd= "ffmpeg -y -i \"concot:%s\" %s" % (a_list, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# 调整音量大小
def a_volume(input_file, volume, out_file):
try:
cmd = "ffmpeg -y -i %s -af volume=%s %s" % (input_file, volume, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False

@ -0,0 +1,50 @@
#!/usr/local/bin/python3
# module sys
import subprocess
# png 转 gif
def img_trans_gif(png_list, out_file):
try:
cmd = "ffmpeg -f image2 -i %s -y %s" % (png_list, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# png 转 视频
def img_trans_video(png_list, duration, out_file):
try:
cmd = "ffmpeg -loop 1 -f image2 -i %s -t %s -vcodec libx264 -y %s" % (png_list, duration, out_file)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False
# gif 转 图片
def gif_trans_img(input_file, out_path, img_prefix, category="png"):
try:
if out_path == "":
return False
out_path = out_path.rstrip("/")
img = img_prefix + "_%d"
out_img = "%s/%s.%s" % (out_path, img, category)
cmd = "ffmpeg -y -i %s %s" % (input_file, out_img)
res = subprocess.call(cmd, shell=True)
if res != 0:
return False
return True
except Exception:
return False

@ -0,0 +1,215 @@
#!/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()

@ -0,0 +1,302 @@
#!/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
Loading…
Cancel
Save