Created
August 1, 2020 20:19
-
-
Save mangtronix/e1cf88b1f990c2f5c578ca8cd276183b to your computer and use it in GitHub Desktop.
Sckit FFmpeg video writing.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "name": "Sckit FFmpeg video writing.ipynb", | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyN8APmEUph0xRgnpxzBgUn8", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/mangtronix/e1cf88b1f990c2f5c578ca8cd276183b/sckit-ffmpeg-video-writing.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "3ZjAc_DFrRnY", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "Test of writing to a video file frame by frame, to avoid out of memory for large videos" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "OAYxtpNCn9Zn", | |
| "colab_type": "code", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 153 | |
| }, | |
| "outputId": "5ef2806c-512e-4ea6-f843-495e13c1b747" | |
| }, | |
| "source": [ | |
| "!apt install ffmpeg" | |
| ], | |
| "execution_count": 1, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "Reading package lists... Done\n", | |
| "Building dependency tree \n", | |
| "Reading state information... Done\n", | |
| "ffmpeg is already the newest version (7:3.4.8-0ubuntu0.2).\n", | |
| "The following package was automatically installed and is no longer required:\n", | |
| " libnvidia-common-440\n", | |
| "Use 'apt autoremove' to remove it.\n", | |
| "0 upgraded, 0 newly installed, 0 to remove and 35 not upgraded.\n" | |
| ], | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "oIPb4pyxn-pj", | |
| "colab_type": "code", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 136 | |
| }, | |
| "outputId": "371440ca-2b69-4726-ac6b-609c5e6a2b29" | |
| }, | |
| "source": [ | |
| "!pip install sk-video" | |
| ], | |
| "execution_count": 3, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "Collecting sk-video\n", | |
| "\u001b[?25l Downloading https://files.pythonhosted.org/packages/dd/3f/ce848b8b2062ad1ccf1449094a740c775f6c761339f411e44f1e090f23a7/sk_video-1.1.10-py2.py3-none-any.whl (2.3MB)\n", | |
| "\u001b[K |████████████████████████████████| 2.3MB 2.8MB/s \n", | |
| "\u001b[?25hRequirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from sk-video) (1.4.1)\n", | |
| "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from sk-video) (1.18.5)\n", | |
| "Installing collected packages: sk-video\n", | |
| "Successfully installed sk-video-1.1.10\n" | |
| ], | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "7aJ6TkOpoQAE", | |
| "colab_type": "code", | |
| "colab": {} | |
| }, | |
| "source": [ | |
| "import skvideo.io\n", | |
| "import numpy as np\n", | |
| "\n", | |
| "outputfilename = \"outputvideo.mp4\"\n", | |
| "\n", | |
| "outputdata = np.random.random(size=(5, 480, 680, 3)) * 255\n", | |
| "outputdata = outputdata.astype(np.uint8)\n", | |
| "\n", | |
| "writer = skvideo.io.FFmpegWriter(outputfilename)\n", | |
| "for i in range(5):\n", | |
| " writer.writeFrame(outputdata[i, :, :, :])\n", | |
| "writer.close()" | |
| ], | |
| "execution_count": 6, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "dehSNfCGoXvF", | |
| "colab_type": "code", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 17 | |
| }, | |
| "outputId": "72062e26-099a-4c37-df02-372a40e4efd6" | |
| }, | |
| "source": [ | |
| "from google.colab import files\n", | |
| "files.download(outputfilename)" | |
| ], | |
| "execution_count": 12, | |
| "outputs": [ | |
| { | |
| "output_type": "display_data", | |
| "data": { | |
| "application/javascript": [ | |
| "\n", | |
| " async function download(id, filename, size) {\n", | |
| " if (!google.colab.kernel.accessAllowed) {\n", | |
| " return;\n", | |
| " }\n", | |
| " const div = document.createElement('div');\n", | |
| " const label = document.createElement('label');\n", | |
| " label.textContent = `Downloading \"${filename}\": `;\n", | |
| " div.appendChild(label);\n", | |
| " const progress = document.createElement('progress');\n", | |
| " progress.max = size;\n", | |
| " div.appendChild(progress);\n", | |
| " document.body.appendChild(div);\n", | |
| "\n", | |
| " const buffers = [];\n", | |
| " let downloaded = 0;\n", | |
| "\n", | |
| " const channel = await google.colab.kernel.comms.open(id);\n", | |
| " // Send a message to notify the kernel that we're ready.\n", | |
| " channel.send({})\n", | |
| "\n", | |
| " for await (const message of channel.messages) {\n", | |
| " // Send a message to notify the kernel that we're ready.\n", | |
| " channel.send({})\n", | |
| " if (message.buffers) {\n", | |
| " for (const buffer of message.buffers) {\n", | |
| " buffers.push(buffer);\n", | |
| " downloaded += buffer.byteLength;\n", | |
| " progress.value = downloaded;\n", | |
| " }\n", | |
| " }\n", | |
| " }\n", | |
| " const blob = new Blob(buffers, {type: 'application/binary'});\n", | |
| " const a = document.createElement('a');\n", | |
| " a.href = window.URL.createObjectURL(blob);\n", | |
| " a.download = filename;\n", | |
| " div.appendChild(a);\n", | |
| " a.click();\n", | |
| " div.remove();\n", | |
| " }\n", | |
| " " | |
| ], | |
| "text/plain": [ | |
| "<IPython.core.display.Javascript object>" | |
| ] | |
| }, | |
| "metadata": { | |
| "tags": [] | |
| } | |
| }, | |
| { | |
| "output_type": "display_data", | |
| "data": { | |
| "application/javascript": [ | |
| "download(\"download_5b43c07c-e436-4247-b38d-ffd3b0a8cb80\", \"frametest_1000.mp4\", 1446464)" | |
| ], | |
| "text/plain": [ | |
| "<IPython.core.display.Javascript object>" | |
| ] | |
| }, | |
| "metadata": { | |
| "tags": [] | |
| } | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "PEHF3796pH6c", | |
| "colab_type": "code", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 425 | |
| }, | |
| "outputId": "102c9bba-5acd-4129-af54-e96c2f651582" | |
| }, | |
| "source": [ | |
| "num_frames = 5 * 60 * 25 # 5 minutes at 25fps\n", | |
| "width = 1280\n", | |
| "height = 720\n", | |
| "longfilename = \"frametest_{}_{}_{}.mp4\".format(width, height, num_frames)\n", | |
| "\n", | |
| "print(\"Writing {} frames to {}\".format(num_frames, longfilename))\n", | |
| "writer = skvideo.io.FFmpegWriter(longfilename)\n", | |
| "for i in range(num_frames):\n", | |
| " if i % 10 == 0:\n", | |
| " print(i)\n", | |
| " outputdata = np.random.random(size=(1, width, height, 3)) * 255\n", | |
| " outputdata = outputdata.astype(np.uint8)\n", | |
| " writer.writeFrame(outputdata[0, :, :, :])\n", | |
| "writer.close()" | |
| ], | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "Writing 7500 frames to frametest_1280_720_7500.mp4\n", | |
| "0\n", | |
| "10\n", | |
| "20\n", | |
| "30\n", | |
| "40\n", | |
| "50\n", | |
| "60\n", | |
| "70\n", | |
| "80\n", | |
| "90\n", | |
| "100\n", | |
| "110\n", | |
| "120\n", | |
| "130\n", | |
| "140\n", | |
| "150\n", | |
| "160\n", | |
| "170\n", | |
| "180\n", | |
| "190\n", | |
| "200\n", | |
| "210\n", | |
| "220\n" | |
| ], | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "ki1A8IYFpagn", | |
| "colab_type": "code", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 17 | |
| }, | |
| "outputId": "e5b99ea1-357e-4b34-9065-f2dc6d1e2a4c" | |
| }, | |
| "source": [ | |
| "files.download(longfilename)" | |
| ], | |
| "execution_count": 19, | |
| "outputs": [ | |
| { | |
| "output_type": "display_data", | |
| "data": { | |
| "application/javascript": [ | |
| "\n", | |
| " async function download(id, filename, size) {\n", | |
| " if (!google.colab.kernel.accessAllowed) {\n", | |
| " return;\n", | |
| " }\n", | |
| " const div = document.createElement('div');\n", | |
| " const label = document.createElement('label');\n", | |
| " label.textContent = `Downloading \"${filename}\": `;\n", | |
| " div.appendChild(label);\n", | |
| " const progress = document.createElement('progress');\n", | |
| " progress.max = size;\n", | |
| " div.appendChild(progress);\n", | |
| " document.body.appendChild(div);\n", | |
| "\n", | |
| " const buffers = [];\n", | |
| " let downloaded = 0;\n", | |
| "\n", | |
| " const channel = await google.colab.kernel.comms.open(id);\n", | |
| " // Send a message to notify the kernel that we're ready.\n", | |
| " channel.send({})\n", | |
| "\n", | |
| " for await (const message of channel.messages) {\n", | |
| " // Send a message to notify the kernel that we're ready.\n", | |
| " channel.send({})\n", | |
| " if (message.buffers) {\n", | |
| " for (const buffer of message.buffers) {\n", | |
| " buffers.push(buffer);\n", | |
| " downloaded += buffer.byteLength;\n", | |
| " progress.value = downloaded;\n", | |
| " }\n", | |
| " }\n", | |
| " }\n", | |
| " const blob = new Blob(buffers, {type: 'application/binary'});\n", | |
| " const a = document.createElement('a');\n", | |
| " a.href = window.URL.createObjectURL(blob);\n", | |
| " a.download = filename;\n", | |
| " div.appendChild(a);\n", | |
| " a.click();\n", | |
| " div.remove();\n", | |
| " }\n", | |
| " " | |
| ], | |
| "text/plain": [ | |
| "<IPython.core.display.Javascript object>" | |
| ] | |
| }, | |
| "metadata": { | |
| "tags": [] | |
| } | |
| }, | |
| { | |
| "output_type": "display_data", | |
| "data": { | |
| "application/javascript": [ | |
| "download(\"download_7d9d89c7-9f69-4266-89ae-67e1983d620a\", \"frametest_1000.mp4\", 77111331)" | |
| ], | |
| "text/plain": [ | |
| "<IPython.core.display.Javascript object>" | |
| ] | |
| }, | |
| "metadata": { | |
| "tags": [] | |
| } | |
| } | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment