Skip to content

Instantly share code, notes, and snippets.

@recursivecodes
Created February 1, 2024 13:58
Show Gist options
  • Save recursivecodes/dad2ba383338b5da78cbefef387118c7 to your computer and use it in GitHub Desktop.
Save recursivecodes/dad2ba383338b5da78cbefef387118c7 to your computer and use it in GitHub Desktop.
A sample Node script that could be used with the Audible Magic binary to identify copyrighted content in an Amazon IVS live stream.
import 'dotenv/config';
import { spawn } from 'node:child_process';
import { mkdirSync, readFileSync } from 'node:fs';
import { inspect } from 'node:util';
import Watcher from 'watcher';
import { IvsClient, GetStreamCommand } from "@aws-sdk/client-ivs";
import { SQSClient, ReceiveMessageCommand, DeleteMessageCommand } from "@aws-sdk/client-sqs";
// create AWS SDK clients
const ivsClient = new IvsClient();
const sqsClient = new SQSClient();
// read SQS Queue
const queueUrl = '[YOUR SQS QUEUE URL]';
const receiveMessageResponse = await sqsClient.send(
new ReceiveMessageCommand(
{
QueueUrl: queueUrl,
MaxNumberOfMessages: 1,
}
)
);
// if there are no messages, get out of here
if (!receiveMessageResponse.Messages) {
console.warn("No SQS messages. Exiting.");
process.exit(1);
}
// there are messages, proceed...
// delete message that we just received
// to prevent processing it in the future
const receiptHandle = receiveMessageResponse.Messages[0].ReceiptHandle;
await sqsClient.send(
new DeleteMessageCommand(
{
QueueUrl: queueUrl,
ReceiptHandle: receiptHandle,
}
)
);
// parse message, get stream ID and channel ARN
const message = JSON.parse(receiveMessageResponse.Messages[0].Body);
const streamId = message.detail.stream_id;
const channelArn = message.resources[0];
// get stream details
const getStreamResponse = await ivsClient.send(
new GetStreamCommand({ channelArn })
);
// get playback URL from stream details
const playbackUrl = getStreamResponse.stream.playbackUrl;
// create a temp path for output
const outputPath = `/tmp/${Date.now()}`;
mkdirSync(outputPath);
// run Audible Magic 🪄
const cmd = '/path/to/identifyStream';
const args = [
'-i',
playbackUrl,
'-c',
'/path/to/Toolkit.config',
'-e',
streamId,
'-O',
outputPath,
'-t',
'/tmp'
];
const audibleMagic = spawn(cmd, args);
audibleMagic.on('error', (err) => {
console.log(err);
});
audibleMagic.stdout.on('data', (data) => {
console.log(data.toString());
});
audibleMagic.stderr.on('data', (err) => {
console.error(err.toString());
process.exit(1);
});
// watch output dir for new files
const watcher = new Watcher(outputPath);
watcher.on('all', async (event, targetPath, targetPathNext) => {
// check for JSON file
if (targetPath.split('.').slice(-1).toString() === 'json') {
// parse and output JSON to console
const result = JSON.parse(
readFileSync(targetPath)
);
console.log(inspect(result, { depth: null, colors: true }));
// do more...
// send chat event to mod queue?
// send file to S3?
// send SQS message?
// send email to admin/mod?
// send message to streamer?
// stop stream?
// retrieve license and send attribution details?
// etc...
}
// delete file if necessary
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment