Skip to content

Instantly share code, notes, and snippets.

@frenchie4111
Created October 5, 2018 17:08
Show Gist options
  • Save frenchie4111/419c3c633ff2c4b8e8a5361cb3a2635a to your computer and use it in GitHub Desktop.
Save frenchie4111/419c3c633ff2c4b8e8a5361cb3a2635a to your computer and use it in GitHub Desktop.
Download Video from KinesisVideoArchivedMedia
const fs = require( 'fs' );
const AWS = require( 'aws-sdk' ),
moment = require( 'moment' ),
video_stitch = require( 'video-stitch' ),
ffmpeg = require( 'fluent-ffmpeg' );
const start_date = moment().subtract( 1, 'minutes' ).toDate();
const end_date = moment().toDate();
const STREAM_NAME = 'myStream';
const STREAM_ARN = 'arn:aws:kinesisvideo:us-east-1:340301842845:stream/myStream/1537299403657';
const kinesis_video = new AWS.KinesisVideo( {
region: 'us-east-1'
} );
console.log( 'starting at: ', moment() );
function getMediaForFragment( kv_archived_media, get_media_params ) {
return new Promise( ( resolve, reject ) => {
kv_archived_media.getMediaForFragmentList( get_media_params, function( err, data ) {
if( err ) {
return reject( err );
}
return resolve( data );
} );
} );
}
const getDataEndpoint = async ( api_name ) => {
const params = {
APIName: api_name,
StreamARN: STREAM_ARN
};
const { DataEndpoint: data_endpoint } = await kinesis_video.getDataEndpoint( params ).promise();
return data_endpoint;
};
const convert = ( in_file, out_file ) => {
return new Promise( ( resolve ) => {
ffmpeg()
.input( in_file )
.size( '320x240' )
.on( 'end', () => {
resolve();
} )
.save( out_file );
} );
};
( async () => {
const kinesis_video_archived_media = new AWS.KinesisVideoArchivedMedia( {
region: 'us-east-1',
endpoint: await getDataEndpoint( 'LIST_FRAGMENTS' )
} );
const list_fragments_params = {
StreamName: STREAM_NAME,
FragmentSelector: {
FragmentSelectorType: 'SERVER_TIMESTAMP',
TimestampRange: {
StartTimestamp: start_date,
EndTimestamp: end_date
}
}
};
const { Fragments } = await kinesis_video_archived_media.listFragments( list_fragments_params ).promise();
const fragments = Fragments.map( ( fragment ) => fragment.FragmentNumber ).sort();
const kv_get_media = new AWS.KinesisVideoArchivedMedia( {
region: 'us-east-1',
endpoint: await getDataEndpoint( 'GET_MEDIA_FOR_FRAGMENT_LIST' )
} );
const to_mp4_promises = [];
let concat_filenames = [];
for( let i = 0; i < fragments.length; i++ ) {
const fragment = fragments[ i ];
const params = {
Fragments: [ fragment ],
StreamName: STREAM_NAME
};
const { Payload } = await getMediaForFragment( kv_get_media, params );
const filename_base = `/tmp/kinesis/video-${ i }`;
fs
.writeFile( filename_base + '.webm', Payload, () => {
to_mp4_promises.push( convert( filename_base + '.webm', filename_base + '.mp4' ) );
} );
concat_filenames.push( filename_base + '.mp4' );
console.log( 'made a new file: ', i );
}
await Promise.all( to_mp4_promises );
await new Promise( ( resolve ) => {
setTimeout( resolve, 2000 );
} );
console.log( 'concat files' );
concat_filenames = concat_filenames.map( file => { return { fileName: file }; } );
console.log( concat_filenames );
// video stitch calls:
// ffmpeg -f concat -safe 0 -protocol_whitelist file,http,https,tcp,tls,crypto
// -i ${args.fileList} -c copy ${outputFileName} ${overwrite}
await video_stitch
.concat( {
silent: false,
overwrite: true
} )
.clips( concat_filenames )
.output( 'test.mp4' )
.concat();
console.log( 'after' );
} )()
.then( () => {
console.log( 'Done' );
} )
.catch( ( err ) => {
console.log( err );
console.log( 'Catch' );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment