diff options
Diffstat (limited to 'downtube.cpp')
-rw-r--r-- | downtube.cpp | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/downtube.cpp b/downtube.cpp index 93f88ba..b3ed581 100644 --- a/downtube.cpp +++ b/downtube.cpp @@ -120,12 +120,17 @@ int main(void) //FCGX_FPrintF(request.out, "path: %s\r\n", tempDir.getPath().string().c_str()); if (command == "getfilename") { - std::string cmd{"youtube-dl -o '%(title)s."s + format + "' --get-filename --no-call-home --restrict-filenames "s + url + " > filename.txt"}; + //std::string cmd{"youtube-dl -o '%(title)s."s + format + "' --get-filename --no-call-home --restrict-filenames "s + url + " > filename.txt"}; + // Recoding to MP4 is too slow currently. So keep original format for now + std::string cmd{"youtube-dl -o '%(title)s.%(ext)s' --get-filename --no-call-home --restrict-filenames "s + url + " > filename.txt"}; if (system(cmd.c_str())) throw std::runtime_error("Can't guess filename"); std::string filename {File::getFile("filename.txt")}; boost::algorithm::trim(filename); + + if (format == "mp3") + filename = fs::path{filename}.stem().string() + ".mp3"; FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); FCGX_FPrintF(request.out, "%s", filename.c_str()); @@ -135,15 +140,35 @@ int main(void) system(cmd.c_str()); // Ignore error - "ERROR: Stream #1:0 -> #0:1 (copy)" - seems to be ok std::string filedata {File::getFile("audio.mp3")}; // may throw + + if (filedata.size() > 30000000) + throw std::runtime_error("File too big"); FCGX_PutS("Content-Type: application/octet-stream\r\n", request.out); FCGX_FPrintF(request.out, "Content-Length: %d\r\n\r\n", filedata.size()); FCGX_PutStr(filedata.c_str(), filedata.size(), request.out); - } else if (format == "mp4") { - std::string cmd{"youtube-dl --no-warnings --no-call-home --no-progress --recode-video mp4 -o video.mp4 --restrict-filenames "s + url}; + } else if (false && format == "mp4") { // disabled for now + //std::string cmd{"youtube-dl --no-warnings --no-call-home --no-progress --recode-video mp4 -o video.mp4 --restrict-filenames "s + url}; + // Recoding to MP4 is too slow currently. So keep original format for now + std::string cmd{"youtube-dl --no-warnings --no-call-home --no-progress -o video.mp4 --restrict-filenames "s + url}; system(cmd.c_str()); // Ignore error + + // youtube-dl gets it wrong and creates, e.g. video.mkv. + // So find it and load it + fs::directory_iterator di{fs::current_path()}; + fs::path filename; + for (const auto& i: di) { + if (i.path().stem().string() == "video"s) { + filename = i.path().filename().string(); + break; + } + } - std::string filedata {File::getFile("video.mp4")}; // may throw + if (filename.empty()) { + throw std::runtime_error("No video file found."); + } + + std::string filedata {File::getFile(filename)}; // may throw FCGX_PutS("Content-Type: application/octet-stream\r\n", request.out); FCGX_FPrintF(request.out, "Content-Length: %d\r\n\r\n", filedata.size()); |