diff --git a/.gitignore b/.gitignore index 4919b1c..d91c45f 100644 --- a/.gitignore +++ b/.gitignore @@ -130,4 +130,4 @@ dist .pnp.* # ytdl -uploads \ No newline at end of file +cached \ No newline at end of file diff --git a/index.js b/index.js index 467aa52..e68d690 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,9 @@ +const { randomInt } = require('crypto'); const ytdl = require('ytdl-core'), fs = require('fs'), path = require('path'), express = require('express'), - ffmpeg = require('fluent-ffmpeg'), + ffmpeg = require('ffmpeg'), bodyParser = require('body-parser') const PORT = process.env.PORT || 8080 @@ -18,7 +19,7 @@ process.on('uncaughtException', (err, origin) => { app.use(bodyParser.urlencoded({ extended: false })) -app.get("/download", (req, res) => { +app.get("/download", async (req, res) => { const filename = req.query.filename const url = req.query.url const format = req.query.format @@ -29,17 +30,36 @@ app.get("/download", (req, res) => { return } - if (format != "mp4" && format != "webm") { - res.header('Content-Disposition', `attachment; filename="${filename}.${format};"`); + res.header('Content-Disposition', `attachment; filename="${filename}.${format}"`); - var proc = new ffmpeg({ source: ytdl(url, { 'quality': quality }) }) - .format(format) - .pipe(res) + var ytpath = path.join(__dirname, 'cached/' + ytdl.getVideoID(url) + String(Math.floor(Math.random() * 100000)) + '.mp4') + + var vidinfo = await ytdl.getBasicInfo(url) + + var ytvid = ytdl(url, { 'quality': quality }) + .pipe(fs.createWriteStream(ytpath)) + .on("close", ()=> { + console.log("Downloaded!") + var proc = new ffmpeg(ytpath) + proc.then(function(video) { + video + .setVideoDuration(vidinfo.videoDetails.lengthSeconds) + .setVideoFormat(format) + .save('cached/' + ytdl.getVideoID(url) + String(Math.floor(Math.random() * 100000)) + "Converted." + format,function (err, file) { + console.log("Converted!") + fs.createReadStream(file).pipe(res) + .on("close", () => { + console.log("Freed!") + fs.unlinkSync(file) + if (fs.existsSync(ytpath)) { + fs.unlinkSync(ytpath) + } + }) + }) + }) + }) + - } else { - res.header('Content-Disposition', `attachment; filename="${filename}.${format}"`); - ytdl(url, { 'format': format, 'quality': quality }).pipe(res); - } }); diff --git a/package-lock.json b/package-lock.json index fd61f32..ba4345e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,17 @@ { "name": "youtubeconverter", - "version": "0.2.0", + "version": "0.4.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "youtubeconverter", - "version": "0.2.0", + "version": "0.4.0", "license": "MIT", "dependencies": { "body-parser": "^1.20.2", "express": "^4.18.2", - "fluent-ffmpeg": "^2.1.2", + "ffmpeg": "^0.0.4", "ytdl-core": "^4.11.5" } }, @@ -32,11 +32,6 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, - "node_modules/async": { - "version": "3.2.4", - "resolved": "https://registry.npmjs.org/async/-/async-3.2.4.tgz", - "integrity": "sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ==" - }, "node_modules/body-parser": { "version": "1.20.2", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", @@ -241,6 +236,14 @@ "node": ">= 0.8" } }, + "node_modules/ffmpeg": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/ffmpeg/-/ffmpeg-0.0.4.tgz", + "integrity": "sha512-3TgWUJJlZGQn+crJFyhsO/oNeRRnGTy6GhgS98oUCIfZrOW5haPPV7DUfOm3xJcHr5q3TJpjk2GudPutrNisRA==", + "dependencies": { + "when": ">= 0.0.1" + } + }, "node_modules/finalhandler": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", @@ -258,18 +261,6 @@ "node": ">= 0.8" } }, - "node_modules/fluent-ffmpeg": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/fluent-ffmpeg/-/fluent-ffmpeg-2.1.2.tgz", - "integrity": "sha512-IZTB4kq5GK0DPp7sGQ0q/BWurGHffRtQQwVkiqDgeO6wYJLLV5ZhgNOQ65loZxxuPMKZKZcICCUnaGtlxBiR0Q==", - "dependencies": { - "async": ">=0.2.9", - "which": "^1.1.1" - }, - "engines": { - "node": ">=0.8.0" - } - }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -365,11 +356,6 @@ "node": ">= 0.10" } }, - "node_modules/isexe": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==" - }, "node_modules/m3u8stream": { "version": "0.8.6", "resolved": "https://registry.npmjs.org/m3u8stream/-/m3u8stream-0.8.6.tgz", @@ -675,16 +661,10 @@ "node": ">= 0.8" } }, - "node_modules/which": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", - "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", - "dependencies": { - "isexe": "^2.0.0" - }, - "bin": { - "which": "bin/which" - } + "node_modules/when": { + "version": "3.7.8", + "resolved": "https://registry.npmjs.org/when/-/when-3.7.8.tgz", + "integrity": "sha512-5cZ7mecD3eYcMiCH4wtRPA5iFJZ50BJYDfckI5RRpQiktMiYTcn0ccLTZOvcbBume+1304fQztxeNzNS9Gvrnw==" }, "node_modules/ytdl-core": { "version": "4.11.5", diff --git a/package.json b/package.json index 25d3146..b8c23c6 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "dependencies": { "body-parser": "^1.20.2", "express": "^4.18.2", - "fluent-ffmpeg": "^2.1.2", + "ffmpeg": "^0.0.4", "ytdl-core": "^4.11.5" } } diff --git a/static/index.html b/static/index.html index 13f27e1..ca2139c 100644 --- a/static/index.html +++ b/static/index.html @@ -29,27 +29,21 @@
Format:
-* using mp4s or webms is highly reccomended, as those are the default formats for youtube.
Any other format will require converting the video, slowing download speed.
Please consider hosting the project yourself! (Check github for more details)
-Check out the Github page to - learn how to host it yourself, how it works, report bugs, and more.
+Please consider donating or host the website yourself! Anything helps! (Check github for more info)