This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
videoUtils.js
167 lines (123 loc) · 3.55 KB
/
videoUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var fs = require('fs');
var child_process = require('child_process');
//var FFMPEG_DIR = '/opt/ffmpeg/bin/';
var FFMPEG_DIR = '/usr/local/bin/';
function maxOfArr(arr) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
return arr.reduce(function(prev, curr/*, idx, arr*/) {
return Math.max(prev, curr);
});
}
var FPS = 25;
var frameTime = 1 / FPS; // TODO not sure this is the best approach, adding one fictitious frame
function findVideoDuration(fp, cb) {
var out = [];
var args = [
FFMPEG_DIR + 'ffprobe',
'-v', 'quiet', // less verbose
'-print_format', 'json', // output json
'-show_format', // return format info
// '-show_streams',
fp
];
//console.log(args.join(' '));
var cmd = args.shift();
var proc = child_process.spawn(cmd, args);
proc.stdout.on('data', function(data) {
out.push( data.toString() );
});
/*proc.stderr.on('data', function(data) {
// out.push( data.toString() );
});*/
proc.on('close', function() {
//console.log('close');
out = out.join('');
out = JSON.parse(out);
var d = parseFloat( out.format.duration );
cb(null, d);
});
}
function computeStartTimes(fileDataArr) {
var t = 0;
fileDataArr.forEach(function(fd) {
fd.t0 = t;
t += fd.duration + frameTime;
});
}
// o {fp, t0}
function webm2Mpegts(o, cb) {
var out = [];
var err = [];
var t0 = o.t0;
var fp = o.filePath;
var fp2 = fp.replace('.webm', '.ts');
o.filePath2 = fp2;
o.fileName2 = o.fileName.replace('.webm', '.ts');
var args = [
FFMPEG_DIR + 'ffmpeg',
'-v', 'quiet', // less verbose
'-loglevel', 'error',
'-i', fp, // inpuf file(s)
'-vcodec', 'libx264', // video codec
'-acodec', 'libfaac', // audio codec
// '-tune', 'zerolatency', // optimize for streaming?
'-r', FPS, // frame rate (fps)
'-profile:v', 'baseline', // ?
'-b:v', '800k', // video bitrate
'-b:a', '48k', // audio bitrate
'-f', 'mpegts', // desired format
// '-fflags', '+igndts', // WTF?! https://trac.ffmpeg.org/ticket/3558
'-strict', 'experimental', // https://trac.ffmpeg.org/ticket/1839
'-mpegts_copyts', '1',
// '-filter:v', "'setpts=10+PTS'", // https://www.ffmpeg.org/ffmpeg-filters.html#toc-setpts_002c-asetpts
'-filter:v', 'setpts=PTS+' + t0 + '/TB',
//-async 1
'-y', // overwrite output files
fp2 // output file
];
//console.log(args.join(' '));
var cmd = args.shift();
var proc = child_process.spawn(cmd, args);
proc.stdout.on('data', function(data) {
out.push( data.toString() );
});
proc.stderr.on('data', function(data) {
err.push( data.toString() );
});
proc.on('close', function() {
out = out.join('').trim();
err = err.join('').trim();
if (out) { console.log('\nOUT\n' + out + '\n'); }
if (err) { console.log('\nERR\n' + err + '\n'); }
cb(null, fp2);
});
}
function generateM3u8Playlist(fileDataArr, playlistFp, isLive, cb) {
var durations = fileDataArr.map(function(fd) {
return fd.duration;
});
var maxT = maxOfArr(durations);
var meta = [
'#EXTM3U',
'#EXT-X-VERSION:3',
'#EXT-X-MEDIA-SEQUENCE:0',
'#EXT-X-ALLOW-CACHE:YES',
'#EXT-X-TARGETDURATION:' + Math.ceil(maxT),
];
fileDataArr.forEach(function(fd) {
meta.push('#EXTINF:' + fd.duration.toFixed(2) + ',');
meta.push( fd.fileName2 );
});
if (!isLive) {
meta.push('#EXT-X-ENDLIST');
}
meta.push('');
meta = meta.join('\n');
fs.writeFile(playlistFp, meta, cb);
}
module.exports = {
findVideoDuration: findVideoDuration,
computeStartTimes: computeStartTimes,
webm2Mpegts: webm2Mpegts,
generateM3u8Playlist: generateM3u8Playlist
};