|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import os\n", |
| 10 | + "def jpeg_compression(scale, src, dst, config):\n", |
| 11 | + "\n", |
| 12 | + " dim = config[\"n_features_per_level\"]\n", |
| 13 | + " n_levels = config[\"n_levels\"]\n", |
| 14 | + "\n", |
| 15 | + " for d in range(dim):\n", |
| 16 | + " for i in range(n_levels):\n", |
| 17 | + " src_path = os.path.join(src, f\"dim{d}\", f\"{str(i).zfill(2)}.png\")\n", |
| 18 | + " save_path = os.path.join(dst, f\"dim{d}\", str(scale).zfill(3))\n", |
| 19 | + " os.makedirs(save_path, exist_ok=True)\n", |
| 20 | + " save_path = os.path.join(save_path, f\"{str(i).zfill(2)}.jpg\")\n", |
| 21 | + "\n", |
| 22 | + " if os.path.isfile(save_path):\n", |
| 23 | + " !rm $save_path\n", |
| 24 | + " !ffmpeg -hide_banner -i $src_path -qscale:v $scale $save_path\n", |
| 25 | + "\n", |
| 26 | + "\n", |
| 27 | + "# keyint=7:min-keyint=7:no-scenecut:me=full:subme=7:bframes=0\n", |
| 28 | + "def hevc_compression(crf, framerate, src, dst, config):\n", |
| 29 | + "\n", |
| 30 | + " dim = config[\"n_features_per_level\"]\n", |
| 31 | + " \n", |
| 32 | + " for d in range(dim):\n", |
| 33 | + " src_path = os.path.join(src, f\"dim{d}\", \"%05d.png\")\n", |
| 34 | + " os.makedirs(os.path.join(dst, f\"dim{d}\"), exist_ok=True)\n", |
| 35 | + " save_path = os.path.join(dst, f\"dim{d}\", f\"{crf}_{framerate}.mp4\")\n", |
| 36 | + "\n", |
| 37 | + " if os.path.isfile(save_path):\n", |
| 38 | + " !rm $save_path\n", |
| 39 | + "\n", |
| 40 | + " !ffmpeg -framerate $framerate -i $src_path -c:v hevc -preset slow -x265-params bframes=0 -crf $crf $save_path\n", |
| 41 | + "\n" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "metadata": {}, |
| 47 | + "source": [ |
| 48 | + "Compress Learnable Keyframes and Sparse Grid" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": null, |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [], |
| 56 | + "source": [ |
| 57 | + "import json\n", |
| 58 | + "datasets = [\"jockey\"]\n", |
| 59 | + "\n", |
| 60 | + "for data in datasets:\n", |
| 61 | + " experiment_name = f\"{data}\"\n", |
| 62 | + " config_path = f\"./logs_nvp/{experiment_name}/config.json\"\n", |
| 63 | + " base_path = f\"./logs_nvp/{experiment_name}/compression\"\n", |
| 64 | + "\n", |
| 65 | + "\n", |
| 66 | + " with open(config_path, 'r') as f:\n", |
| 67 | + " config = json.load(f)\n", |
| 68 | + "\n", |
| 69 | + " config = config[\"nvp\"]\n", |
| 70 | + " keyframe_path = os.path.join(base_path, \"src\", \"keyframes\", \"xy\")\n", |
| 71 | + " save_path = os.path.join(base_path, \"dst\", \"keyframes\", \"xy\")\n", |
| 72 | + " jpeg_compression(scale=2, src=keyframe_path, dst=save_path, config=config[\"2d_encoding_xy\"])\n", |
| 73 | + "\n", |
| 74 | + " keyframe_path = os.path.join(base_path, \"src\", \"keyframes\", \"xt\")\n", |
| 75 | + " save_path = os.path.join(base_path, \"dst\", \"keyframes\", \"xt\")\n", |
| 76 | + " jpeg_compression(scale=3, src=keyframe_path, dst=save_path, config=config[\"2d_encoding_xt\"])\n", |
| 77 | + "\n", |
| 78 | + " keyframe_path = os.path.join(base_path, \"src\", \"keyframes\", \"yt\")\n", |
| 79 | + " save_path = os.path.join(base_path, \"dst\", \"keyframes\", \"yt\")\n", |
| 80 | + " jpeg_compression(scale=3, src=keyframe_path, dst=save_path, config=config[\"2d_encoding_yt\"])\n", |
| 81 | + "\n", |
| 82 | + " sparsegrid_path = os.path.join(base_path, \"src\", \"sparsegrid\")\n", |
| 83 | + " save_path = os.path.join(base_path, \"dst\", \"sparsegrid\")\n", |
| 84 | + " hevc_compression(crf=21, framerate=25, src=sparsegrid_path, dst=save_path, config=config[\"3d_encoding\"])\n", |
| 85 | + " " |
| 86 | + ] |
| 87 | + } |
| 88 | + ], |
| 89 | + "metadata": { |
| 90 | + "interpreter": { |
| 91 | + "hash": "53f0588b3c374ffb8d6102e58c8bd6fde8c17a0cc886162e9d708a7e3ec6b0c9" |
| 92 | + }, |
| 93 | + "kernelspec": { |
| 94 | + "display_name": "Python 3.7.13 ('inr')", |
| 95 | + "language": "python", |
| 96 | + "name": "python3" |
| 97 | + }, |
| 98 | + "language_info": { |
| 99 | + "codemirror_mode": { |
| 100 | + "name": "ipython", |
| 101 | + "version": 3 |
| 102 | + }, |
| 103 | + "file_extension": ".py", |
| 104 | + "mimetype": "text/x-python", |
| 105 | + "name": "python", |
| 106 | + "nbconvert_exporter": "python", |
| 107 | + "pygments_lexer": "ipython3", |
| 108 | + "version": "3.8.8" |
| 109 | + }, |
| 110 | + "orig_nbformat": 4 |
| 111 | + }, |
| 112 | + "nbformat": 4, |
| 113 | + "nbformat_minor": 2 |
| 114 | +} |
0 commit comments