具体原理不知,而且代码写的很拙劣,只是玩一玩
本博客是模仿以上大佬的代码
综合版,ubuntu+ruby
其实我是看另一个大佬的代码ubuntu+python版
只是实现了gif制作
step1:
如何为视频加字幕 Aegisub字幕教学
step2:
研究Aegisub的字幕.ass文件
[Script Info]
; Script generated by Aegisub 3.2.2
; http://www.aegisub.org/
Title: Default Aegisub file
ScriptType: v4.00+
WrapStyle: 0
ScaledBorderAndShadow: yes
YCbCr Matrix: None
[Aegisub Project Garbage]
Audio File: C:/Users/Administrator/Desktop/sorrypy-master/static/sorry/template.mp4
Video File: C:/Users/Administrator/Desktop/sorrypy-master/static/sorry/template.mp4
Video AR Mode: 4
Video AR Value: 1.781250
Active Line: 8
Video Position: 411
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:00.00,0:00:06.87,Default,,0,0,0,,好,别说我是移除联盟的
Dialogue: 0,0:00:05.00,0:00:07.02,Default,,0,0,0,,就算你想诬告我
Dialogue: 0,0:00:07.02,0:00:09.62,Default,,0,0,0,,我有的是钱让律师帮我打官司
Dialogue: 0,0:00:09.62,0:00:11.22,Default,,0,0,0,,我想我根本不用坐牢
Dialogue: 0,0:00:11.22,0:00:12.72,Default,,0,0,0,,你别以为有钱了不起啊
Dialogue: 0,0:00:12.72,0:00:15.62,Default,,0,0,0,,sorry,有钱真的了不起
Dialogue: 0,0:00:15.62,0:00:19.62,Default,,0,0,0,,不过我想你不会明白这种感觉
Dialogue: 0,0:00:19.62,0:00:21.62,Default,,0,0,0,,不明白
Dialogue: 0,0:00:21.62,0:00:23.62,Default,,0,0,0,,
在最下面是字幕的startTime和endTime,最后面是字幕内容
step3:
如何制作属于自己的gif,当然要用到ffmepg
具体原理我也不知道
首先下载ffmepg
sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next
sudo apt-get update
sudo apt-get install ffmpeg
安装好后
终端中运行下面这句话,大概是意思将视频附加字幕转换成gif
运行这句话,需要有以下元素input.mp4,text2.ass
ffmpeg -i input.mp4 -r 8 -vf ass=text2.ass,scale=300:-1 -y sorry.gif
input.mp4是刚刚的为所欲为的视频,text2.ass就是一个小重点了,因为我们要修改他的内部字幕
step4:
修改字幕
首先,你要自己做一个字幕,得到视频中每一句话的startTime和endTime,这样才能对的上嘴型,让人看gif不至于太假
#coding:utf-8 #
# import re
# oldFile = open("sorry.ass","r")
#
# context = oldFile.read()
# # print context
# # print 'finish'
# # print "\n\n"
#
# newContext = re.sub(r'0,,(.*?)\n','0,,{sentence}\n',context)
# newFile = open("test.ass",'w')
# newFile.write(newContext)
# newFile.close()
# 以上是将数据替换成{sentence}
# file = open('test.ass','rb')
# file2 = open('text2.ass','w')
# lines = file.readlines()
# sentences = ['1','2','3','4','5','6','7','8','9']
# i = 0
# for line in lines:
# if '{sentence}' in line:
# line = line.replace('{sentence}',sentences[i])
# i += 1
# print line
# file2.write(line)
#以上实现的将sentence换成对应的字幕
原理:将自己得到的sorry.ass进行正则匹配,将字幕内容都用{sentence}替换
,然后再通过,将想改的字幕依次填入sentences这个list,最后遍历再将{sentence}替换掉。
最后这代码可以不看
具体源代码https://github.com/pompeii666/mp4ToGif
from subprocess import Popen,PIPE
ass_path = 'text2.ass'
gif_path = 'sorry.gif'
video_path = 'input.mp4'
cmd = "ffmpeg -i {video_path} -r 8 -vf ass={ass_path},scale=300:-1 -y {gif_path}" \
.format(video_path=video_path, ass_path=ass_path, gif_path=gif_path)
print(cmd)
p = Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE)
p.wait()
if p.returncode==-1:
print("error")