Files
ds_fire_fighter/video_experiment.ipynb
T

236 lines
16 KiB
Plaintext
Raw Normal View History

2024-08-08 14:58:44 +01:00
{
"cells": [
{
"cell_type": "code",
2024-08-15 21:18:38 +01:00
"execution_count": 2,
2024-08-08 14:58:44 +01:00
"metadata": {},
"outputs": [],
2024-08-12 22:33:39 +01:00
"source": [
"# !pip install moviepy\n",
"# !pip install ffmpeg-python"
]
2024-08-08 14:58:44 +01:00
},
{
"cell_type": "code",
2024-08-15 21:18:38 +01:00
"execution_count": 3,
2024-08-08 14:58:44 +01:00
"metadata": {},
"outputs": [],
2024-08-12 22:33:39 +01:00
"source": [
"from moviepy.editor import VideoFileClip\n",
"import os\n",
"import ffmpeg\n",
"# importing module that prerocess the audio file \n",
2024-08-14 23:09:10 +01:00
"from utils import create_audio_document\n"
2024-08-12 22:33:39 +01:00
]
2024-08-08 14:58:44 +01:00
},
{
2024-08-12 22:33:39 +01:00
"cell_type": "markdown",
2024-08-08 14:58:44 +01:00
"metadata": {},
2024-08-12 22:33:39 +01:00
"source": [
"## Video Preprocessing Pipeline"
]
2024-08-08 14:58:44 +01:00
},
{
"cell_type": "code",
2024-08-15 21:18:38 +01:00
"execution_count": 11,
2024-08-08 14:58:44 +01:00
"metadata": {},
"outputs": [],
2024-08-12 22:33:39 +01:00
"source": [
"video_path = 'data/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC].mp4'\n",
"\n",
"\n",
"\n",
"# creating a function to preprocess the videos\n",
"def preprocess_video_data(video_path: str, time_interval: int):\n",
" \n",
" # Load the video file\n",
" video = VideoFileClip(video_path)\n",
" \n",
" # Get the duration of the video\n",
" duration = video.duration\n",
" \n",
" # create an audio version of the video\n",
" audio_path = video_path.replace('.mp4', '.mp3')\n",
" _ = video.audio.write_audiofile(audio_path)\n",
" \n",
" # creating a snapshot of the videos at the time interval\n",
" # Extract the video filename without extension\n",
" video_name = os.path.splitext(os.path.basename(video_path))[0]\n",
"\n",
" # Create a directory for snapshots using the video name\n",
2024-08-15 21:18:38 +01:00
" snapshot_dir = os.path.join(os.path.dirname(video_path), f\"{video_name}\")\n",
2024-08-12 22:33:39 +01:00
" os.makedirs(snapshot_dir, exist_ok=True)\n",
"\n",
" # Set the interval to 3 minutes (180 seconds)\n",
2024-08-14 23:09:10 +01:00
" interval = 30\n",
2024-08-12 22:33:39 +01:00
"\n",
" # Get the duration of the video using ffmpeg\n",
" probe = ffmpeg.probe(video_path)\n",
" duration = float(probe['format']['duration'])\n",
"\n",
" # Loop through the video and take snapshots at 0s, 3min, 6min, etc.\n",
" for i in range(0, int(duration), interval):\n",
2024-08-15 21:18:38 +01:00
" start_time = i\n",
" end_time = min(i + interval, int(duration))\n",
" \n",
" # Format the interval as 'start-end'\n",
" interval_str = f\"{start_time}-{end_time}\"\n",
" \n",
2024-08-12 22:33:39 +01:00
" # Save the snapshot as an image file in the created folder\n",
2024-08-15 21:18:38 +01:00
" frame_img = os.path.join(snapshot_dir, f\"frame_at_{interval_str}s.png\")\n",
2024-08-12 22:33:39 +01:00
" \n",
" # Extract the frame using ffmpeg\n",
" (\n",
" ffmpeg\n",
2024-08-15 21:18:38 +01:00
" .input(video_path, ss=start_time)\n",
2024-08-12 22:33:39 +01:00
" .output(frame_img, vframes=1)\n",
" .run()\n",
" )\n",
"\n",
" print(f\"Snapshots saved in {snapshot_dir}.\")\n",
" \n",
" \n",
" # now creating document from the audio file\n",
2024-08-15 21:18:38 +01:00
" documents = create_audio_document(audio_path, file_type='video', chunk_duration_minutes=0.5)\n",
" \n",
" # deleting the audio file\n",
" os.remove(audio_path)\n",
2024-08-12 22:33:39 +01:00
" return documents\n",
"\n"
]
2024-08-08 14:58:44 +01:00
},
{
"cell_type": "code",
2024-08-15 21:18:38 +01:00
"execution_count": 12,
2024-08-08 14:58:44 +01:00
"metadata": {},
2024-08-12 22:33:39 +01:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MoviePy - Writing audio in data/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC].mp3\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" \r"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"MoviePy - Done.\n",
2024-08-15 21:18:38 +01:00
"Snapshots saved in data\\How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC].\n",
2024-08-12 22:33:39 +01:00
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk1.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk2.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk3.mp3\n",
2024-08-15 21:18:38 +01:00
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk4.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk5.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk6.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk7.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk8.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk9.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk10.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk11.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk12.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk13.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk14.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk15.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk16.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk17.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk18.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk19.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk20.mp3\n",
"Exporting How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunks/How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]_chunk21.mp3\n"
2024-08-12 22:33:39 +01:00
]
}
],
"source": [
"# Implementing the function\n",
2024-08-15 21:18:38 +01:00
"documents = preprocess_video_data(video_path, 30)"
2024-08-12 22:33:39 +01:00
]
2024-08-08 14:58:44 +01:00
},
{
"cell_type": "code",
2024-08-15 21:18:38 +01:00
"execution_count": null,
2024-08-08 14:58:44 +01:00
"metadata": {},
2024-08-12 22:33:39 +01:00
"outputs": [
{
"data": {
"text/plain": [
2024-08-15 21:18:38 +01:00
"[Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '0.0-0.5 minutes', 'file_type': 'video'}, page_content=\" Hi everybody! Here is the latest installment of AutoDoc's video tutorials on replacing car parts.\"),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '0.5-1.0 minutes', 'file_type': 'video'}, page_content=''),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '1.0-1.5 minutes', 'file_type': 'video'}, page_content=\" I'm going to use a plastic to prevent the air from coming out. I'm going to use a plastic to prevent the air from coming out. I'm going to use a plastic to prevent the air from coming out. I'm going to use a plastic to prevent the air from coming out. I'm going to use a plastic to prevent the air from coming out. I'm going to use a plastic to prevent the air from coming out. I'm going to use a plastic to prevent the air from coming out. I'm going to use a plastic to prevent the air from coming out. I'm going to use a plastic to prevent the air from coming out. I'm going to use a plastic to prevent the air from coming out.\"),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '1.5-2.0 minutes', 'file_type': 'video'}, page_content=' The week.'),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '2.0-2.5 minutes', 'file_type': 'video'}, page_content=''),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '2.5-3.0 minutes', 'file_type': 'video'}, page_content=''),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '3.0-3.5 minutes', 'file_type': 'video'}, page_content=' 15. Install the new Thanks for watching!'),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '3.5-4.0 minutes', 'file_type': 'video'}, page_content=\" I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. you\"),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '4.0-4.5 minutes', 'file_type': 'video'}, page_content=''),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '4.5-5.0 minutes', 'file_type': 'video'}, page_content=' You can buy spare parts from us on our website or in the Autodoc app.'),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '5.0-5.5 minutes', 'file_type': 'video'}, page_content=\" I'm going to make a fire with a fire extinguisher. I'll make a fire with a fire extinguisher. I'll make a fire with a fire extinguisher. I'll make a fire with a fire extinguisher. I'll make a fire with a fire extinguisher.\"),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '5.5-6.0 minutes', 'file_type': 'video'}, page_content=''),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '6.0-6.5 minutes', 'file_type': 'video'}, page_content=' Are you interested in this product? All links can be found in the description.'),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '6.5-7.0 minutes', 'file_type': 'video'}, page_content=\" I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. I'm going to make a fire with a fire-breathing gun. Thanks for watching!\"),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '7.0-7.5 minutes', 'file_type': 'video'}, page_content=\" I'm going to make a fire with a fire extinguisher. I'm going to make a fire with a fire extinguisher. I'm going to make a fire with a fire extinguisher. I'm going to make a fire with a fire extinguisher. I'm going to make a fire with a fire extinguisher.\"),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '7.5-8.0 minutes', 'file_type': 'video'}, page_content=' The Share this video if you found it interesting.'),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '8.0-8.5 minutes', 'file_type': 'video'}, page_content=''),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '8.5-9.0 minutes', 'file_type': 'video'}, page_content=''),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '9.0-9.5 minutes', 'file_type': 'video'}, page_content=\" I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one. I'm going to make a new one.\"),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '9.5-10.0 minutes', 'file_type': 'video'}, page_content=' Thank you for watching our video tutorials. If you enjoyed watching, click thumbs up and share it with your friends. Have a nice day! Follow us on social media. Find us on Instagram and TikTok.'),\n",
" Document(metadata={'filename': 'How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC]', 'duration': '10.0-10.5 minutes', 'file_type': 'video'}, page_content='')]"
2024-08-12 22:33:39 +01:00
]
},
2024-08-15 21:18:38 +01:00
"execution_count": 10,
2024-08-12 22:33:39 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"documents"
]
2024-08-08 14:58:44 +01:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
2024-08-14 23:09:10 +01:00
"source": [
"# [{filename: \"car repair.pdf\", pages: [12, 55, 356], description: \"lorem ipsum\", filetype:\"pdf\", thumbnail:\"carrepair.jpg\"}\n",
"# ,{filename: \"how to repair car.mp4\", pages: [12, 55, 356], description: \"lorem ipsum\", filetype:\"video\", thumbnail:\"how to repair car.jpg\"}]"
]
2024-08-08 14:58:44 +01:00
}
],
"metadata": {
2024-08-12 22:33:39 +01:00
"kernelspec": {
"display_name": "smog_env",
"language": "python",
"name": "python3"
},
2024-08-08 14:58:44 +01:00
"language_info": {
2024-08-12 22:33:39 +01:00
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
2024-08-08 14:58:44 +01:00
}
},
"nbformat": 4,
"nbformat_minor": 2
}