Esempio n. 1
0
        /// <summary>
        /// Play the youtube video in the attached Video Player component.
        /// </summary>
        /// <param name="videoUrl">Youtube url (e.g. https://www.youtube.com/watch?v=VIDEO_ID)</param>
        /// <returns>A Task to await</returns>
        /// <exception cref="NotSupportedException">When the youtube url doesn't contain any supported streams</exception>
        public async Task PlayVideoAsync(string videoUrl = null)
        {
            try
            {
                videoUrl = videoUrl ?? youtubeUrl;

                var metaData = await YoutubeDl.GetVideoMetaDataAsync(videoUrl,
                                                                     is360Video?YoutubeDlOptions.Three60 : YoutubeDlOptions.Default);

                m_VideoPlayer.source = VideoSource.Url;

                //Resetting the same url restarts the video...
                if (m_VideoPlayer.url != metaData.Url)
                {
                    m_VideoPlayer.url = metaData.Url;
                }

                m_VideoPlayer.Play();
                youtubeUrl = videoUrl;
                YoutubeVideoStarting?.Invoke(youtubeUrl);
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Download a youtube video to a destination folder
        /// </summary>
        /// <param name="destinationFolder">A folder to create the file in</param>
        /// <param name="videoUrl">Youtube url (e.g. https://www.youtube.com/watch?v=VIDEO_ID)</param>
        /// <param name="progress">An object implementing `IProgress` to get download progress, from 0 to 1</param>
        /// <param name="cancellationToken">A CancellationToken used to cancel the current async task</param>
        /// <returns>Returns the path to the file where the video was downloaded</returns>
        /// <exception cref="NotSupportedException">When the youtube url doesn't contain any supported streams</exception>
        public async Task <string> DownloadVideoAsync(string destinationFolder = null, string videoUrl = null, CancellationToken cancellationToken = default)
        {
            try
            {
                videoUrl = videoUrl ?? youtubeUrl;

                var video = await YoutubeDl.GetVideoMetaDataAsync(videoUrl);

                cancellationToken.ThrowIfCancellationRequested();

                var fileName = $"{video.Title}.{video.Extension}";

                var invalidChars = Path.GetInvalidFileNameChars();
                foreach (var invalidChar in invalidChars)
                {
                    fileName = fileName.Replace(invalidChar.ToString(), "_");
                }

                var filePath = fileName;
                if (!string.IsNullOrEmpty(destinationFolder))
                {
                    Directory.CreateDirectory(destinationFolder);
                    filePath = Path.Combine(destinationFolder, fileName);
                }

                await YoutubeDownloader.DownloadAsync(video, filePath, cancellationToken);

                return(filePath);
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
                return(null);
            }
        }
        /// <summary>
        /// Triggers a request to youtube-dl to parse the webpage for the raw video url
        /// </summary>
        /// <param name="videoUrl">Youtube url (e.g. https://www.youtube.com/watch?v=VIDEO_ID)</param>
        /// <param name="options">Options for downloading the raw video</param>
        /// <param name="cancellationToken">A CancellationToken used to cancel the current async task</param>
        /// <returns>A Task to await</returns>
        public static async Task <string> GetRawVideoUrlAsync(string videoUrl, YoutubeDlOptions options = null, CancellationToken cancellationToken = default)
        {
            options = options ?? YoutubeDlOptions.Default;
            var metaData = await YoutubeDl.GetVideoMetaDataAsync(videoUrl, options, cancellationToken);

            return(metaData.Url);
        }
Esempio n. 4
0
        async void Start()
        {
            Debug.Log("Loading...");
            var metadata = await YoutubeDl.GetVideoMetaDataAsync <MyYoutubeVideoMetadata>(videoUrl);

            Debug.Log($"Video description: {metadata.Description}");
            Debug.Log($"View count: {metadata.ViewCount}");
        }
Esempio n. 5
0
        async void Start()
        {
            Log("Loading...");

            // Optimize by specifying the fields we're interested in, to avoid downloading everything.
            // Optional. When omitted, the fields will be read from the type by reflection.
            string[] fields = { "title", "description", "view_count" };

            var metadata = await YoutubeDl.GetVideoMetaDataAsync <MyYoutubeVideoMetadata>(videoUrl, fields);

            Log($"- Video title: {metadata.Title}");
            Log($"- Video description: {metadata.Description}");
            Log($"- View count: {metadata.ViewCount}");
        }
        public static async Task PlayYoutubeVideoAsync(this VideoPlayer videoPlayer, string youtubeUrl, CancellationToken cancellationToken = default)
        {
            var metaData = await YoutubeDl.GetVideoMetaDataAsync <YoutubeVideoMetaData>(youtubeUrl, YoutubeDlOptions.Default, k_PlayFields, cancellationToken);

            var rawUrl = metaData.Url;

            videoPlayer.source = VideoSource.Url;

            //Resetting the same url restarts the video...
            if (videoPlayer.url != rawUrl)
            {
                videoPlayer.url = rawUrl;
            }

            await videoPlayer.PrepareAsync(cancellationToken);

            videoPlayer.Play();
        }
        /// <summary>
        /// Download a youtube video to a destination folder
        /// </summary>
        /// <param name="destinationFolder">A folder to create the file in</param>
        /// <param name="videoUrl">Youtube url (e.g. https://www.youtube.com/watch?v=VIDEO_ID)</param>
        /// <param name="cancellationToken">A CancellationToken used to cancel the current async task</param>
        /// <returns>Returns the path to the file where the video was downloaded</returns>
        public async Task <string> DownloadVideoAsync(string destinationFolder = null, string videoUrl = null, CancellationToken cancellationToken = default)
        {
            videoUrl = videoUrl ?? youtubeUrl;

            var video = await YoutubeDl.GetVideoMetaDataAsync(videoUrl);

            cancellationToken.ThrowIfCancellationRequested();

            var fileName = GetVideoFileName(video);

            var filePath = fileName;

            if (!string.IsNullOrEmpty(destinationFolder))
            {
                Directory.CreateDirectory(destinationFolder);
                filePath = Path.Combine(destinationFolder, fileName);
            }

            await YoutubeDownloader.DownloadAsync(video, filePath, cancellationToken);

            return(filePath);
        }