【问题标题】:youtube - video upload failure - unable to convert file - encoding the video wrong?youtube - 视频上传失败 - 无法转换文件 - 视频编码错误?
【发布时间】:2023-03-12 06:31:02
【问题描述】:

我正在使用 .NET 创建一个视频上传应用程序。虽然是 与 YouTube 通信并上传文件、处理 该文件失败。 YouTube 给了我错误信息,“上传失败 (无法转换视频文件)。”这应该意味着“你的 视频采用我们的转换器无法识别的格式..."

我尝试了两个不同的视频,都上传了 当我手动进行操作时处理正常。所以我怀疑我的代码是 a.) 未正确编码视频和/或 b.) 未发送我的 API 正确请求。

以下是我如何构建我的 API PUT 请求和编码 视频:

任何关于错误可能是什么的建议将不胜感激。

谢谢

附:我没有使用客户端库,因为我的应用程序将使用 可恢复上传功能。因此,我正在手动构建我的 API 请求。

文档:http://code.google.com/intl/ja/apis/youtube/2.0/developers_guide_protocol_resumable_uploads.html#Uploading_the_Video_File

代码:

            // new PUT request for sending video
            WebRequest putRequest = WebRequest.Create(uploadURL);

            // set properties
            putRequest.Method = "PUT";
            putRequest.ContentType = getMIME(file); //the MIME type of the uploaded video file

            //encode video
            byte[] videoInBytes = encodeVideo(file); 

     public static byte[] encodeVideo(string video)
     {
        try
        {
            byte[] fileInBytes = File.ReadAllBytes(video);
            Console.WriteLine("\nSize of byte array containing " + video + ": " + fileInBytes.Length);
            return fileInBytes;
        }
        catch (Exception e)
        {
            Console.WriteLine("\nException:  " + e.Message + "\nReturning an empty byte array");
            byte [] empty = new byte[0];
            return empty;
        }
     }//encodeVideo

            //encode custom headers in a byte array
            byte[] PUTbytes = encode(putRequest.Headers.ToString());

          public static byte[] encode(string headers)
          {            
              ASCIIEncoding encoding = new ASCIIEncoding();
              byte[] bytes = encoding.GetBytes(headers);
              return bytes;
           }//encode 

            //entire request contains headers + binary video data
            putRequest.ContentLength = PUTbytes.Length + videoInBytes.Length;

            //send request - correct?
            sendRequest(putRequest, PUTbytes);
            sendRequest(putRequest, videoInBytes);

     public static void sendRequest(WebRequest request, byte[] encoding)
    {
        Stream stream = request.GetRequestStream(); // The GetRequestStream method returns a stream to use to send data for the HttpWebRequest.

        try
        {
            stream.Write(encoding, 0, encoding.Length);

        }
        catch (Exception e)
        {
            Console.WriteLine("\nException writing stream: " + e.Message);
        }
     }//sendRequest

【问题讨论】:

  • 请不要在标题中重复“.NET”之类的标签。将它们留在它们所属的标签中。

标签: .net video encoding youtube youtube-api


【解决方案1】:

我不知道 YouTube 正在寻找什么格式,但如果它是一种在您的 Windows 系统上应该可以识别的格式,我建议您将转换后的视频保存到磁盘上的文件中,然后尝试打开它。

【讨论】:

    【解决方案2】:

    发送请求分两部分完成...您发送标头,包括您拥有的视频大小... YouTube 以 url 响应,然后您将视频发送到该 url.. 看起来像您正在尝试在一个请求中发送所有内容。有点像这样。

    Try
            Try
                _request = CType(WebRequest.Create(_requestUrl), HttpWebRequest)
    
                With _request
                    .ContentType = "application/atom+xml; charset=UTF-8"
                    .ContentLength = _postBytes.Length
                    .Method = "POST"
                    .Headers.Add("Authorization", String.Format("GoogleLogin auth={0}", MasterAccessToken.ClientLoginToken))
                    .Headers.Add("GData-Version", "2")
                    .Headers.Add("X-GData-Key", String.Format("key={0}", YouTube.Constants.Security.DEVELOPERKEY))
                    .Headers.Add("Slug", filename)
                End With
    
                _writeStream = _request.GetRequestStream
                With _writeStream
                    .Write(_postBytes, 0, _postBytes.Length)
                End With
    
                Using _response = CType(_request.GetResponse, HttpWebResponse)
                    With _response
                        If .StatusCode = HttpStatusCode.OK OrElse .StatusCode = HttpStatusCode.Created Then
                            _ans = _response.Headers("Location")
                        Else
                            Throw New WebException("Cannot get ClientLogin upload location", Nothing, WebExceptionStatus.ProtocolError, _response)
                        End If
                    End With
                End Using
    
            Catch ex As Exception
    
            Finally
                If _writeStream IsNot Nothing Then
                    _writeStream.Close()
                End If
    
            End Try
    
            _videoUploadLocation = _ans
    
            'Got the upload location..... now get the file
            Dim _file As FileInfo = New FileInfo(filename)
            Dim _fileLength As Integer
    
            Using _fileStream As System.IO.FileStream = _file.OpenRead
                _fileLength = CType(_fileStream.Length, Integer)
    
                If _fileLength = 0 Then
                    Throw New FileLoadException("File appears to be of zero length in UploadVideoFromFileClientLogin:", filename)
                End If
    
                'create the webrequest
                _request = CType(WebRequest.Create(_videoUploadLocation), HttpWebRequest)
    
                'No authentication headers needed..
                With _request
                    .Timeout = 6000000       'Timeout for this request changed to 10 minutes
                    .ReadWriteTimeout = 6000000
                    .KeepAlive = True
                    .ContentType = "application/octet-stream"
                    .ContentLength = _fileLength
                    .Method = "POST"
                End With
    
                'and get the stream
                _writeStream = _request.GetRequestStream
    
                'And send it over the net
                m_StreamUtils.CancelRequest = False
                m_StreamUtils.SendStreamToStream(_fileStream, _writeStream, AddressOf UploadPogressChanged)
                m_CancelRequest = m_StreamUtils.CancelRequest
            End Using
    
            If Not (m_CancelRequest) Then
    
                Using _response = CType(_request.GetResponse, HttpWebResponse)
                    With _response
                        If .StatusCode = HttpStatusCode.Created Then
                            _ans = _response.ResponseUri.AbsoluteUri
                        Else
                            Throw New WebException("Cannot get ClientLogin upload location", Nothing, WebExceptionStatus.ProtocolError, _response)
                        End If
                    End With
                End Using
            Else
                _ans = String.Empty
    
            End If
    
            If _writeStream IsNot Nothing Then
                _writeStream.Close()
            End If
    

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 1970-01-01
      • 2012-05-21
      • 2012-03-26
      • 2019-03-15
      • 2017-09-11
      • 1970-01-01
      • 2021-10-12
      • 2012-07-19
      相关资源
      最近更新 更多