Youtube API - 使用vb.net上传大文件

时间:2011-12-13 00:08:40

标签: vb.net api youtube

我正在尝试使用VB.NET中的API将大型(4MB +)文件上传到youtube。 较小的文件上传很好,但是任何大于4MB的文件都会产生一个错误(我认为)实际上与超时有关:请求被中止:请求被取消了。

我已阅读并重新阅读API doco,Google搜索等在VB.NET中寻找示例,但似乎没有任何内容可供vb.net使用 一些程序员遇到了同样的问题,答案都是围绕c#或Java - 我都不熟悉。 我尝试了settings.timeout和settings.maximum的不同组合,但它似乎没有什么区别

目前的代码是:

Sub UploadYouTube(ByVal sSourceFile As String,ByVal sTitle As String,ByVal sMediaCategory As String,ByVal sDesc As String)         Dim uSettings作为YouTubeRequestSettings,uRequest As YouTubeRequest,newVideo As Video,CreatedVideo As Video,VideoId As String         Dim vContentType As String =“video”         尝试             uSettings =新的YouTubeRequestSettings(,,,)

        uRequest = New YouTubeRequest(uSettings)

        newVideo = New Video()
        newVideo.Title = sTitle '"Test";
        newVideo.Tags.Add(New MediaCategory("Education", YouTubeNameTable.CategorySchema))
        newVideo.Description = sDesc        '"Testing Testing Testing"
        newVideo.YouTubeEntry.Private = False
        uRequest.Settings.Timeout = 60 * 60 * 1000
        uRequest.Settings.Maximum = 2000000000

        ' Determine the content type
        If sSourceFile.EndsWith(".mov") Then
            vContentType = "video/quicktime"
        ElseIf sSourceFile.EndsWith(".avi") Or sSourceFile.EndsWith(".mpg") Or sSourceFile.EndsWith(".mpeg") Then
            vContentType = "video/mpeg"
        ElseIf sSourceFile.EndsWith(".wmv") Then
            vContentType = "video/x-ms-wmv"
        ElseIf sSourceFile.EndsWith(".m4v") Then
            vContentType = "video/m4v"
        ElseIf sSourceFile.EndsWith(".mp4") Then
            vContentType = "video/mp4"
        ElseIf sSourceFile.EndsWith(".3gp") Then
            vContentType = "video/3gpp"
        End If
        newVideo.YouTubeEntry.MediaSource = New MediaFileSource(sSourceFile, vContentType)

        CreatedVideo = uRequest.Upload(newVideo)
        VideoId = CreatedVideo.VideoId
        ' Save the video Id to the database!
    Catch ex As Exception
        debug.print("Error. MainModule.Main. " & ex.Message, 5)
    End Try
End Sub

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

Python示例:https://github.com/Mathieu69/Pitivi_Gargamel/blob/upload_merger/pitivi/uploader.py在3个月前做过,希望它有所帮助。

答案 1 :(得分:0)

我试图通过使用backgroundworker来解决超时问题。它有效。它似乎并没有真正在后台工作。我认为RunWorkerAsync会启动,继续下一个命令,然后回发。相反它只是挂起几分钟,就像上传整个75MB文件一样,然后回发成功。如果我带走了背景工作者然后只是执行上传,它会像你的那样失败。这是我的代码有用。

Sub up_load(s As Object, e As EventArgs)
    Dim worker As BackgroundWorker = New BackgroundWorker

    worker.WorkerReportsProgress = True
    worker.WorkerSupportsCancellation = True
    AddHandler (worker.DoWork), AddressOf begin_upload
    worker.RunWorkerAsync()
    lblmsg.Text = "Successfully initiated upload"
End Sub

Sub begin_upload(s As Object, e As DoWorkEventArgs)

    Dim request As New YouTubeRequest(settings)
    Dim vidupload As New Video()

    vidupload.Title = "My Big Test Movie"
    vidupload.Tags.Add(New MediaCategory("Nonprofit", YouTubeNameTable.CategorySchema))
    vidupload.Keywords = "church, jesus"
    vidupload.Description = "See the entire video"
    vidupload.YouTubeEntry.Private = False
    vidupload.YouTubeEntry.setYouTubeExtension("location", "Downers Grove, IL")
    vidupload.YouTubeEntry.MediaSource = New MediaFileSource("c:\users\greg\test3.asf", "video/x-ms-wmv")

    Dim createdVideo As Video = Request.Upload(vidupload)
End Sub