From 2359825e634a6ed429272cdf744939588844a9d1 Mon Sep 17 00:00:00 2001 From: Mauricio Rojas Date: Fri, 22 Mar 2024 16:16:53 -0300 Subject: [PATCH] Download master and stream from file system --- VirtualEncoder/index.js | 51 +++++++++++++++++++--- package-lock.json | 93 ++++++++++++++++++++++++++++++++++++++++- package.json | 5 ++- 3 files changed, 141 insertions(+), 8 deletions(-) diff --git a/VirtualEncoder/index.js b/VirtualEncoder/index.js index 6fcbd57..baff227 100644 --- a/VirtualEncoder/index.js +++ b/VirtualEncoder/index.js @@ -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, } @@ -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); }; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index f58b7a5..7f6cea4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,8 @@ "name": "ffmpeg-to-mux", "version": "1.0.0", "dependencies": { - "@ffmpeg-installer/ffmpeg": "^1.1.0" + "@ffmpeg-installer/ffmpeg": "^1.1.0", + "axios": "^1.6.8" }, "devDependencies": { "azure-functions-core-tools": "^4.x" @@ -131,6 +132,21 @@ "win32" ] }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "node_modules/axios": { + "version": "1.6.8", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.8.tgz", + "integrity": "sha512-v/ZHtJDU39mDpyBoFVkETcd/uNdxrWRrg3bKpOKzXFA6Bvqopts6ALSMU3y6ijYxbw2B+wPrIv46egTzJXCLGQ==", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/azure-functions-core-tools": { "version": "4.0.5571", "resolved": "https://registry.npmjs.org/azure-functions-core-tools/-/azure-functions-core-tools-4.0.5571.tgz", @@ -499,6 +515,81 @@ "buffer-crc32": "~0.2.3", "fd-slicer": "~1.1.0" } + }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/follow-redirects": { + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" } } } diff --git a/package.json b/package.json index 0068c53..984a312 100644 --- a/package.json +++ b/package.json @@ -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" } -} \ No newline at end of file +}