有一个有趣的开源代码,可以生成有趣的gif表情,觉得很有意思想集成玩玩。Github地址:
https://github.com/q809198545/node-sorry
坑一:
如果你是mac用户,在安装ffmpeg 的时候,如果想要生存Gif表情一定要记得 安装好libass,否则会报错。
命令如下:
brew install ffmpeg –with-libass
当然最好一次性把需要的lib全安装了。
坑二:
由于本人的服务器是windows的,所以在部署服务的时候,本以为windows只需要把exe文件安装好就行了。
但是这个不一样:
第一步,最好采用作者提供ffmpeg.exe 否则可能缺少libass 库;
第二步,这个只需要把ffmpeg.exe的目录添加到path环境变量中即可,不是点击next安装类型;
第三步,最坑的一步,代码中生成giff文件归根结底为执行下面这行命令:
代码如下:
var cmd = "ffmpeg -i " + videoPath + " -r 8 -vf ass=" + assPath + ",scale=300:-1 -y " + gifPath
运行时的实际命令如下:
ffmpeg -i D:\Pros\ProsThinkjs/www/static/templates/sorry/template.mp4 -r 8 -vf "ass=D:/Pros/ProsThinkjs/www/static/runtime/cache/sorry-c208aced3e7f9a28cffe2af47843e64d.gif.ass,scale=300:-2" -y D:\Pros\ProsThinkjs/www/static/runtime/cache/sorry-c208aced3e7f9a28cffe2af47843e64d.gif
看着好像一切都很正确来着,但是会报下面的错误:
Press [q] to stop, [?] for help
[ass @ 0000000003e4e8e0] Unable to parse option value "/Pros/Pro
sThinkjs/www/static/runtime/cache/sorry-c208aced3e7f9a28cffe2af47843e64d.gif.ass
" as image size
Last message repeated 1 times
[ass @ 0000000003e4e8e0] Error setting option original_size to value /Pr
os/ProsThinkjs/www/static/runtime/cache/sorry-c208aced3e7f9a28cffe2af478
43e64d.gif.ass.
[Parsed_ass_0 @ 0000000003e4e800] Error applying options to the filter.
[AVFilterGraph @ 000000000298d120] Error initializing filter 'ass' with args 'D:/Pros/ProsThinkjs/www/static/runtime/cache/sorry-c208aced3e7f9a2
8cffe2af47843e64d.gif.ass'
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0
Conversion failed!
很明显,这个命令在执行的时候,ass文件的路径变了。
纠结了好久发现,ass=后面这个文件路径,在windows系统下面不能够使用盘符(如D:\xxxx),只能使用相对路径。
测试了如下几个命令都是OK的:
success:
ffmpeg -i template.mp4 -vf "ass=test.ass" out.gif
ffmpeg -i template.mp4 -vf ass=test.ass out2.gif
>ffmpeg -i template.mp4 -vf ass=test.ass,scale=300:-1 -y out3.gif
>ffmpeg -i template.mp4 -r 8 -vf ass=test.ass,scale=300:-1 -y out4.gif
ffmpeg -i H:/test/template.mp4 -r 8 -vf ass=test.ass out4.gif
ffmpeg -i H:/test/template.mp4 -r 8 -vf ass=test.ass H:/test/out4.gif
H:\>ffmpeg -i H:/test/template.mp4 -r 8 -vf ass=./test/test.ass H:/test/out4.gi
f
可以发现ass有没有双引号都无所谓。
那么在部署时候,就需要改一下代码了,判断系统是否时windows系统,如果是的话,ass文件路径返回相对路径,否则可以返回绝对路径
//此处是个坑,在windows平台上面不能返回绝对路径,因为带有盘符,导致ass=xxx 无法解析路径出错,只能使用相对路径。
var osType = os.type();
if(osType.toLocaleLowerCase().indexOf('windows') >=0){
return "./www/static/runtime/cache/" + filename + ".ass";//服务器需要携带‘.’自己的那台华硕电脑不需要
}else{
return outputFilePath;//outputFilePath = rootPath + "/www/static/runtime/cache/" + filename + ".ass
}
第四步,还有一个小坑就是返回相对路径时,在我的windows电脑上面前面可以不带 . 但在我的服务器上面如果不带点也无法找到文件,故还需注意一下。
如果大家有啥更好的意见欢迎留言评论。