From fe815b455d3f9333cf2f4977ca5a1d2e063a86aa Mon Sep 17 00:00:00 2001 From: SummerTail <1525249038@qq.com> Date: Fri, 30 Jun 2023 20:49:07 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E8=8E=B7=E5=8F=96=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E5=8F=8A=E6=B5=8B=E8=AF=95=E7=94=A8=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E4=B8=8E=E8=AF=B7=E6=B1=82=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/index.js | 48 +++++---- util/utils.js | 57 ++++++++--- view/dist/index.html | 4 +- view/src/page/index.vue | 210 ++++++++++++++++++++++------------------ 4 files changed, 185 insertions(+), 134 deletions(-) diff --git a/router/index.js b/router/index.js index 334b7c7..64c2854 100644 --- a/router/index.js +++ b/router/index.js @@ -1,13 +1,19 @@ const Router = require("koa-router") -const { getQqListData } = require("../util/getList/qq") +const {getQqListData} = require("../util/getList/qq") +const {getVideoTotalLength} = require("../util/utils") const KoaSSEStream = require('koa-sse-stream'); -const { dowload } = require("../util/download"); -const { addDownList, getDownList, setDownState } = require("../util/sql/download"); +const {dowload} = require("../util/download"); +const {addDownList, getDownList, setDownState} = require("../util/sql/download"); const dayjs = require("dayjs") const router = new Router() // const dowloadlist = [] -router.get("/getQqList",async (ctx)=>{ +router.get("/test", async (ctx) => { + let url = ctx.query.url + ctx.body = getVideoTotalLength(url) +}) + +router.get("/getQqList", async (ctx) => { let url = ctx.query.url let videoid = url.split("/") for(let i in videoid){ @@ -20,7 +26,7 @@ router.get("/getQqList",async (ctx)=>{ ctx.body = list }) -router.get("/startDown",async (ctx)=>{ +router.get("/startDown", async (ctx) => { let title = ctx.query.title let url = ctx.query.url let save = ctx.query.save @@ -28,10 +34,10 @@ router.get("/startDown",async (ctx)=>{ // title,url,save // }) // startDown() - addDownList(title,url,save,new Date().getTime(),0) - ctx.body={ - code:0, - msg:"添加成功" + addDownList(title, url, save, new Date().getTime(), 0) + ctx.body = { + code: 0, + msg: "添加成功" } }) // 连接池 @@ -44,33 +50,35 @@ const SSE_CONF = { router.get('/msg', KoaSSEStream(SSE_CONF), ctx => { // 每次连接会进行一个 push clientList.push(ctx.sse); - if(clientList.length > 5){ + if (clientList.length > 5) { // 超过5个删除最开始的一个 // todo 浏览器关闭后自动检测删除 clientList.shift() } }) let start = false -function startDown(data){ + +function startDown(data) { start = true - dowload(data,async (data2,isok)=>{ + dowload(data, async (data2, isok) => { let name = data2.title - clientList.forEach((i)=>{ - i.send(isok ? `${name}下载完成` : data2 ) + clientList.forEach((i) => { + i.send(isok ? `${name}下载完成` : data2) }) - if(isok){ + if (isok) { // dowloadlist.shift() - await setDownState(data.id,1) + await setDownState(data.id, 1) start = false } }) } -setInterval(async ()=>{ + +setInterval(async () => { let dowloadlist = await getDownList() - if(dowloadlist.length != 0 && !start){ + if (dowloadlist.length != 0 && !start) { startDown(dowloadlist[0]) - }else if(dowloadlist.length == 0){ + } else if (dowloadlist.length == 0) { start = false } -},1000) +}, 1000) module.exports = router \ No newline at end of file diff --git a/util/utils.js b/util/utils.js index 251f433..ae6a7cd 100644 --- a/util/utils.js +++ b/util/utils.js @@ -1,27 +1,52 @@ -const { spawn } = require('child_process'); +const {spawn} = require('child_process'); -function seep(time){ - return new Promise((res)=>{ - setTimeout(()=>{ +function seep(time) { + return new Promise((res) => { + setTimeout(() => { res() - },time) + }, time) }) } -function dow(info,callback){ - const lux = spawn("./bin/yt-dlp.exe", ['--cookies-from-browser', 'chrome', '-P', info.save, '-o', info.title + '.mp4', info.url]) - lux.stdout.on('data', (data) => { - console.log(String(data)) - callback(String(data),false) - }); - lux.on('close', (code) => { - console.log("结束") - callback(code,true) - }); + +/** + * 获取视频长度 + * @param {String} filePath 文件绝对路径 + * @returns {number} 时长(秒) + */ +function getVideoTotalLength(filePath) { + + const lux = spawn("./bin/ffmpeg.exe", ['-i', filePath, '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv="p=0"']) + console.log("join") + lux.stdout.on('data', (data) => { + console.log("开始") + console.log(data) + }); + lux.on('close', (code) => { + console.log("结束") + console.log(code.toString()) + }); + + return 0; } + +function dow(info, callback) { + const lux = spawn("./bin/yt-dlp.exe", ['--cookies-from-browser', 'chrome', '-P', info.save, '-o', info.title + '.mp4', info.url]) + lux.stdout.on('data', (data) => { + console.log(String(data)) + callback(String(data), false) + }); + lux.on('close', (code) => { + console.log("结束") + callback(code, true) + }); +} + + module.exports = { seep, - dow + dow, + getVideoTotalLength } \ No newline at end of file diff --git a/view/dist/index.html b/view/dist/index.html index 019c932..47d3b3f 100644 --- a/view/dist/index.html +++ b/view/dist/index.html @@ -5,8 +5,8 @@