1

I have downloaded mp4 video file from google drive to my website.

$service = new Google_Service_Drive($client);
$file_id = "1JpRYTuW_tlxcjgdsfhsdfhRmJFfjxJA3Rh";

$response = $service->files->get($file_id, array('alt' => 'media'));
$content  = $response->getBody()->getContents();

$file = '1JpRYTuW_tlxcjgdsfhsdfhRmJFfjxJA3Rh.mp4';
file_put_contents($file, $content, LOCK_EX);

But I cannot play it, because of video format. So I have two questions:

  1. How can I download video from google drive with API v3 and service account in original video format?

  2. How I can embed non shared video from google drive with API v3 and service account to my website?

Thanks

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Alex
  • 111
  • 13
  • you need to add the mimetype to your download. – Linda Lawton - DaImTo Oct 09 '19 at 06:32
  • But mimetype for export, not for get. Export works with files smaller than 10mb. – Alex Oct 09 '19 at 07:08
  • Alex: Hello! I have used the exact same code to try and download an mp4 file from my drive into my computer, and it worked as expected. Could you try manually opening the downloaded mp4 file and making sure the contents are OK? This would mean that the problem is in the embedding part. Also, may I ask you to attach the code you are using for embedding that video into your website? Cheers – carlesgg97 Oct 09 '19 at 09:58
  • Video downloaded by API opens on desktop computer but it plays only audio or nothing on the website. However, if I convert it and upload it to the website it works. Also, if I download video from the google disk as user and upload it to the website it works. I embed video with html5 video tag. – Alex Oct 09 '19 at 10:26
  • Alex: Thanks for providing this information. There seems to be a bug somewhere along the line. Is it possible that you share the original video so I can test it myself and try to find a solution? Otherwise, could you provide codec information of the original and downloaded files (using a tool such as mediainfo on linux)? Cheers – carlesgg97 Oct 09 '19 at 12:52
  • My link for the test page https://hometask.consultant.co.ua/video.html Link for the videos https://hometask.consultant.co.ua/5.mp4 https://hometask.consultant.co.ua/7.mp4 https://hometask.consultant.co.ua/9.mp4 5.mp4 was downloaded with google drive API. 7.mp4 was downloaded as user from the google drive. 9.mp4 it is 5.mp4 converted on PC and uploaded to the website by ftp. – Alex Oct 10 '19 at 12:39
  • @Alex Thanks a lot for providing the links! I have been able to verify that in fact 5.mp4 is corrupted, despite playing perfectly in my computer (I'm using the latest version of Google Chrome). Could you also provide the original video so that I can reproduce the issue and find exactly what is going on? Thanks again – carlesgg97 Oct 10 '19 at 15:14
  • I downloaded other video and it works. Are there any way to get video from google drive with API and play it without saving to disk? – Alex Oct 10 '19 at 19:31
  • @Alex The issue is definitely related to that specific file you were using, as it works with other videos. Could you provide it in order for me to confirm it? – carlesgg97 Oct 11 '19 at 08:46
  • @Alex To play that video from the website, another option you have is to share the video only to those who possess the link, and then embed it. You can also periodically sync (ie CRON task) your google drive folder with your server so that it can serve the content to your users. see: https://github.com/gdrive-org/gdrive – carlesgg97 Oct 11 '19 at 09:03
  • I think it was a broken mp4 file. What about embedding video into my site? I don't want users to share the links to the google drive files. I understand that it is impossible to do hundred percent but I want to limit it as much as possible. Are there any easy ways to get embedding code from google drive by API? – Alex Oct 12 '19 at 12:48
  • @Alex The best option you have is to synchronise the videos from your google drive to your server (see my last response) and serve them behind authentication. You could have endpoints such as https://www.yourwebsite.com/videos/21515.mp4 that would require the user to be signed in to your website in order to access it. You may find more information here: https://stackoverflow.com/questions/3603271/http-file-access-and-php-sessions – carlesgg97 Oct 14 '19 at 10:46

0 Answers0