2010-06-08 4 views
5

.NET을 사용하여 비디오 업로드 응용 프로그램을 만듭니다. 은 YouTube와 통신하고 파일을 업로드하지만 파일 처리가 실패합니다. YouTube에서 '업로드가 실패했습니다. (동영상 파일을 변환 할 수 없습니다.)'오류 메시지가 표시됩니다. 이것은 아마 내가 그것을 수동으로 할 때 및 프로세스 벌금을 업로드 둘 다 두 개의 서로 다른 비디오와 시도를 한youtube - 비디오 업로드 실패 - 파일을 변환 할 수 없습니다 - 비디오 인코딩을 잘못 인코딩 했습니까?

"... 당신의 비디오는 우리의 컨버터가 인식 할 수없는 형식으로되어"있다는 것을 의미. 따라서 내 코드가 가.) 비디오를 올바르게 인코딩하지 않았거나 b) 내 API를 제대로 보내지 못했습니다. 요청이 적절합니다.

어떤 제안 오류가 극명하게 될 것이다 될 수 있는지에 : 내 API의 PUT 요청을 구성하고 비디오를 인코딩 얼마나 아래

이다.

감사합니다.

P. 내 응용 프로그램은 재개 가능한 업로드 기능을 사용하기 때문에 클라이언트 라이브러리를 사용하지 않습니다. 따라서 수동으로 내 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 
+0

제목에 ".NET"과 같은 태그를 반복하지 마십시오. 그들이 속한 태그에 두십시오. –

답변

0

나는 유튜브가 찾고있는 어떤 형식 모르겠지만, 그것은 당신의 윈도우 시스템에서 인식 할 수 있어야 형식이라면, 당신이 저장 제안 변환 된 비디오를 디스크의 파일로 변환 한 다음 열어보십시오.

0

전송 요청은 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