Josep Valls
Josep Valls

Reputation: 5560

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.

Upvotes: 7

Views: 2452

Answers (2)

kaytwo
kaytwo

Reputation: 46

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']

Upvotes: 3

ashleedawg
ashleedawg

Reputation: 21639

This is in the wrong language from the OP, but I assume translating to a Python cURL call is a straightforward task.

I successfully used the function below to retrieve Google Photos image and video files sizes by calling it with the file's baseUrl, as retrieved from the API:

function retrieve_remote_file_size($url){
     $ch = curl_init($url);

     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_HEADER, TRUE);
     curl_setopt($ch, CURLOPT_NOBODY, TRUE);

     $data = curl_exec($ch);
     $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

     curl_close($ch);
     return $size;
}

(Source)

Note that (contrary to the "Warning" at the above baseUrl link) I did not need to specify a width/height or download parameter in order for the baseUrl to work with this function.

Upvotes: 0

Related Questions