{ "cells": [ { "cell_type": "markdown", "id": "39bb3aca-6d5a-44ed-8419-a9e06e84ef5a", "metadata": {}, "source": [ "# Write video with transparent alpha layer" ] }, { "cell_type": "code", "execution_count": 1, "id": "a52a4e67-fcdc-4ee8-a443-c0478d0e8200", "metadata": {}, "outputs": [], "source": [ "import cutcutcodec" ] }, { "cell_type": "markdown", "id": "e4a077fd-ec62-4b76-81ae-673c7c4762c5", "metadata": {}, "source": [ "## Create a video stream with an alpha layer" ] }, { "cell_type": "code", "execution_count": 2, "id": "2397fde8-b504-4752-b9a6-c7ad6a80008c", "metadata": {}, "outputs": [], "source": [ "signal_rgb = cutcutcodec.generation.GeneratorVideoNoise().apply_video_subclip(0, 10) # 10 seconds of pure noise\n", "signal_rgba = signal_rgb.apply_video_equation(\"r0\", \"g0\", \"b0\", \"0.5\") # alpha layer should be more complex" ] }, { "cell_type": "code", "execution_count": 3, "id": "d6dc513a-445d-4263-842d-fcbfe84ca17f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the frame contains 4 channels\n" ] } ], "source": [ "frame = signal_rgba.out_streams[0].snapshot(0, (480, 720))\n", "print(f\"the frame contains {frame.channels} channels\")" ] }, { "cell_type": "markdown", "id": "dc4cf4d8-8198-4d49-95ef-1b8b2c821939", "metadata": {}, "source": [ "## Find the right codec and pixel format" ] }, { "cell_type": "code", "execution_count": 4, "id": "128c66e6-04fb-41bb-93d5-b05f1e439cda", "metadata": {}, "outputs": [], "source": [ "from cutcutcodec.core.compilation.export.compatibility import Compatibilities" ] }, { "cell_type": "code", "execution_count": 5, "id": "6e85247b-b328-4cce-b1d8-d3541c7fb416", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Testing encoder/muxer: 100%|███████████████████████████| 17325/17325 [00:15<00:00, 1132.37comb/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['apng', 'dpx', 'dxv', 'jpeg2000', 'png', 'qoi', 'rawvideo', 'tiff', 'xwd']\n" ] } ], "source": [ "# for rgba\n", "rgba_comp = Compatibilities().codecs_video(pix_fmt=\"rgba\")\n", "print(sorted(rgba_comp))" ] }, { "cell_type": "code", "execution_count": 6, "id": "ac809cca-abe7-4046-940e-bf0f059c4389", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Testing encoder/muxer: 100%|███████████████████████████| 17325/17325 [00:13<00:00, 1303.77comb/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['ffv1', 'ffvhuff', 'jpeg2000', 'rawvideo']\n" ] } ], "source": [ "# for yuva420p\n", "yuva_comp = Compatibilities().codecs_video(pix_fmt=\"yuva420p\")\n", "print(sorted(yuva_comp))" ] }, { "cell_type": "markdown", "id": "d77ef49f-f1df-43d4-9ff6-bee72df21429", "metadata": {}, "source": [ "## Write the video" ] }, { "cell_type": "code", "execution_count": 7, "id": "2a17514d-06e1-4108-b6c3-b9c43bb22ec8", "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Encoding my_video.mp4: 100%|████████████████████████████████████████| 10.00s/10.00s [03:03<00:00]\n" ] } ], "source": [ "# lossless\n", "streams_settings = [\n", " {\"encodec\": \"jpeg2000\", \"rate\": 30, \"shape\": (480, 720), \"pix_fmt\": \"rgba\"},\n", "]\n", "cutcutcodec.write(signal_rgba.out_streams, \"/tmp/my_video.mp4\", streams_settings=streams_settings)" ] }, { "cell_type": "code", "execution_count": 8, "id": "e0b0de00-2f26-423e-b0eb-75e965eeeb7d", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Encoding my_video.mkv: 100%|████████████████████████████████████████| 10.00s/10.00s [00:17<00:00]\n" ] } ], "source": [ "# lossly\n", "streams_settings = [\n", " {\"encodec\": \"ffv1\", \"rate\": 30, \"shape\": (480, 720), \"pix_fmt\": \"yuva420p\"},\n", "]\n", "cutcutcodec.write(signal_rgba.out_streams, \"/tmp/my_video.mkv\", streams_settings=streams_settings)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.13.2" } }, "nbformat": 4, "nbformat_minor": 5 }