1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "UvTPRErEO2cl"
},
"source": [
"##### Copyright 2025 Google LLC."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "LCJL7_hQO3jW"
},
"outputs": [],
"source": [
"# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2Xjw-LFKoD1-"
},
"source": [
"# Gemini API: Analyze a Video - Historic Event Recognition\n",
"\n",
"This notebook shows how you can use Gemini models' multimodal capabilities to recognize which historic event is happening in the video."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dJY5C59gPf-m"
},
"source": [
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Analyze_a_Video_Historic_Event_Recognition.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" height=30/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "NwP4PBGWoQiJ"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[?25l \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m0.0/137.7 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r",
"\u001b[2K \u001b[91mββββββββββββββββββββββββββββββββββββββ\u001b[0m\u001b[91mβΈ\u001b[0m\u001b[90mβ\u001b[0m \u001b[32m133.1/137.7 kB\u001b[0m \u001b[31m5.4 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m137.7/137.7 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h"
]
}
],
"source": [
"%pip install -U -q \"google-genai>=1.0.0\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KsMCUn_nRRtV"
},
"source": [
"## Configure your API key\n",
"\n",
"To run the following cell, your API key must be stored in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) for an example."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "yGjQlQ9hoSOk"
},
"outputs": [],
"source": [
"from google import genai\n",
"from google.colab import userdata\n",
"\n",
"API_KEY = userdata.get('GOOGLE_API_KEY')\n",
"client = genai.Client(api_key=API_KEY)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9y6o91Xb7RZb"
},
"source": [
"## Example\n",
"\n",
"This example uses [video of President Ronald Reagan's Speech at the Berlin Wall](https://s3.amazonaws.com/NARAprodstorage/opastorage/live/16/147/6014716/content/presidential-libraries/reagan/5730544/6-12-1987-439.mp4) taken on June 12 1987."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "M794IEOG7QzI"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--2025-03-04 14:04:54-- https://s3.amazonaws.com/NARAprodstorage/opastorage/live/16/147/6014716/content/presidential-libraries/reagan/5730544/6-12-1987-439.mp4\n",
"Resolving s3.amazonaws.com (s3.amazonaws.com)... 52.217.89.118, 52.217.64.198, 52.216.106.21, ...\n",
"Connecting to s3.amazonaws.com (s3.amazonaws.com)|52.217.89.118|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 628645171 (600M) [video/mp4]\n",
"Saving to: βberlin.mp4β\n",
"\n",
"berlin.mp4 100%[===================>] 599.52M 41.0MB/s in 12s \n",
"\n",
"2025-03-04 14:05:07 (48.2 MB/s) - βberlin.mp4β saved [628645171/628645171]\n",
"\n"
]
}
],
"source": [
"# Download video\n",
"path = \"berlin.mp4\"\n",
"url = \"https://s3.amazonaws.com/NARAprodstorage/opastorage/live/16/147/6014716/content/presidential-libraries/reagan/5730544/6-12-1987-439.mp4\"\n",
"!wget $url -O $path"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "efnp0oAq7kYx"
},
"outputs": [],
"source": [
"# Upload video\n",
"video_file = client.files.upload(file=path)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "ij9-oG3g7yyj"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
".............."
]
}
],
"source": [
"import time\n",
"# Wait until the uploaded video is available\n",
"while video_file.state.name == \"PROCESSING\":\n",
" print('.', end='')\n",
" time.sleep(5)\n",
" video_file = client.files.get(name=video_file.name)\n",
"\n",
"if video_file.state.name == \"FAILED\":\n",
" raise ValueError(video_file.state.name)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CFG1q42HTHmp"
},
"source": [
"The uploaded video is ready for processing. This prompt instructs the model to provide basic information about the historical events portrayed in the video."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "Zhnsiy9O8OXG"
},
"outputs": [],
"source": [
"system_prompt = \"\"\"\n",
" You are historian who specializes in events caught on film.\n",
" When you receive a video answer following questions:\n",
" When did it happen?\n",
" Who is the most important person in video?\n",
" How the event is called?\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mvbM5XRJjp6T"
},
"source": [
"Some historic events touch on controversial topics that may get flagged by Gemini API, which blocks the response for the query.\n",
"\n",
"Because of this, it might be a good idea to turn off safety settings."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "GroygcLLNH3J"
},
"outputs": [],
"source": [
"safety_settings = [\n",
" {\n",
" \"category\": \"HARM_CATEGORY_HARASSMENT\",\n",
" \"threshold\": \"BLOCK_NONE\",\n",
" },\n",
" {\n",
" \"category\": \"HARM_CATEGORY_HATE_SPEECH\",\n",
" \"threshold\": \"BLOCK_NONE\",\n",
" },\n",
" {\n",
" \"category\": \"HARM_CATEGORY_SEXUALLY_EXPLICIT\",\n",
" \"threshold\": \"BLOCK_NONE\",\n",
" },\n",
" {\n",
" \"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\",\n",
" \"threshold\": \"BLOCK_NONE\",\n",
" },\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "PWLK7TEi7_mP"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Certainly! Here's the information about the video:\n",
"\n",
"Based on the video, here are the answers to your questions:\n",
"\n",
"- **When did it happen?** This event happened on June 12, 1987.\n",
"- **Who is the most important person in the video?** The most important person in the video is Ronald Reagan, who was the President of the United States at the time.\n",
"- **How is the event called?** The event is commonly referred to as President Reagan's \"Tear Down This Wall\" speech.\n"
]
}
],
"source": [
"from google.genai import types\n",
"\n",
"MODEL_ID = \"gemini-3-flash-preview\" # @param [\"gemini-2.5-flash-lite\", \"gemini-2.5-flash\", \"gemini-2.5-pro\", \"gemini-2.5-flash-preview\", \"gemini-3-pro-preview\"] {\"allow-input\":true, isTemplate: true}\n",
"response = client.models.generate_content(\n",
" model=f\"models/{MODEL_ID}\",\n",
" contents=[\n",
" \"Analyze the video please\",\n",
" video_file\n",
" ],\n",
" config=types.GenerateContentConfig(\n",
" system_instruction=system_prompt,\n",
" safety_settings=safety_settings,\n",
" ),\n",
" )\n",
"print(response.text)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T6ByVwbmTfve"
},
"source": [
"As you can see, the model correctly provided information about the dates, Ronald Reagan, who was the main subject of the video, and the name of this event.\n",
"\n",
"You can delete the video to prevent unnecessary data storage."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "SomiSVmu8Czk"
},
"outputs": [
{
"data": {
"text/plain": [
"DeleteFileResponse()"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Delete video\n",
"client.files.delete(name=video_file.name)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aBM_LtQLLJA9"
},
"source": [
"## Summary\n",
"\n",
"Now you know how you can prompt Gemini models with videos and use them to recognize historic events.\n",
"\n",
"This notebook shows only one of many use cases. Check the [Video understanding](../quickstarts/Video_understanding.ipynb) notebook for more examples of using the Gemini API with videos."
]
}
],
"metadata": {
"colab": {
"name": "Analyze_a_Video_Historic_Event_Recognition.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}