Skip to content

Instantly share code, notes, and snippets.

@egorvinogradov
Last active May 18, 2017 02:04
Show Gist options
  • Select an option

  • Save egorvinogradov/c960391279edc3cc401d608ff0840870 to your computer and use it in GitHub Desktop.

Select an option

Save egorvinogradov/c960391279edc3cc401d608ff0840870 to your computer and use it in GitHub Desktop.
Generate bash commands to cut and concatenate a video based on time periods (using ffmpeg)
/*
1. Open http://majestic-geese.surge.sh
2. Insert time periods e.g.:
5:49 - 7:24
10:11 - 11:22
11:58- 12:45
13:04- 13:34
13:48 - 13:58
17:08- 18:08
3. Open debug bar
4. Insert code below
5. run: copy(makeCommands('v.avi', 'mp4', 480))
6. Insert and run clipboard into Terminal
*/
function getPeriods(){
var data = document.getElementsByTagName('textarea')[0].value.trim();
return data.split(/\n/).map(function(str){
str = str.trim();
var period = str.split(/\s?\-\s?/);
var duration = makePeriodString(period[0], period[1]);
var startStr = period[0];
if (startStr.split(':')[0].length === 1) {
startStr = '0' + startStr;
}
return [startStr, duration];
});
}
function makePeriodString(a, b){
a = a.split(':');
b = b.split(':');
var minDiff = b[0] - a[0];
var secDiff = b[1] - a[1];
var durationSec = minDiff * 60 + secDiff;
var durationOnlySec = (durationSec % 60).toString();
if (durationOnlySec < 10) {
durationOnlySec = '0' + durationOnlySec;
}
return Math.floor(durationSec / 60) + ':' + durationOnlySec;
}
function makeConvertCommands(videoPath, periods, ext){
return periods.map(function(item, i){
return `ffmpeg -i ${videoPath} -ss 00:${item[0]} -c copy -t 00:0${item[1]} part${i}.${ext}`;
});
}
function makeFileListCommand(periods, ext){
var content = periods.map(function(item, i){
return `file part${i}.${ext}`
}).join(' \\n');
return `echo -e "${content}" >> vlist_${ext}.txt`;
}
function makeCommands(videoPath, ext, res){
var MOVIE_NAME = 'mentopship_movie';
var periods = getPeriods();
var convertC = makeConvertCommands(videoPath, periods, ext).join(' && ');
var fileListC = makeFileListCommand(periods, ext);
var glueC = `ffmpeg -f concat -i vlist_${ext}.txt -c copy ${MOVIE_NAME}.${ext}`;
var resizeC = '';
if (res) {
resizeC = `ffmpeg -i ${MOVIE_NAME}.${ext} -s 480x320 -c:a copy ${MOVIE_NAME}_480.${ext}`;
}
var cleanUpCommand = `rm part*.${ext} && rm vlist_*.txt`;
return [
convertC,
fileListC,
glueC,
resizeC,
cleanUpCommand,
].join(' && ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment