如何在上传视频文件后获取youtube视频链接?

时间:2015-11-04 22:13:11

标签: 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();
    }
}

1 个答案:

答案 0 :(得分:0)

.net sample中,视频上传后,会有一些代码来检查我认为的视频ID:

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

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

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