From c84961e03e045a708b8344d0ff097ac1fc122294 Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Mon, 2 Oct 2023 10:33:09 +0200 Subject: [PATCH] feat: download video file and save to mp4 --- Google Drive/Google_Drive_Download_file.ipynb | 40 ++- .../Google_Drive_Download_video_file.ipynb | 316 ++++++++++++++++++ 2 files changed, 348 insertions(+), 8 deletions(-) create mode 100644 Google Drive/Google_Drive_Download_video_file.ipynb diff --git a/Google Drive/Google_Drive_Download_file.ipynb b/Google Drive/Google_Drive_Download_file.ipynb index 95e75e3a7b..d175da0cc6 100644 --- a/Google Drive/Google_Drive_Download_file.ipynb +++ b/Google Drive/Google_Drive_Download_file.ipynb @@ -19,7 +19,7 @@ "tags": [] }, "source": [ - "# Google Drive - Download file\n", + "# Google Drive - Download video file\n", "

Give Feedback | Bug report" ] }, @@ -31,7 +31,7 @@ "tags": [] }, "source": [ - "**Tags:** #googledrive #google #drive #download #object #python #api" + "**Tags:** #googledrive #google #drive #download #mp4 #object #python #api" ] }, { @@ -64,7 +64,7 @@ "tags": [] }, "source": [ - "**Description:** This notebook download a file in a Google Drive account." + "**Description:** This notebook download a video file from a Google Drive account and save into your local in mp4." ] }, { @@ -112,8 +112,10 @@ }, "outputs": [], "source": [ + "from googleapiclient.http import MediaIoBaseDownload\n", + "from googleapiclient.discovery import build\n", "from google.oauth2.service_account import Credentials\n", - "from googleapiclient.discovery import build" + "import io" ] }, { @@ -147,7 +149,7 @@ "outputs": [], "source": [ "service_account_file = 'service_account.json'\n", - "object_id = \"1NkwIJ1GysFOkRLYGZA2HP9Cb4HplEUE7\"" + "object_id = \"10XwzBEJhWAjUamqtmRL-klB_-WfQ05rj\"" ] }, { @@ -197,7 +199,7 @@ "tags": [] }, "source": [ - "### Download file" + "### Download video file" ] }, { @@ -210,13 +212,27 @@ }, "outputs": [], "source": [ - "def download_file(\n", + "def download_video_file(\n", " service,\n", " object_id,\n", "):\n", + " # Init\n", + " request = service.files().get_media(fileId=object_id)\n", + "\n", + " # Download the file\n", + " fh = io.BytesIO()\n", + " downloader = MediaIoBaseDownload(fh, request)\n", + " done = False\n", + " while done is False:\n", + " status, done = downloader.next_chunk()\n", + " print(\"Download %d%%.\" % int(status.progress() * 100))\n", + "\n", + " # Write the file to disk\n", + " with open(f\"{object_id}.mp4\", 'wb') as f:\n", + " f.write(fh.getvalue())\n", " return object_id\n", "\n", - "object_id = download_file(\n", + "object_id = download_video_file(\n", " service,\n", " object_id,\n", ")" @@ -256,6 +272,14 @@ "source": [ "object_id" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "69b4836e-7054-42a6-8e06-74baaf7cff4f", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/Google Drive/Google_Drive_Download_video_file.ipynb b/Google Drive/Google_Drive_Download_video_file.ipynb new file mode 100644 index 0000000000..d175da0cc6 --- /dev/null +++ b/Google Drive/Google_Drive_Download_video_file.ipynb @@ -0,0 +1,316 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "reliable-framework", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "first-flesh", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Google Drive - Download video file\n", + "

Give Feedback | Bug report" + ] + }, + { + "cell_type": "markdown", + "id": "380d25e5-dff9-48b8-9ba4-02d8a378a3f0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #googledrive #google #drive #download #mp4 #object #python #api" + ] + }, + { + "cell_type": "markdown", + "id": "6dadd151-c64c-4e1d-a8b0-c388991d9061", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)" + ] + }, + { + "cell_type": "markdown", + "id": "8c17ff12-47cd-48c9-802c-fc5746a70e2d", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-09-29 (Created: 2023-09-29)" + ] + }, + { + "cell_type": "markdown", + "id": "f1e785ec-37cc-4183-9bf0-45d3bcb60fe8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook download a video file from a Google Drive account and save into your local in mp4." + ] + }, + { + "cell_type": "markdown", + "id": "c40d71d3-b383-47bf-9f12-3cb97174a758", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:**\n", + "- [Google Drive API Documentation](https://developers.google.com/drive/api/v3/reference)\n", + "- [Google Drive Python Quickstart](https://developers.google.com/drive/api/v3/quickstart/python)" + ] + }, + { + "cell_type": "markdown", + "id": "5293e6b6-e0a4-4236-9ab6-1f49e03bd61e", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "54354ce2-314f-4e9b-9b27-4fbaa4b9470a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dcc7668a-6dae-46c6-9258-112ef1e0e0ce", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "from googleapiclient.http import MediaIoBaseDownload\n", + "from googleapiclient.discovery import build\n", + "from google.oauth2.service_account import Credentials\n", + "import io" + ] + }, + { + "cell_type": "markdown", + "id": "14eed474-177d-4481-bc68-3df1052009ec", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n", + "**Pre-requisite**\n", + "\n", + "You need to set up service account credentials to use Google Drive API. For detailed instructions on how to do this, please refer to the [Google Drive Python Quickstart guide](https://developers.google.com/drive/api/v3/quickstart/python).\n", + "\n", + "After creating the service account, you will get a client_email (found in your service account JSON file). Make sure to share your Google Drive or specific folder with this client_email.\n", + "\n", + "**Mandatory**\n", + "- `service_account_file`: This is the JSON file that contains your service account credentials. You'll get this file when you create a service account.\n", + "- `object_id`: Object ID to be downloaded." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39e16c43-910f-4fe9-8169-bb8e5ae0f207", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "service_account_file = 'service_account.json'\n", + "object_id = \"10XwzBEJhWAjUamqtmRL-klB_-WfQ05rj\"" + ] + }, + { + "cell_type": "markdown", + "id": "58f24ccc-992f-4226-b222-297a71547d39", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "e8231d86-3c7f-473e-99df-9ad6fbff6f21", + "metadata": {}, + "source": [ + "### Connect to GCP" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bf55e0e3-a56d-46d7-b332-46f8c80cfa73", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def gcp_connect(file_path): \n", + " # Load the service account credentials\n", + " credentials = Credentials.from_service_account_file(file_path)\n", + "\n", + " # Build the service\n", + " service = build('drive', 'v3', credentials=credentials)\n", + " return service\n", + " \n", + "service = gcp_connect(service_account_file)" + ] + }, + { + "cell_type": "markdown", + "id": "584a2ded-458a-4bd2-905a-1de213630d6a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Download video file" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fd1504e2-4b7c-47b1-82ab-9835aca3f19c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "def download_video_file(\n", + " service,\n", + " object_id,\n", + "):\n", + " # Init\n", + " request = service.files().get_media(fileId=object_id)\n", + "\n", + " # Download the file\n", + " fh = io.BytesIO()\n", + " downloader = MediaIoBaseDownload(fh, request)\n", + " done = False\n", + " while done is False:\n", + " status, done = downloader.next_chunk()\n", + " print(\"Download %d%%.\" % int(status.progress() * 100))\n", + "\n", + " # Write the file to disk\n", + " with open(f\"{object_id}.mp4\", 'wb') as f:\n", + " f.write(fh.getvalue())\n", + " return object_id\n", + "\n", + "object_id = download_video_file(\n", + " service,\n", + " object_id,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "8677efe4-4b2c-49b9-9006-faa79b927610", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "0848c423-70cf-4188-ba13-a3827e55375e", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c3a945cb-347f-42dd-82ef-e863ee18d97a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "object_id" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "69b4836e-7054-42a6-8e06-74baaf7cff4f", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + }, + "naas": { + "notebook_id": "4d62505c0b27fe497af44172fffb4bd8ca5ed2bcfcd1945d9434f00347ac442d", + "notebook_path": "Google Drive/Google_Drive_Download_file.ipynb" + }, + "papermill": { + "default_parameters": {}, + "environment_variables": {}, + "parameters": {}, + "version": "2.3.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}