Search code examples
javagroovyyoutube-apiyoutube-data-api

How do I add the original file name to a YouTube API video upload?


How do I set the file name for my YouTube video uploads?

I am uploading via the videos.insert endpoint in the YouTube v3 API.

If I omit the fileDetails part, everything else works as expected.

I tried to add the fileDetails part, like so:

        List<String> parts = []
        def videoData = new Video()
        // ...omitted code to set up snippet, etc...

        // set file details:
        def fileDetails = new VideoFileDetails()
        fileDetails.setFileName("filename.mp4")
        fileDetails.setCreationTime(fileAttributes.creationTime().toInstant().toString())
        fileDetails.setFileSize(BigInteger.valueOf(Files.size(videoFileInfo.filePath)))
        videoData.setFileDetails(fileDetails)
        parts.add('fileDetails')

        // upload the video
        def mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT, Files.newInputStream(videoFileInfo.filePath))
        mediaContent.setLength(Files.size(videoFileInfo.filePath))

        def insertCommand = youTube.videos().insert(parts.join(','), videoData, mediaContent)
        // ...omitted code to set up progress listener...
        def returnedVideo = insertCommand.execute()

I get a 400 response:

POST https://www.googleapis.com/upload/youtube/v3/videos?part=status,snippet,fileDetails&uploadType=resumable
{
  "code" : 400,
  "errors" : [ {
    "domain" : "youtube.part",
    "location" : "part",
    "locationType" : "parameter",
    "message" : "'{0}'",
    "reason" : "unexpectedPart"
  } ],
  "message" : "'{0}'"
}

Edit

I have also tried specifying the fileDetails via PUT request with the same result:

//        def fileDetails = new VideoFileDetails()
//        fileDetails.setFileName(fileName)
//        fileDetails.setCreationTime(videoFileInfo.fileAttributes.creationTime().toInstant().toString())
//        fileDetails.setFileSize(BigInteger.valueOf(Files.size(videoFileInfo.filePath)))
//        returnedVideo.setFileDetails(fileDetails)
//        parts.add('fileDetails')
//        youTube.videos().update('fileDetails', returnedVideo).execute()

Solution

  • can't test it

    according to Add filename to YouTube API upload - you can't set fileDetails part

    however to set filename the same link suggesting to set Slug http header

    so, try this code:

    def insertCommand = youTube.videos().insert(...)
    def headers = insertCommand.getRequestHeaders()
    headers.set('Slug', "filename.mp4")
    insertCommand.setRequestHeaders(headers) //not sure if this required
    ...
    insertCommand.execute()
    

    i found reference to Slug header only in error description: https://developers.google.com/youtube/v3/docs/videos/insert#errors

    invalidFilename: The video filename specified in the Slug header is invalid.