const gql = require('graphql-tag'); const sleep = require('await-sleep'); const { environment, orgSlug, makeS3Locator, request, findMediaItem, checkWorkflow, } = require('./common'); // used to parametrize the output location const s3bucket = 'oz-zl-demo' const s3path = `demo_1523053276229`; // the output folder const outputFileLocator = makeS3Locator(s3bucket, `${s3path}/outputs/imf2flat_${Date.now()}.mov`); // the CPL to flatten const inputCpl = 'CPL_Meridian_OV_English.xml'; // the id of the transcoder profile to use const transcoderProfileId = '4b3bff3e-f2b1-452a-9165-3829170b851c'; async function transcodeFile(fileId, transcoderProfileId) { const mutation = gql` mutation TranscodeFile($options: TranscodeOptionsInput) { transcodeFile(options: $options) { id status } } `; const mutationVariables = { options: { inputFileId: fileId, transcoderProfileId, outputLocator: outputFileLocator } }; const data = await request(mutation, mutationVariables); return data.transcodeFile.id; } async function main() { // find the CPL in the Ownzones database console.log('Searching for file'); const file = await findMediaItem(inputCpl); 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'); } // trigger the transcode and obtain the workflow id const workflowId = await transcodeFile(file.id, transcoderProfileId); console.log(`Started workflow ${workflowId}`); console.log('Waiting for workflow to complete...'); let workflow = { status: 'pending' }; // poll workflow status until it's out of pending or started 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') { // success - get the id of the output file in connect const outputFile = await findMediaItem(outputFileLocator.url); console.log(`Completed: ${outputFileLocator.url} (https://${environment}/${orgSlug}/files/${outputFile.id})`); } else if (workflow.status === 'failed') { // failed - dump the error messages of all the tasks in the workflow workflow.tasks.forEach(task => { if (task.error) console.error(task.error.message); }); } } main().catch(err => { console.error(err.stack); process.exit(1); });