如何从媒体网址(视频)获取可搜索的流c#

时间:2018-06-23 13:23:16

标签: c# youtube netstream

所以我的问题是我可以直接链接到youtube视频,我可以毫无问题地下载该视频,现在无法暂停,无需从偏移量0重新启动就可以恢复

有什么方法可以下载/获取网络流(youtube视频)并寻找我想要的偏移量吗?

可自编译的简单示例(添加NuGet软件包VideoLibrary)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using VideoLibrary;
namespace YoutubeDownloader
{
    class Program
    {
        static void Main(string[] args)
        {
            YouTube youtube = YouTube.Default;
            // 19 seconds of nature slides Totaly SFW NO AUDIO too
            YouTubeVideo video = youtube
                .GetVideo("https://www.youtube.com/watch?v=668nUCeBHyY");
            string outFile = $"dummy2{video.FileExtension}";
            FileStream fs = new FileStream(outFile, FileMode.OpenOrCreate);
            // problem if the user started the download ,then it break at somepoint 
            // I know the video is on-demand so what ever position you wanna play 
            // I can get 
            // but can't because the stream is a net stream and can't be seekable 
            // here is what I tried but it throws size bigger than expected 
            Stream videoStream = WebRequest
                .Create(video.Uri).GetResponse().GetResponseStream();
            byte[] buff = new byte[1024];
            int i = 0;
            int n = 0;
            while ((n = videoStream.Read(buff, i, buff.Length)) > 0)
            {

                fs.Write(buff, i, buff.Length);
                i++;
            }
            // right now i download and get the video's like this 
            using (WebClient client = new WebClient())
            {
                // I use as assync and handle events but for simplicity 
                // i'm just gonna use this
                client.DownloadProgressChanged += 
                    new DownloadProgressChangedEventHandler(downloadProgressChanged);
                client.DownloadFileCompleted +=
                    new System.ComponentModel.AsyncCompletedEventHandler(downloadFInished);
                client.DownloadFileAsync(new Uri(video.Uri), outFile);
                // but I have no way of resuming or pausing 

            }
        }
        private static void downloadFInished(object sender, AsyncCompletedEventArgs e)
        {
            // do something on download finshed 
        }
        private static void downloadProgressChanged(object sender,
            DownloadProgressChangedEventArgs e)
        {
            // show progress or what ever 
        }
    }
}

粘贴bin链接https://pastebin.com/3zqsVLA1

谢谢

0 个答案:

没有答案