Retrieve file size for videos stored on Google Photos
Asked Answered
I

2

7

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.

Illailladvised answered 8/5, 2019 at 17:58 Comment(6)
Happy to share the script once I get this resolved. It reports size by album, media type and camera (so you can discount the free space provided for Pixel owners)Illailladvised
Although I'm not sure whether this is the method you want, for example, if you can see the folder of Google Photos, you can retrieve the file size using Drive API. How about this?Ferreira
Thanks for the suggestion. I just tried the Google Drive API and the Photos files are not listed. I see an option to "backup" files from Photos do Drive on my Photos app but I anticipate this will take even more of my limited space.Illailladvised
Thank you for replying. When I see my Google Drive, I can see the folder of Google Photos. And when I retrieve the files in the folder of Google Photos using Drive API, all files can be retrieved. So I proposed to use Drive API. Is this situation different from your situation?Ferreira
@Ferreira Thanks for your comment. I checked again; I was originally searching for the video files in my Google Drive and couldn't find them. I do see a Google Photos folder but if I list it, I only see subfolders until 2017. That is around the time I changed my phone (upgraded to Pixel). Those are the photos and videos that currently count towards my quota and I can list them but I still would like to be able to check the file size of the recent videos that I have taken with the Pixel and that in the near future will count towards storage space.Illailladvised
Also, just in case anyone is interested, the Google Drive file identifiers don't match the media item identifiers from Google Photos which is a bummer to match file sizes to albums; I guess I can still use file names. Also, according to Google Drive, old video files in the Google Photos folder report a file size but quotaBytesUsed is 0 even those files are using Google Photos quota.Illailladvised
P
3

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']
Patentee answered 28/2, 2021 at 21:52 Comment(0)
P
0

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.

Phenformin answered 12/9, 2019 at 12:12 Comment(1)
Thanks for this answer, I have not been able to test it yet. Did you make sure that the sizes were correct? In my code I can check the content length in the header and that size is correct for photo files. The problem is that the reported size is much smaller than the actual size of the video file.Illailladvised

© 2022 - 2024 — McMap. All rights reserved.