{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "26637b56",
   "metadata": {},
   "source": [
    "# Video Handling (Using OpenCV - cv2)\n",
    "\n",
    "This section covers the essential steps for reading, playing, and inspecting video data.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd74af63",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34872dcf",
   "metadata": {},
   "source": [
    "## 1. Reading and Playing a Video\n",
    "\n",
    "The fundamental tool for video in OpenCV is the cv2.VideoCapture object.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "013f73ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "def play_video(video_path):\n",
    "    # 1. Open the video file\n",
    "    cap = cv2.VideoCapture(video_path)\n",
    "\n",
    "    if not cap.isOpened():\n",
    "        print(\"Error: Could not open video file.\")\n",
    "        return\n",
    "\n",
    "    # 2. Loop through frames\n",
    "    while cap.isOpened():\n",
    "        # Read the next frame\n",
    "        ret, frame = cap.read()\n",
    "\n",
    "        # 'ret' (return value) is True if a frame was read successfully\n",
    "        if ret:\n",
    "            # Display the frame\n",
    "            cv2.imshow(\"Video Playback\", frame)\n",
    "\n",
    "            # Wait for 25ms (or less for faster playback).\n",
    "            # The 'q' key stops the loop.\n",
    "            if cv2.waitKey(25) & 0xFF == ord(\"q\"):\n",
    "                break\n",
    "        else:\n",
    "            # End of video stream\n",
    "            break\n",
    "\n",
    "    # 3. Release the capture object and close all windows\n",
    "    cap.release()\n",
    "    cv2.destroyAllWindows()\n",
    "\n",
    "\n",
    "play_video(\"path/to/your/video.mp4\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6f8b521",
   "metadata": {},
   "source": [
    "## 2. Inspecting Video Metadata and Data\n",
    "\n",
    "You can use the .get() method to retrieve key video properties and inspect the frame data itself.\n",
    "\n",
    "| Property         | OpenCV Code                        | Description                |\n",
    "| ---------------- | ---------------------------------- | -------------------------- |\n",
    "| Frame Width      | cap.get(cv2.CAP_PROP_FRAME_WIDTH)  | Number of columns (pixels) |\n",
    "| Frame Height     | cap.get(cv2.CAP_PROP_FRAME_HEIGHT) | Number of rows (pixels)    |\n",
    "| Frame Rate (FPS) | cap.get(cv2.CAP_PROP_FPS)          | Frames per second          |\n",
    "| Frame Count      | cap.get(cv2.CAP_PROP_FRAME_COUNT)  | Total number of frames     |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "825279a1",
   "metadata": {},
   "source": [
    "## Data Inspection:\n",
    "\n",
    "When you read a frame using ret, frame = cap.read(), the frame variable is a NumPy array.\n",
    "\n",
    "- Representation: A video frame is just an image! It's a 3D NumPy array of shape (Height, Width, 3) (for BGR color).\n",
    "- Accessing Data: You can treat it like any image in your image processing labs (e.g., frame[y, x] gives you the BGR color of a specific pixel).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b55c922",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
