pythonhttp-headersvideo-streaminggoogle-photosgoogle-photos-api

Retrieve file size for videos stored on Google Photos


Context: I wanted to see how I'm using my Google Photos space and I wrote a little script in Python that uses the Google Photos API to retrieve all my albums and it's contents (using https://developers.google.com/photos/library/reference/rest/v1/mediaItems/search). The file information is not there but using the mediaItem baseUrl (documented https://developers.google.com/photos/library/reference/rest/v1/mediaItems#MediaItem) I can then perform a HEAD request and get the content-length from the headers. This seems to work fine for the photos but the size for videos is grossly underestimated. My guess is that Google Photos is getting ready to stream the videos and it's not sending the whole video information.

Question: Is there any way to retrieve file size for videos stored on Google Photos, hopefully, without having to download the whole video? The app does know about the file size, but that doesn't seem to be available in the API. Is there any way to send some request headers to get the file size?

Extra info: I'm using Python and the httplib2.Http() for my HEAD requests (happy to use the requests module or any other alternative).

This is the information retrieved from the API, this video file is a little over 100MB (definitely not 30k):

{
  "id": "XYZ",
  "productUrl": "https://photos.google.com/lr/photo/XYZ",
  "baseUrl": "https://lh3.googleusercontent.com/lr/ABC",
  "mimeType": "video/mp4",
  "mediaMetadata": {
    "creationTime": "2018-11-27T03:43:27Z",
    "width": "1920",
    "height": "1080",
    "video": {
      "fps": 120,
      "status": "READY"
    }
  },
  "filename": "VID_20181126_174327.mp4"
}

These are the headers received from the HEAD request to baseUrl:

{
  "access-control-expose-headers": "Content-Length",
  "etag": "\"v15ceb\"",
  "expires": "Fri, 01 Jan 1990 00:00:00 GMT",
  "cache-control": "private, max-age=86400, no-transform",
  "content-disposition": "inline;filename=\"VID_20181126_174327.jpg\"",
  "content-type": "image/jpeg",
  "vary": "Origin",
  "x-content-type-options": "nosniff",
  "date": "Wed, 08 May 2019 17:39:42 GMT",
  "server": "fife",
  "content-length": "31652",
  "x-xss-protection": "0",
  "alt-svc": "quic=\":443\"; ma=2592000; v=\"46,44,43,39\"",
  "status": "200",
  "content-location": "https://lh3.googleusercontent.com/lr/ABC"
}

Thanks.


Solution

  • The Photos API docs mention that for videos, the baseUrl that the API returns refers to a thumbnail for the video rather than the video itself, and you've got to append =dv to the baseUrl to actually receive the video. It also looks from my experimentation that the content-length that comes back from that endpoint is accurate:

    import requests
    
    baseUrl = "https://lh3.googleusercontent.com/lr/AF..."
    # just the thumbnail's size
    requests.head(baseUrl,allow_redirects=True).headers['content-length']
    # the entire video's size
    requests.head(baseUrl + "=dv",allow_redirects=True).headers['content-length']