{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install moviepy\n",
    "# !pip install ffmpeg-python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from moviepy.editor import VideoFileClip\n",
    "import os\n",
    "import ffmpeg\n",
    "# importing module that prerocess the audio file \n",
    "from utils import create_audio_document\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Video Preprocessing Pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "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",
    "    snapshot_dir = os.path.join(os.path.dirname(video_path), f\"{video_name}\")\n",
    "    os.makedirs(snapshot_dir, exist_ok=True)\n",
    "\n",
    "    # Set the interval to 3 minutes (180 seconds)\n",
    "    interval = 30\n",
    "\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",
    "        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",
    "        # Save the snapshot as an image file in the created folder\n",
    "        frame_img = os.path.join(snapshot_dir, f\"frame_at_{interval_str}s.png\")\n",
    "        \n",
    "        # Extract the frame using ffmpeg\n",
    "        (\n",
    "            ffmpeg\n",
    "            .input(video_path, ss=start_time)\n",
    "            .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",
    "    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",
    "    return documents\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "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",
      "Snapshots saved in data\\How to change front wheel bearing on TOYOTA RAV4 II [TUTORIAL AUTODOC].\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]_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",
      "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"
     ]
    }
   ],
   "source": [
    "# Implementing the function\n",
    "documents = preprocess_video_data(video_path, 30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[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='')]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "documents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "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\"}]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "smog_env",
   "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.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
