Skip to content

Commit fe1d7d0

Browse files
authored
fix: import youtube urls on desktop v0.2.0 (#69)
* Added configurable dev mode url for remote server usage * Restructured all download process * Deleted unnecessery console.log * Made total progress bar better, deleted current progress text, component cleaned with splitting up to new files, added collapsable list for downloaded videos * Made pr prettier 🎉 * Added check for not showing 'information is loading' without isDownloading
1 parent 44fbae3 commit fe1d7d0

File tree

6 files changed

+337
-284
lines changed

6 files changed

+337
-284
lines changed

desktop/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function createWindow() {
2222

2323
// and load the index.html of the app.
2424
if (process.env.USE_DEV_SERVER) {
25-
mainWindow.loadURL("http://localhost:6001")
25+
// USAGE: DEV_SERVER_URL=https://e2a6dfb7.ngrok.io npm run start:desktop:dev
26+
mainWindow.loadURL(process.env.DEV_SERVER_URL || "http://localhost:6001")
2627
} else {
2728
mainWindow.loadURL(
2829
formatUrl({
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
export default ({
2+
youtubeUrl,
3+
title: videoTitle,
4+
remote,
5+
videoQuality,
6+
downloadPath,
7+
onProgress,
8+
onComplete,
9+
overallProgress,
10+
}) => {
11+
let starttime = Date.now()
12+
13+
const ytdl = remote.require("ytdl-core")
14+
const path = remote.require("path")
15+
16+
const videoName = videoQuality.includes("audio")
17+
? `${videoTitle + ".mp3"}`
18+
: `${videoTitle + ".mp4"}`
19+
20+
const fullVideoPath = path.join(downloadPath, videoName)
21+
const writableVideoFile = remote
22+
.require("fs")
23+
.createWriteStream(fullVideoPath)
24+
25+
const videoFormat = videoName.endsWith(".mp3") ? "audio/mp3" : "video/mp4"
26+
27+
const youtubeVideoWithOptions = ytdl(youtubeUrl, {
28+
format: videoFormat,
29+
quality: videoQuality !== null ? videoQuality : "highest",
30+
})
31+
32+
youtubeVideoWithOptions.pipe(writableVideoFile)
33+
34+
youtubeVideoWithOptions.once("response", () => {
35+
starttime = Date.now()
36+
})
37+
38+
youtubeVideoWithOptions.on("progress", (chunkLength, downloaded, total) => {
39+
const percent = downloaded / total
40+
// const downloadedMinutes = (Date.now() - starttime) / 1000 / 60
41+
42+
// const downloadedPercentage = (percent * 100).toFixed(2)
43+
// const downloadedSize = (downloaded / 1024 / 1024).toFixed(2)
44+
// const downloadedMBs = (
45+
// downloaded /
46+
// 1024 /
47+
// 1024
48+
// ).toFixed(2)
49+
// const totalSize = (total / 1024 / 1024).toFixed(2)
50+
// const informationText = `${downloadedPercentage}% downloaded (${downloadedMBs}MB of ${totalSize}MB)\nestimated time left: ${(downloadedMinutes / percent -downloadedMinutes).toFixed(2)}minutes`
51+
52+
onProgress({
53+
progress: percent * 100,
54+
})
55+
56+
overallProgress(percent * 100)
57+
})
58+
59+
youtubeVideoWithOptions.on("end", () => onComplete(fullVideoPath))
60+
61+
return {
62+
cancel: () => {
63+
youtubeVideoWithOptions.destroy()
64+
},
65+
}
66+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const getYoutubeVideoInformation = async (
2+
remote,
3+
splittedURLsArray,
4+
progressCallback = () => null
5+
) => {
6+
const ytdl = remote.require("ytdl-core")
7+
const checkedVideos = []
8+
if (splittedURLsArray.length > 0) {
9+
for (let i = 0; i < splittedURLsArray.length; i++) {
10+
const url = splittedURLsArray[i]
11+
const video = await new Promise((resolve, reject) => {
12+
ytdl.getBasicInfo(url, (err, info) => {
13+
if (info && !err) {
14+
resolve({
15+
url,
16+
title: info.title,
17+
})
18+
} else {
19+
const errorText = `Error with video at "${url}"\n\n${err.toString()}`
20+
reject(new Error(errorText))
21+
}
22+
})
23+
})
24+
checkedVideos.push(video)
25+
progressCallback({
26+
progress: (i / (splittedURLsArray.length - 1)) * 100,
27+
text: "Inspecting Video Information...",
28+
})
29+
}
30+
}
31+
return checkedVideos
32+
}
33+
34+
export default getYoutubeVideoInformation

0 commit comments

Comments
 (0)