Last active
April 17, 2019 16:33
-
-
Save asins/c2a54246ced68c0d24b021c09188a362 to your computer and use it in GitHub Desktop.
通过ffmpeg对m4a文件设置meta信息
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const { spawn } = require( 'child_process' ); | |
const sourcePath = path.resolve('./video/'); | |
const distPath = path.resolve('./new/'); | |
const ALBUM = '我修的可能是假仙'; | |
const AUTHOR = '牛大宝'; | |
let videoFileList; | |
function setVideoTags(filePath, cb) { | |
const pathParse = path.parse(filePath); | |
const ls = spawn( 'ffmpeg', [ | |
'-i', filePath, | |
'-b:a', '65k', | |
'-codec', 'copy', | |
'-metadata', `title=${pathParse.name}`, | |
'-metadata', `album=${ALBUM}`, | |
'-metadata', `author=${AUTHOR}`, | |
path.resolve(distPath, pathParse.base) | |
]); | |
// ls.stdout.on( 'data', data => { | |
// console.log( `stdout: ${data}` ); | |
// }); | |
// | |
// ls.stderr.on( 'data', data => { | |
// console.log( `stderr: ${data}` ); | |
// }); | |
ls.on( 'close', code => { | |
console.log( `${filePath}, Code: ${code}` ); | |
cb && cb(); | |
}); | |
} | |
function clearDistDir(directory) { | |
fs.readdir(directory, (err, files) => { | |
if (err) throw err; | |
for (const file of files) { | |
fs.unlink(path.join(directory, file), err => { | |
if (err) throw err; | |
}); | |
} | |
}); | |
} | |
function handleVideo(index) { | |
const videoPath = videoFileList[index]; | |
if(!videoPath) return; | |
// if(index > 2) return; | |
setVideoTags(path.resolve(sourcePath, videoPath), function() { | |
handleVideo(++index); | |
}); | |
} | |
// 遍历文件夹 | |
fs.readdir(sourcePath, function(err, items) { | |
videoFileList = items; | |
clearDistDir(distPath); | |
handleVideo(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment