const gql = require('graphql-tag'); const sleep = require('await-sleep'); const { environment, orgSlug, makeS3Locator, request, findMediaItem, checkWorkflow, } = require('./common'); // the input file of the demuxer const filename = '201_CADWC_CreateADance_NOSLATE_Stereo1.mov'; const s3bucket = 'oz-zl-demo' // make sure never to overwrite existing files (using a timestamp here) const s3path = `demo_1523053276229/output/${Date.now()}`; const s3base = `s3://${s3bucket}/demo_1523053276229/`; const videoOut = makeS3Locator(s3bucket, `${s3path}/image_out.mxf`); const audioOut = makeS3Locator(s3bucket, `${s3path}/audio_out.mxf`); const cplOut = makeS3Locator(s3bucket, `${s3path}/CPL_out.mxf`); /** * Creates a Demux Profile */ async function createDemuxProfile({ name, bitDepth, chromaSubsampling, frameRate, pixelFormat, profileMainlevel, profileSublevel, profileType, timecode, }) { const mutation = gql` mutation CreateDemuxProfile($input: DemuxProfileInput!) { createDemuxProfile(input: $input) { id } } `; const mutationVariables = { input: { files: [], name, outputProperties: { bitDepth, chromaSubsampling, frameRate, pixelFormat, profile: { mainlevel: profileMainlevel, sublevel: profileSublevel, type: profileType, }, timecode, }, }, }; const data = await request(mutation, mutationVariables); return data.createDemuxProfile.id; } /** * Triggers a sample demux workflow on the specified file id, with the specified * demux profile id. */ async function runDemux({ inputFileId, demuxProfileId }) { const mutation = gql` mutation DemultiplexFile($options: DemuxOptionsInput) { demuxFile(options: $options) { id } } `; const options = { inputFileId, demuxProfileId, outputCplLocator: cplOut, outputs: [ { type: 'image', inputStreamIndex: 0, outputLocator: videoOut, }, { type: 'audio', layout: 'stereo', language: 'en', outputLocator: audioOut, channelsMapping: [ { fileId: inputFileId, streamIndex: 1, channels: [ { channelIndex: 0, channelLabel: 'FL' }, ], }, { fileId: inputFileId, streamIndex: 1, channels: [ { channelIndex: 1, channelLabel: 'FR' }, ], }, ], }, ], }; const response = await request(mutation, { options }); return response.demuxFile.id; } async function main() { console.log('Searching for file'); const file = await findMediaItem(filename); console.log(`Found input file: https://${environment}/${orgSlug}/files/${file.id}`); // make sure the input file is completely ingested before moving forward if (file.ingestWorkflow.status !== 'completed') { throw new Error('File is not fully ingested.'); } else { console.log('File is ready'); } console.log('Creating demux profile...'); const demuxProfileId = await createDemuxProfile({ name: `BCS@5 2997 test - ${Date.now()}`, bitDepth: '10', chromaSubsampling: '422', frameRate: '24000/1001', pixelFormat: 'yuv', profileMainlevel: 5, profileSublevel: null, profileType: 'JPEG2000BroadcastContributionSingleTile', timecode: '00:00:00:00', }); console.log(`Using new demux profile ${demuxProfileId}`); console.log('Running demux...'); const workflowId = await runDemux({ inputFileId: file.id, demuxProfileId, }); console.log(`Started workflow ${workflowId}`); console.log('Waiting for workflow to complete...'); let workflow = { status: 'pending' }; while (workflow.status === 'pending' || workflow.status === 'started') { await sleep(3000); workflow = await checkWorkflow(workflowId); console.log(`Status: ${workflow.status} (${workflow.progress}%)`); } if (workflow.status === 'completed') { const [videoFile, audioFile, cplFile] = await Promise.all([ findMediaItem(videoOut.key), findMediaItem(audioOut.key), findMediaItem(cplOut.key), ]); console.log(`Completed: video: ${videoOut.url} (https://${environment}/${orgSlug}/files/${videoFile.id}) audio: ${audioOut.url} (https://${environment}/${orgSlug}/files/${audioFile.id}) cpl: ${cplOut.url} (https://${environment}/${orgSlug}/assets/${cplFile.assetId}/compositions/${cplFile.id}) `); } else if (workflow.status === 'failed') { workflow.tasks.forEach(task => { if (task.error) console.error(task.error.message); }); } }; main().catch(err => { console.error(err.stack); process.exit(1); });