c# - 上传视频文件后如何获取 youtube 视频链接?

标签 c# .net youtube-api youtube-data-api google-api-dotnet-client

这是视频文件上传时的事件:

public static TimeSpan time = new TimeSpan();
Video objects = null;

private void videosInsertRequest_ResponseReceived(Video obj)
{
    System.Timers.Timer aTimer;
    aTimer = new System.Timers.Timer();
    aTimer.Elapsed += aTimer_Elapsed;
    aTimer.Interval = 10000;
    aTimer.Enabled = false;
    uploadstatus = obj.Status.UploadStatus;
    if (uploadstatus == "uploaded")
    {
        fileuploadpercentages = 100; 
        stopwatch.Stop();
        var milliseconds = stopwatch.ElapsedMilliseconds;
        time = stopwatch.Elapsed;
        time = TimeSpan.FromSeconds(Math.Round(time.TotalSeconds));
        uploadstatus = "file uploaded successfully";
        string[] lines = File.ReadAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt");
        File.WriteAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt", lines.Skip(1));
        if (Form1.uploadedFilesList.Count > 0)
            Form1.uploadedFilesList.RemoveAt(0);                
    }
    if (uploadstatus == "Completed")
    {

    }

    objects = obj;
}

我使用了一个断点,我看到变量 obj 有一些属性,其中之一是 id。但是没有链接属性或显示链接的东西。

这就是我上传视频的方式,但我不确定它是否有助于我在上传完成后找到上传的视频链接。

private void UploadVideo(string FileName, string VideoTitle, string VideoDescription)
{
    try
    {
        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
        });

        video.Snippet = new VideoSnippet();
        video.Snippet.Title = VideoTitle;
        video.Snippet.Description = VideoDescription;
        video.Snippet.Tags = new string[] { "tag1", "tag2" };
        video.Status = new VideoStatus();
        video.Status.PrivacyStatus = "public";
        using (var fileStream = new FileStream(FileName, FileMode.Open))
        {

            const int KB = 0x400;
            var minimumChunkSize = 256 * KB;

            var videosInsertRequest = youtubeService.Videos.Insert(video,
                "snippet,status", fileStream, "video/*");
            videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
            videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
            // The default chunk size is 10MB, here will use 1MB.
            videosInsertRequest.ChunkSize = minimumChunkSize * 3;
            dt = DateTime.Now;
            totalBytes = fileStream.Length;
            videosInsertRequest.Upload();
        }
    }
    catch (Exception errors)
    {
        string errorss = errors.ToString();
    }
}

最佳答案

.net sample ,在视频上传后,有一些代码可以检查我认为是视频的 ID:

void videosInsertRequest_ResponseReceived(Video video)
    {
      Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
    }

因此您应该能够传入您的 video 对象并获取其 ID。

或者,您可以尝试使用 playlists.list() 查看最近上传的内容并指定您上传的播放列表 ID,然后获取最新视频的 ID。

关于c# - 上传视频文件后如何获取 youtube 视频链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33533078/

相关文章:

javascript - gapi.client.youtube 未定义?

c# - 透明重叠圆形进度条(自定义控件)

c# - DDD(Domain Driven Design),如何处理实体状态变化,封装需要处理大量数据的业务规则

c# - 在 MVC 3 中使用现有的 Web 服务

c# - AES _Encryption 在 Mysql 中,在 C#.Net 中解密

c# - 在 .Net 中调用命名空间的最佳方法是什么

c# - 结束任务以防止失控的最佳方式是什么

youtube-api - YouTube 数据 API V3 - 错误访问未配置

android - 如果用户没有安装 YouTube 应用程序,我该如何在 Android 应用程序中播放 YouTube 视频?

c# - MvvmCross 绑定(bind)到 UIButton.TitleLabel.Text