Skip to content

Commit

Permalink
Download master and stream from file system
Browse files Browse the repository at this point in the history
  • Loading branch information
MauriRojas committed Mar 22, 2024
1 parent 01e73f2 commit 2359825
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 8 deletions.
51 changes: 46 additions & 5 deletions VirtualEncoder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ const childProcess = require('child_process');
const { resolve } = require('path');
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const path = require("path");
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const os = require('os');

const streamToMux = (context) => {
const streamToMux = (filePath, context) => {
return new Promise(async (resolve, reject) => {
context.log(`Running ffmpeg from ${ffmpegPath}`);

var inputAbsolutePath = path.resolve("./input.mp4");
//var inputAbsolutePath = path.resolve("./input.mp4");

// ffmpeg -re -i myfile_1.mp4 -r 30 -c:v libx264 -x264-params keyint=60:scenecut=0 -preset fast -b:v 5M -maxrate 6M -bufsize 3M -threads 4 -f flv rtmp://global-live.mux.com:5222/app/{my_stream_key}
const child = childProcess.spawn(
ffmpegPath,
// note, args must be an array when using spawn
['-i', `${inputAbsolutePath}`, '-f', 'flv', 'rtmp://global-live.mux.com:5222/app/5ac28812-8320-0c76-2ba2-313288af035f'],
['-i', `${filePath}`, '-f', 'flv', 'rtmp://global-live.mux.com:5222/app/5ac28812-8320-0c76-2ba2-313288af035f'],
{
windowsVerbatimArguments: true,
}
Expand All @@ -40,20 +44,57 @@ const streamToMux = (context) => {
resolve('Streamed successfully')
} else {
context.log(`FFmpeg encountered an error, check the console output`);
console.log(child);
reject(`FFmpeg encountered an error, check the console output`);
}
});
});
};

const downloadFromMuxToFileSystem = async (url, filePath, context) => {
const writer = fs.createWriteStream(filePath);

context.log("Downloading asset from Mux");

const response = await axios({
method: 'GET',
url: url,
responseType: 'stream'
});

context.log("Writing asset to file system");

response.data.pipe(writer);

context.log("Saved on file system");

return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}

module.exports = async function (context, myQueueItem) {
context.log('JavaScript queue trigger function processing work item', myQueueItem);

// 2. Process the message here
context.log('Starting streaming blob');

var finished = await streamToMux(context);
const url = "https://master.mux.com/qENtrEqJz7SSsoTGkp6O9NMNyR6u2YMz/master.mp4?skid=default&signature=NjVmZjFjZDZfNWVlM2JiYzcyYjRhZmJkY2U5NWEwODU5YzViZjI5MzhiNTUxOGY3NGU1ZjVkNTQxZTJiNTFkN2U2NjlhNGI2OA==";
const tempDir = os.tmpdir();
const outputDir = path.join(tempDir, `downloaded_file`);
fs.mkdirSync(outputDir, { recursive: true });
const filePath = path.join(outputDir, 'downloaded_file.mp4')

await downloadFromMuxToFileSystem(url, filePath, context)
.then(() => {
context.log('File downloaded successfully');
})
.catch((error) => {
context.error('Error downloading file:', error);
throw 'Error downloading file:' + error;
});

var finished = await streamToMux(filePath, context);

context.log("Finished execution", finished);
};
93 changes: 92 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"test": "echo \"No tests yet...\""
},
"dependencies": {
"@ffmpeg-installer/ffmpeg": "^1.1.0"
"@ffmpeg-installer/ffmpeg": "^1.1.0",
"axios": "^1.6.8"
},
"devDependencies": {
"azure-functions-core-tools": "^4.x"
}
}
}

0 comments on commit 2359825

Please sign in to comment.