Skip to content

Commit

Permalink
feat: download video file and save to mp4
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentLvr committed Oct 2, 2023
1 parent 46fc88c commit c84961e
Show file tree
Hide file tree
Showing 2 changed files with 348 additions and 8 deletions.
40 changes: 32 additions & 8 deletions Google Drive/Google_Drive_Download_file.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"tags": []
},
"source": [
"# Google Drive - Download file\n",
"# Google Drive - Download video file\n",
"<a href=\"https://app.naas.ai/user-redirect/naas/downloader?url=https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/Google%20Drive/Google_Drive_Download_file.ipynb\" target=\"_parent\"><img src=\"https://naasai-public.s3.eu-west-3.amazonaws.com/Open_in_Naas_Lab.svg\"/></a><br><br><a href=\"https://bit.ly/3JyWIk6\">Give Feedback</a> | <a href=\"https://github.com/jupyter-naas/awesome-notebooks/issues/new?assignees=&labels=bug&template=bug_report.md&title=Google+Drive+-+Download+file:+Error+short+description\">Bug report</a>"
]
},
Expand All @@ -31,7 +31,7 @@
"tags": []
},
"source": [
"**Tags:** #googledrive #google #drive #download #object #python #api"
"**Tags:** #googledrive #google #drive #download #mp4 #object #python #api"
]
},
{
Expand Down Expand Up @@ -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."
]
},
{
Expand Down Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -147,7 +149,7 @@
"outputs": [],
"source": [
"service_account_file = 'service_account.json'\n",
"object_id = \"1NkwIJ1GysFOkRLYGZA2HP9Cb4HplEUE7\""
"object_id = \"10XwzBEJhWAjUamqtmRL-klB_-WfQ05rj\""
]
},
{
Expand Down Expand Up @@ -197,7 +199,7 @@
"tags": []
},
"source": [
"### Download file"
"### Download video file"
]
},
{
Expand All @@ -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",
")"
Expand Down Expand Up @@ -256,6 +272,14 @@
"source": [
"object_id"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69b4836e-7054-42a6-8e06-74baaf7cff4f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading

0 comments on commit c84961e

Please sign in to comment.