代码拉取完成,页面将自动刷新
import os.path
import shutil
import subprocess
import time
class C2Gif:
def __init__(self):
if shutil.which('ffmpeg') is None:
raise 'Error: Please set ffmpeg in the path'
@staticmethod
def check():
command = "ffmpeg -version"
ffmpeger = subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='UTF-8')
output, error = ffmpeger.communicate()
print(output)
print(error)
@staticmethod
def cfile2gif(file_path, fps=30, scale=1080):
"""
ffmpeg -i input.webm -vf "fps=15,scale=600:-1:flags=lanczos,palettegen" -y palettegen.png
ffmpeg -i input.webm -i palettegen.png -lavfi "fps=15,scale=600:-1:flags=lanczos[x];[x][1:v]paletteuse" -gifflags -transdiff -y output.gif
param '-ss 00:00:01' to set the start
ffmpeg -i input.mp4 -vf "scale=480:-1:flags=lanczos,fps=15" -gifflags -transdiff -y output.gif
"""
command_paletteuse = f'ffmpeg -i {file_path} -vf "fps={fps},scale={scale}:-1:flags=lanczos,palettegen" -y paletteuse.png'
command_gif = f'ffmpeg -i {file_path} -i paletteuse.png -lavfi "fps={fps},scale={scale}:-1:flags=lanczos[x];[x][1:v]paletteuse" -gifflags -transdiff -y {file_path}.gif'
ffmpeger = subprocess.Popen(command_paletteuse, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
encoding='UTF-8')
output, error = ffmpeger.communicate()
print(output)
print(error)
time.sleep(2)
ffmpeger = subprocess.Popen(command_gif, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
encoding='UTF-8')
output, error = ffmpeger.communicate()
print(output)
print(error)
time.sleep(2)
os.remove('paletteuse.png')
print('Complete')
@staticmethod
def cfile2mp3(file_path):
"""
ffmpeg -i input.mp4 -vn -c:a copy output.mp3
"""
command_voice = f'ffmpeg -i {file_path} -f mp3 -vn {file_path}.mp3'
ffmpeger = subprocess.Popen(command_voice,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='UTF-8')
output, error = ffmpeger.communicate()
print(output)
print(error)
print('Complete')
if __name__ == '__main__':
C2Gif().check()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。