Skip to content

Instantly share code, notes, and snippets.

@carlfriess
Created April 10, 2016 17:41
Show Gist options
  • Save carlfriess/78469f51f7a96657b498fea4e79784d8 to your computer and use it in GitHub Desktop.
Save carlfriess/78469f51f7a96657b498fea4e79784d8 to your computer and use it in GitHub Desktop.
Node.js script to add a subtitles track to a mp4 from an srt file.
#!/usr/local/bin/node
const readline = require('readline');
var exec = require('child_process').exec;
var path = require('path');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Video file: ', (videoPath) => {
var videoFile = path.parse(videoPath.trim().replace(/\\(?!\\)/g, ""));
rl.question('Subtitles file: ', (subtitlesPath) => {
var subtitlesFile = path.parse(subtitlesPath.trim().replace(/\\(?!\\)/g, ""));
if (subtitlesFile.ext != ".srt") {
console.log("⚠️ Subtitles must be in .srt format!");
rl.close();
return;
}
rl.question('Output file: [same as input] ', (outputPath) => {
var outputFile = path.parse((outputPath ? outputPath : videoPath).trim().replace(/\\(?!\\)/g, ""));
if (!outputPath) {
outputFile.name += " (subtitled)";
outputFile.base = outputFile.name + outputFile.ext;
}
console.log("🚀 Running...");
exec("ffmpeg -i \"" + path.format(videoFile) + "\" -f srt -i \"" + path.format(subtitlesFile) + "\" -c:v copy -c:a copy -c:s mov_text \"" + path.format(outputFile) + "\"", function(error, stdout, stderr) {
if (error) {
console.log(error);
console.log(stdout);
console.log(stderr);
console.log("❌ Something went wrong!");
}
else {
console.log("Done! 🍿 😎")
}
}).stdin.write("y\n");
rl.close();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment