|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 116, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "from ipywidgets import interact\n", |
| 10 | + "from matplotlib import pyplot as plt\n", |
| 11 | + "import ffmpeg\n", |
| 12 | + "import ipywidgets as widgets\n", |
| 13 | + "import numpy as np" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": 117, |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "probe = ffmpeg.probe('in.mp4')\n", |
| 23 | + "video_info = next(stream for stream in probe['streams'] if stream['codec_type'] == 'video')\n", |
| 24 | + "width = int(video_info['width'])\n", |
| 25 | + "height = int(video_info['height'])\n", |
| 26 | + "num_frames = int(video_info['nb_frames'])" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 118, |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [], |
| 34 | + "source": [ |
| 35 | + "out, err = (\n", |
| 36 | + " ffmpeg\n", |
| 37 | + " .input('in.mp4')\n", |
| 38 | + " .output('pipe:', format='rawvideo', pix_fmt='rgb24')\n", |
| 39 | + " .run(capture_stdout=True)\n", |
| 40 | + ")\n", |
| 41 | + "video = (\n", |
| 42 | + " np\n", |
| 43 | + " .frombuffer(out, np.uint8)\n", |
| 44 | + " .reshape([-1, height, width, 3])\n", |
| 45 | + ")" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": 115, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [ |
| 53 | + { |
| 54 | + "data": { |
| 55 | + "application/vnd.jupyter.widget-view+json": { |
| 56 | + "model_id": "17d13d7551114fb39a1fad933cf0398a", |
| 57 | + "version_major": 2, |
| 58 | + "version_minor": 0 |
| 59 | + }, |
| 60 | + "text/plain": [ |
| 61 | + "interactive(children=(IntSlider(value=0, description='frame', max=209), Output()), _dom_classes=('widget-inter…" |
| 62 | + ] |
| 63 | + }, |
| 64 | + "metadata": {}, |
| 65 | + "output_type": "display_data" |
| 66 | + } |
| 67 | + ], |
| 68 | + "source": [ |
| 69 | + "@interact(frame=(0, num_frames))\n", |
| 70 | + "def show_frame(frame=0):\n", |
| 71 | + " plt.imshow(video[frame,:,:,:])" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "code", |
| 76 | + "execution_count": null, |
| 77 | + "metadata": {}, |
| 78 | + "outputs"
C2E4
: [], |
| 79 | + "source": [] |
| 80 | + } |
| 81 | + ], |
| 82 | + "metadata": { |
| 83 | + "kernelspec": { |
| 84 | + "display_name": "Python 3", |
| 85 | + "language": "python", |
| 86 | + "name": "python3" |
| 87 | + }, |
| 88 | + "language_info": { |
| 89 | + "codemirror_mode": { |
| 90 | + "name": "ipython", |
| 91 | + "version": 3 |
| 92 | + }, |
| 93 | + "file_extension": ".py", |
| 94 | + "mimetype": "text/x-python", |
| 95 | + "name": "python", |
| 96 | + "nbconvert_exporter": "python", |
| 97 | + "pygments_lexer": "ipython3", |
| 98 | + "version": "3.6.4" |
| 99 | + } |
| 100 | + }, |
| 101 | + "nbformat": 4, |
| 102 | + "nbformat_minor": 2 |
| 103 | +} |
0 commit comments