Skip to content

Commit

Permalink
Add colab nb
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikWin authored Oct 22, 2024
1 parent 700c1c1 commit 83d8416
Showing 1 changed file with 145 additions and 0 deletions.
145 changes: 145 additions & 0 deletions misc/Colab_Vidformer.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "N1tb10v9WSZJ"
},
"outputs": [],
"source": [
"!wget https://github.com/ixlab/vidformer/releases/download/v0.6.1/vidformer-cli-ubuntu22.04-amd64\n",
"!mv vidformer-cli-ubuntu22.04-amd64 /usr/local/bin/vidformer-cli\n",
"!chmod +x /usr/local/bin/vidformer-cli\n",
"!apt update && apt upgrade -y && apt install -y libopencv-dev libfdk-aac-dev\n",
"!pip3 install vidformer==0.6.1 --upgrade"
]
},
{
"cell_type": "markdown",
"source": [
"## [Vidformer](https://github.com/ixlab/vidformer) colab demo"
],
"metadata": {
"id": "_M0bd0brLXw2"
}
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "FYQvFyR7WwJ5"
}
},
{
"cell_type": "code",
"source": [
"import vidformer as vf\n",
"from fractions import Fraction\n",
"\n",
"server = vf.YrdenServer()\n",
"tos = vf.Source(server, \"tos_720p\", \"https://f.dominik.win/data/dve2/tos_720p.mp4\", stream=0)\n",
"\n",
"print(tos.fmt())\n",
"\n",
"domain = [Fraction(i, 24) for i in range(24 * 30)]\n",
"def render(t: Fraction, i: int):\n",
" clip_start_point = Fraction(5 * 60, 1) # start at 5 * 60 seconds\n",
" return tos[t + clip_start_point]\n",
"\n",
"spec = vf.Spec(domain, render, tos.fmt())\n",
"spec.save(server, \"my-clip.mp4\")"
],
"metadata": {
"id": "JyRdCYYPWsel"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from IPython.display import Video\n",
"Video(\"my-clip.mp4\", embed=True)"
],
"metadata": {
"id": "ZWgu_US1YC_C"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Or you can use the cv2 frontend; just change `import cv2` to `import vidformer.cv2 as cv2`:"
],
"metadata": {
"id": "xfi0dYKSMsp-"
}
},
{
"cell_type": "code",
"source": [
"import cv2\n",
"# import vidformer.cv2 as cv2 # Use me instead!\n",
"\n",
"video_url = \"https://f.dominik.win/data/dve2/tos_720p.mp4\"\n",
"cap = cv2.VideoCapture(video_url)\n",
"assert cap.isOpened()\n",
"\n",
"start_time = 5 * 60\n",
"clip_duration = 5\n",
"fps = cap.get(cv2.CAP_PROP_FPS)\n",
"frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n",
"frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n",
"\n",
"start_frame = int(start_time * fps)\n",
"total_frames = int(clip_duration * fps)\n",
"\n",
"cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)\n",
"\n",
"fourcc = cv2.VideoWriter_fourcc(*'mp4v')\n",
"out = cv2.VideoWriter('output_clip.mp4', fourcc, fps, (frame_width, frame_height))\n",
"\n",
"for _ in range(total_frames):\n",
" ret, frame = cap.read()\n",
" if not ret:\n",
" print(\"Error: Could not read frame.\")\n",
" break\n",
" out.write(frame)\n",
"\n",
"cap.release()\n",
"out.release()\n",
"print(\"Clip saved as 'output_clip.mp4'.\")"
],
"metadata": {
"id": "LiNdbdD-MrMf"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Want to go further? See https://ixlab.github.io/vidformer/getting-started.html"
],
"metadata": {
"id": "uJaEbrHPLS2w"
}
}
]
}

0 comments on commit 83d8416

Please sign in to comment.