Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions controller/render.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const fs = require('fs')
const md5 = require('md5')
const path = require('path')
const ffmpeg = require('ffmpeg')
const rootPath = path.resolve(__dirname + "/..")
var execSync = require('child_process').execSync;

// 默认大小
const __SIZE = 500;

// 生成对应字幕
function renderAss(templateName, sentences, filename) {
outputFilePath = "/public/cache/" + filename + ".ass"
templatePath = rootPath + "/public/templates/" + templateName + "/template.ejs"
Expand All @@ -12,6 +17,7 @@ function renderAss(templateName, sentences, filename) {
renderedAssText = require('ejs').render(template, {
'sentences': sentences
})

fs.writeFileSync(rootPath + outputFilePath, renderedAssText)
return outputFilePath
}
Expand All @@ -20,14 +26,51 @@ function makeGifWithFfmpeg(templateName, sentences, filename) {
assPath = renderAss(templateName, sentences, filename)
gifPath = "./public/cache/" + filename
videoPath = "./public/templates/" + templateName + "/template.mp4"
console.log(assPath, gifPath, videoPath)
var cmd = "ffmpeg -i " + videoPath + " -r 8 -vf ass=." + assPath + ",scale=300:-1 -y " + gifPath
console.log(cmd);

// 使用默认尺寸
var _size = __SIZE;

// 动态获取尺寸
try {
execSync(cmd);
var process = new ffmpeg(videoPath);
process.then(function (video) {
// 视频尺寸和默认尺寸取小
if(video.metadata.video.resolution.w < __SIZE)
{
_size = video.metadata.video.resolution.w;
}

console.log("视频尺寸:" + video.metadata.video.resolution.w);
}, function (err) {
console.log('Error: ' + err);
}).then(function () {
console.log(assPath, gifPath, videoPath)
var cmd = "ffmpeg -i " + videoPath + " -r 8 -vf ass=." + assPath + ",scale=" + _size + ":-1 -y " + gifPath
console.log(cmd);
try {
execSync(cmd);

console.log("生成gif成功")
} catch (e) {
console.log(e);
}
});
} catch (e) {
console.log(e);
console.log(e.code);
console.log(e.msg);
}

// 移动上去
// console.log(assPath, gifPath, videoPath)
// var cmd = "ffmpeg -i " + videoPath + " -r 8 -vf ass=." + assPath + ",scale=" + __SIZE + ":-1 -y " + gifPath
// console.log(cmd);
// try {
// execSync(cmd);

// console.log("生成gif成功")
// } catch (e) {
// console.log(e);
// }
}

function renderGif(templateName, sentences) {
Expand Down
Loading