Skip to content

Commit

Permalink
feat: get the video filename from the youtube-dl log (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
jely2002 committed Jul 14, 2021
1 parent 304ef7e commit f3bae3d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
39 changes: 22 additions & 17 deletions modules/QueryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,32 +351,37 @@ class QueryManager {

async openVideo(args) {
let video = this.getVideo(args.identifier);
let file = video.filename;
let fallback = false;
if(video.type === "playlist") {
shell.openPath(video.downloadedPath);
return;
}
fs.readdir(video.downloadedPath, (err, files) => {
for(const file of files) {
if(file.substr(0, file.lastIndexOf(".")) === video.getFilename()) {
if(args.type === "folder") {
shell.showItemInFolder(path.join(video.downloadedPath, file));
} else if(args.type === "item") {
shell.openPath(path.join(video.downloadedPath, file));
} else {
console.error("Wrong openVideo type specified.")
if(file == null) {
fs.readdir(video.downloadedPath, (err, files) => {
for (const searchFile of files) {
if (file.substr(0, file.lastIndexOf(".")) === video.getFilename()) {
file = searchFile;
break;
}
return;
}
}
//Fallback
if(args.type === "folder") {
if(file == null) {
fallback = true;
file = video.getFilename() + ".mp4";
}
});
}
if(args.type === "folder") {
if(fallback) {
shell.openPath(video.downloadedPath);
} else if(args.type === "item") {
shell.openPath(path.join(video.downloadedPath, video.getFilename()) + ".mp4");
} else {
console.error("Wrong openVideo type specified.")
shell.showItemInFolder(path.join(video.downloadedPath, file));
}
});
} else if(args.type === "item") {
shell.openPath(path.join(video.downloadedPath, file));
} else {
console.error("Wrong openVideo type specified.")
}
}

getUnifiedAvailableSubtitles(videos) {
Expand Down
1 change: 1 addition & 0 deletions modules/download/DownloadQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class DownloadQuery extends Query {
let result = null;
try {
result = await this.environment.downloadLimiter.schedule(() => this.start(this.url, args, (liveData) => {
this.video.setFilename(liveData);
if (!liveData.includes("[download]")) return;
if (!initialReset) {
initialReset = true;
Expand Down
16 changes: 16 additions & 0 deletions modules/types/Video.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Utils = require("../Utils");
const path = require("path");

class Video {
constructor(url, type, environment) {
Expand All @@ -17,9 +18,24 @@ class Video {
this.hasMetadata = false;
this.downloaded = false;
this.error = false;
this.filename = null;
this.identifier = Utils.getRandomID(32);
}

setFilename(liveData) {
console.log(liveData)
if(liveData.includes("[download] Destination: ")) {
this.filename = path.basename(liveData.replace("[download] Destination: ", ""));
} else if(liveData.includes("[ffmpeg] Merging formats into \"")) {
const noPrefix = liveData.replace("[ffmpeg] Merging formats into \"", "");
this.filename = path.basename(noPrefix.trim().slice(0, -1));
} else if(liveData.includes("[ffmpeg] Adding metadata to '")) {
const noPrefix = liveData.replace("[ffmpeg] Adding metadata to '", "");
this.filename = path.basename(noPrefix.trim().slice(0, -1));
}
console.log(this.filename)
}

getFilename() {
if(this.hasMetadata) {
let sanitizeRegex = /(?:[/<>:"|\\?*]|[\s.]$)/g;
Expand Down

0 comments on commit f3bae3d

Please sign in to comment.