FFMPEG Time-Range To Time-Diff Using JavaScript

Posted at

Say you have a MP4 file you wish to cut very accurately,
You use a video-player such as "Media-Player Classic" or even VLC, and you stop the video at the correct points and write down the start and end times.

FFMPEG needs for its -ss switch a DELTA not a range...
well... you can use a calculator.. or do it in your head, but keep in mind to include the milliseconds too...

Ain't nobody got time for that..




var   zero = Number(new Date("2016-01-01 " + "00:00:00.000"))
, a = Number(new Date("2016-01-01 " + "00:39:00.680"))
, b = Number(new Date("2016-01-01 " + "00:39:19.120"))
, c = new Date(b - a + zero)
;

console.log(
c.getHours()
+ ":" + c.getMinutes()
+ ":" + c.getSeconds()
+ "." + c.getMilliseconds()
);


Fill in your times in a and b,


run the code and get a time-diff string compatible with FFMPEG :))





the idea is to calculate the diff between two dates, but still maintain a valid date value (adding the "zero"),
and creating custom format using the date class is quite easy too :)))

for the above example,
you'll get something like that: "0:0:18.440"..




And naturally.. it can be made into a nice little bookmarklet..
(function(zero, base, a, b, c, userin){
if(null === userin) return;

userin = userin.replace(/\\s*/g,"").split("-");
a = Number(new Date(base + userin[0]));
b = Number(new Date(base + userin[1]));
c = new Date(b - a + zero);

c = c.getHours() + ":" +
c.getMinutes() + ":" +
c.getSeconds() + "." +
c.getMilliseconds()
;

prompt("☆ RANGE: " + userin.join("") + "\
" +
"∆ DIFF: " + c + "\
", c);

}(
Number(new Date("2016-01-01 " + "00:00:00.000"))
, "2016-01-01 "
, null
, null
, null
, prompt("FFMPEG Compatible Range2Diff", "00:39:00.680-00:39:19.120")
));


or the "flat way..":
javascript:(function(zero,base,a,b,c,userin){if(null===userin)return;userin=userin.replace(/\\s*/g,"").split("-");a=Number(new Date(base+userin[0]));b=Number(new Date(base+userin[1]));c=new Date(b-a+zero);c=c.getHours()+":"+c.getMinutes()+":"+c.getSeconds()+"."+c.getMilliseconds();prompt(" RANGE:  "+userin.join("")+"\
"+" DIFF: "+c+"\
",c)})(Number(new Date("2016-01-01 "+"00:00:00.000")),"2016-01-01 ",null,null,null,prompt("FFMPEG Compatible Range2Diff","00:39:00.680-00:39:19.120"));


And it is ALWAYS a good idea to zero-right-pad the output so you'll get 09 instead of just 9..

(function(zero, base, a, b, c, userin){
if(null === userin) return;

userin = userin.replace(/\\s*/g,"").split("-");
a = Number(new Date(base + userin[0]));
b = Number(new Date(base + userin[1]));
c = new Date(b - a + zero);

c = ("00" + c.getHours() ).substr(-2) + ":" +
("00" + c.getMinutes() ).substr(-2) + ":" +
("00" + c.getSeconds() ).substr(-2) + "." +
("000" + c.getMilliseconds() ).substr(-3)
;

prompt("☆ RANGE: " + userin.join("") + "\
" +
"∆ DIFF: " + c + "\
", c);

}(
Number(new Date("2016-01-01 " + "00:00:00.000"))
, "2016-01-01 "
, null
, null
, null
, prompt("FFMPEG Compatible Range2Diff", "00:39:00.680-00:39:19.120")
));


or in the "flat" way:
javascript:(function(zero,base,a,b,c,userin){if(null===userin)return;userin=userin.replace(/\\s*/g,"").split("-");a=Number(new Date(base+userin[0]));b=Number(new Date(base+userin[1]));c=new Date(b-a+zero);c=("00"+c.getHours()).substr(-2)+":"+("00"+c.getMinutes()).substr(-2)+":"+("00"+c.getSeconds()).substr(-2)+"."+("000"+c.getMilliseconds()).substr(-3);prompt("☆ RANGE:  "+userin.join("")+"\
"+"∆ DIFF: "+c+"\
",c)})(Number(new Date("2016-01-01 "+"00:00:00.000")),"2016-01-01 ",null,null,null,prompt("FFMPEG Compatible Range2Diff","00:39:00.680-00:39:19.120"));