Beispiel #1
0
        /// <summary>
        /// Downloads the information from a URL
        /// </summary>
        /// <param name="url">The URL to get the information of</param>
        /// <returns>The information downloaded. Null if the download failed</returns>
        public async Task <DownloadInfo> GetDownloadInfoFromUrl(string url)
        {
            ConLog.Log("YouTubeDL", "Downloading info from " + url, LogType.Info);

            string output = await RunProcessAndGetOutput("--no-playlist --quiet --dump-json -f \"bestvideo+bestaudio/best/bestaudio/bestvideo\" --merge-output-format \"mp4\" \"" + url + "\"");

            //Check for error
            if (output == null)
            {
                ConLog.Log("YouTubeDL", "Failed to download info from " + url, LogType.Warning);
                return(null);
            }

            //Get json
            DownloadInfo result = JsonConvert.DeserializeObject <DownloadInfo>(output);

            ConLog.Log("YouTubeDL", "Downloaded info from " + url, LogType.Ok);
            return(result);
        }
        /// <summary>
        /// Downloads a file from a URL
        /// </summary>
        /// <param name="url">The URL to download from</param>
        /// <returns>True if it was successful, false if not</returns>
        private async Task <bool> DownloadUrl(string url)
        {
            //Get the info
            DownloadInfo info = await Downloader.GetDownloadInfoFromUrl(url);

            //Validate
            if (info == null)
            {
                return(false);
            }
            if (info.VideoCodec == null && info.AudioCodec == null)
            {
                return(false);
            }
            if (info.RequestedUrls == null && info.Url == null)
            {
                return(false);
            }

            //Create a new file object
            string httpVideoUrl = "";
            string httpAudioUrl = "";
            int    uuid         = Uuid.GetUuid();

            if (!string.IsNullOrEmpty(info.VideoCodec) && info.VideoCodec != "none")
            {
                httpVideoUrl = "http://" + MediaserverIp + ":" + MediaserverPort + "/download/" + uuid + "." + info.Extension;
            }
            else
            {
                httpAudioUrl = "http://" + MediaserverIp + ":" + MediaserverPort + "/download/" + uuid + "." + info.Extension;
            }

            PlayableFile newFile = new PlayableFile(httpVideoUrl, httpAudioUrl, info.Title, null, new SubtitleInfo[0], info.Duration, false, FileType.Downloaded);

            //Add it to the list of files
            Files.Add(newFile);

            //Send available files updated message
            WebServer.SendRawData("AvailableFilesUpdated", new byte[0]);

            //Download the file to the temporary folder
            bool isSuccess = await Downloader.DownloadFileFromUrl(url, DownloadFilesPath + "\\incomplete\\" + uuid + "." + info.Extension);

            //If it was a success, change the availability to true, and add to the http server
            if (isSuccess)
            {
                File.Move(DownloadFilesPath + "\\incomplete\\" + uuid + "." + info.Extension, DownloadFilesPath + "\\" + uuid + "." + info.Extension);
                //SetHttpServerFiles();
            }
            //If it wasn't a success, remove it from the list of files
            else
            {
                Files.Remove(newFile);
                newFile.IsAvailable = true;
                WebServer.SendRawData("AvailableFilesUpdated", new byte[0]);
            }

            //Send available files updated message
            //WebServer.SendRawData("AvailableFilesUpdated", new byte[0]);

            //Return whether it was success
            return(isSuccess);
        }