Skip to content

Instantly share code, notes, and snippets.

@kevinGodell
Created February 9, 2018 15:51
Show Gist options
  • Save kevinGodell/21e8bb12cc460fe29c8e8071ba5bc17a to your computer and use it in GitHub Desktop.
Save kevinGodell/21e8bb12cc460fe29c8e8071ba5bc17a to your computer and use it in GitHub Desktop.
save init and segments to file system
'use strict';
console.time('=====> test.js');
//const assert = require('assert');
const Mp4Frag = require('../index');
const { spawn } = require('child_process');
//const frameLimit = 200;
const gop = 10;
//const count = Math.ceil(frameLimit/gop);//expected number of segments to be cut from ffmpeg
const scale = 640;
const fps = 10;
let counter = 0;
const params = [
/* log info to console */
'-loglevel', 'quiet',
'-stats',
/* use hardware acceleration if available */
'-hwaccel', 'auto',
/* use an artificial video input */
//'-re',
//'-f', 'lavfi',
//'-i', 'testsrc=size=1280x720:rate=20',
'-i',
'test.mp4',
/* set output flags */
'-an',
'-c:v', 'libx264',
'-movflags', '+frag_keyframe+empty_moov+default_base_moof',
'-f', 'mp4',
'-vf', `fps=${fps},scale=${scale}:-1,format=yuv420p`,
//'-frames', frameLimit,
'-g', gop,
'-profile:v', 'main',
'-level', '3.1',
'-crf', '25',
'-metadata', 'title=test mp4',
'pipe:1'
];
const mp4frag = new Mp4Frag();
mp4frag.on('initialized', (data)=> {
//use file system to save data.initialization as init.mp4
console.log(data.initialization);
//assert(data.mime === 'video/mp4; codecs="avc1.4D401F"', `${data.mime} !== video/mp4; codecs="avc1.4D401F"`);
});
mp4frag.on('segment', (data)=> {
//use file system to save data as a segment, use the counter variable to make an incremented name such as `seg-${counter}.m4s`
console.log(data);
counter++;
});
mp4frag.on('error', (data)=> {
//error is expected when ffmpeg exits without unpiping
console.log('mp4frag error', data);
});
const ffmpeg = spawn('ffmpeg', params, {stdio: ['ignore', 'pipe', 'inherit']});
ffmpeg.on('error', (error) => {
console.log('ffmpeg error', error);
});
ffmpeg.on('exit', (code, signal) => {
//assert(counter === count, `${counter} !== ${count}`);
//assert(code === 0, `FFMPEG exited with code ${code} and signal ${signal}`);
console.timeEnd('=====> test.js');
});
ffmpeg.stdio[1].pipe(mp4frag);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment