[
  {
    "path": ".github/FUNDING.yml",
    "content": "github: yuvraj108c\ncustom: [\"https://paypal.me/yuvraj108c\", \"https://buymeacoffee.com/yuvraj108cZ\"]\n"
  },
  {
    "path": "4k_Video_Upscaler_Colab_(Real_ESRGAN).ipynb",
    "content": "{\n  \"nbformat\": 4,\n  \"nbformat_minor\": 0,\n  \"metadata\": {\n    \"colab\": {\n      \"provenance\": [],\n      \"gpuType\": \"T4\",\n      \"authorship_tag\": \"ABX9TyPUAre5beb+ZXuBaeRDGgx7\",\n      \"include_colab_link\": true\n    },\n    \"kernelspec\": {\n      \"name\": \"python3\",\n      \"display_name\": \"Python 3\"\n    },\n    \"language_info\": {\n      \"name\": \"python\"\n    },\n    \"accelerator\": \"GPU\"\n  },\n  \"cells\": [\n    {\n      \"cell_type\": \"markdown\",\n      \"metadata\": {\n        \"id\": \"view-in-github\",\n        \"colab_type\": \"text\"\n      },\n      \"source\": [\n        \"<a href=\\\"https://colab.research.google.com/github/yuvraj108c/4k-video-upscaler-colab/blob/main/4k_Video_Upscaler_Colab_(Real_ESRGAN).ipynb\\\" target=\\\"_parent\\\"><img src=\\\"https://colab.research.google.com/assets/colab-badge.svg\\\" alt=\\\"Open In Colab\\\"/></a>\"\n      ]\n    },\n    {\n      \"cell_type\": \"markdown\",\n      \"source\": [\n        \"# 4k Video Upscaler Colab (Real-ESRGAN)\\n\",\n        \"\\n\",\n        \"Adapted from: [Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN)\\n\",\n        \"\\n\",\n        \"Made with ❤️ by: [yuvraj108c](https://github.com/yuvraj108c)\\n\",\n        \"\\n\",\n        \"Github repository: https://github.com/yuvraj108c/4k-video-upscaler-colab\"\n      ],\n      \"metadata\": {\n        \"id\": \"zEksijsOSulF\"\n      }\n    },\n    {\n      \"cell_type\": \"markdown\",\n      \"source\": [\n        \"# 1. Setup (~1 minute)\"\n      ],\n      \"metadata\": {\n        \"id\": \"Jt28DQ93QK5Z\"\n      }\n    },\n    {\n      \"cell_type\": \"code\",\n      \"source\": [\n        \"import torch\\n\",\n        \"assert torch.cuda.is_available(), \\\"GPU not detected.. Please change runtime to GPU\\\"\\n\",\n        \"\\n\",\n        \"from PIL import Image\\n\",\n        \"import cv2, os, subprocess\\n\",\n        \"from tqdm import tqdm\\n\",\n        \"\\n\",\n        \"!git clone https://github.com/xinntao/Real-ESRGAN.git\\n\",\n        \"%cd Real-ESRGAN\\n\",\n        \"\\n\",\n        \"!pip install -q torch==2.0.1 torchvision==0.15.2 --extra-index-url https://download.pytorch.org/whl/cu118\\n\",\n        \"!pip install -q basicsr facexlib gfpgan ffmpeg ffmpeg-python\\n\",\n        \"!pip install -q -r requirements.txt\\n\",\n        \"!python setup.py develop\\n\",\n        \"\\n\",\n        \"!pip install \\\"numpy<2\\\"\\n\",\n        \"mount_drive = False\"\n      ],\n      \"metadata\": {\n        \"id\": \"kzcD1CMyQtbv\"\n      },\n      \"execution_count\": null,\n      \"outputs\": []\n    },\n    {\n      \"cell_type\": \"markdown\",\n      \"source\": [\n        \"\\n\",\n        \"# 2. Mount drive (optional)\"\n      ],\n      \"metadata\": {\n        \"id\": \"kRdP_e_rdWrr\"\n      }\n    },\n    {\n      \"cell_type\": \"code\",\n      \"source\": [\n        \"from google.colab import drive\\n\",\n        \"mount_drive=False #@param{type:\\\"boolean\\\"}\\n\",\n        \"\\n\",\n        \"if mount_drive:\\n\",\n        \"  drive.mount('/content/gdrive/')\"\n      ],\n      \"metadata\": {\n        \"cellView\": \"form\",\n        \"id\": \"vdYKV-kKdKNf\"\n      },\n      \"execution_count\": 2,\n      \"outputs\": []\n    },\n    {\n      \"cell_type\": \"markdown\",\n      \"source\": [\n        \"# 3. Upscale video\\n\",\n        \"\\n\",\n        \"- The upscaled video will be saved to `output_dir`\\n\",\n        \"- If google drive is mounted, it will be also saved at `MyDrive/Upscaled Videos (REAL-ESRGAN)`\\n\"\n      ],\n      \"metadata\": {\n        \"id\": \"mZZPG007SB4A\"\n      }\n    },\n    {\n      \"cell_type\": \"code\",\n      \"execution_count\": null,\n      \"metadata\": {\n        \"cellView\": \"form\",\n        \"id\": \"NX2v5PH9O7Sw\"\n      },\n      \"outputs\": [],\n      \"source\": [\n        \"video_path=\\\"/content/video.mp4\\\" #@param{type:\\\"string\\\"}\\n\",\n        \"output_dir=\\\"/content/\\\" #@param{type:\\\"string\\\"}\\n\",\n        \"resolution = \\\"FHD (1920 x 1080)\\\" # @param [\\\"FHD (1920 x 1080)\\\", \\\"2k (2560 x 1440)\\\", \\\"4k (3840 x 2160)\\\",\\\"2 x original\\\", \\\"3 x original\\\", \\\"4 x original\\\"] {type:\\\"string\\\"}\\n\",\n        \"model = \\\"RealESRGAN_x4plus\\\" #@param [\\\"RealESRGAN_x4plus\\\" , \\\"RealESRGAN_x4plus_anime_6B\\\", \\\"realesr-animevideov3\\\"]\\n\",\n        \"\\n\",\n        \"assert os.path.exists(video_path), \\\"Video file does not exist\\\"\\n\",\n        \"\\n\",\n        \"video_capture = cv2.VideoCapture(video_path)\\n\",\n        \"video_width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))\\n\",\n        \"video_height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))\\n\",\n        \"\\n\",\n        \"final_width = None\\n\",\n        \"final_height = None\\n\",\n        \"aspect_ratio = float(video_width/video_height)\\n\",\n        \"\\n\",\n        \"# Get output resolutions\\n\",\n        \"match resolution:\\n\",\n        \"  case \\\"FHD (1920 x 1080)\\\":\\n\",\n        \"    final_width=1920\\n\",\n        \"    final_height=1080\\n\",\n        \"  case \\\"2k (2560 x 1440)\\\":\\n\",\n        \"    final_width=2560\\n\",\n        \"    final_height=1440\\n\",\n        \"  case \\\"4k (3840 x 2160)\\\":\\n\",\n        \"    final_width=3840\\n\",\n        \"    final_height=2160\\n\",\n        \"  case \\\"2 x original\\\":\\n\",\n        \"    final_width=2*video_width\\n\",\n        \"    final_height=2*video_height\\n\",\n        \"  case \\\"3 x original\\\":\\n\",\n        \"    final_width=3*video_width\\n\",\n        \"    final_height=3*video_height\\n\",\n        \"  case \\\"4 x original\\\":\\n\",\n        \"    final_width=4*video_width\\n\",\n        \"    final_height=4*video_height\\n\",\n        \"\\n\",\n        \"if aspect_ratio == 1.0 and \\\"original\\\" not in resolution:\\n\",\n        \"  final_height = final_width\\n\",\n        \"\\n\",\n        \"if aspect_ratio < 1.0 and \\\"original\\\" not in resolution:\\n\",\n        \"  temp = final_width\\n\",\n        \"  final_width = final_height\\n\",\n        \"  final_height = temp\\n\",\n        \"\\n\",\n        \"scale_factor = max(final_width/video_width, final_height/video_height)\\n\",\n        \"isEven = int(video_width * scale_factor) % 2 == 0 and int(video_height * scale_factor) % 2 == 0\\n\",\n        \"\\n\",\n        \"# scale_factor needs to be even\\n\",\n        \"while isEven == False:\\n\",\n        \"  scale_factor += 0.01\\n\",\n        \"  isEven = int(video_width * scale_factor) % 2 == 0 and int(video_height * scale_factor) % 2 == 0\\n\",\n        \"\\n\",\n        \"print(f\\\"Upscaling from {video_width}x{video_height} to {final_width}x{final_height}, scale_factor={scale_factor}\\\")\\n\",\n        \"\\n\",\n        \"!python inference_realesrgan_video.py -n {model} -i '{video_path}' -o '{output_dir}' --outscale {scale_factor}\\n\",\n        \"\\n\",\n        \"video_name_with_ext = os.path.basename(video_path)\\n\",\n        \"video_name = video_name_with_ext.replace(\\\".mp4\\\",\\\"\\\")\\n\",\n        \"upscaled_video_path = f\\\"{output_dir}{video_name}_out.mp4\\\"\\n\",\n        \"final_video_name = f\\\"{video_name}_upscaled_{final_width}_{final_height}.mp4\\\"\\n\",\n        \"final_video_path = os.path.join(output_dir, final_video_name)\\n\",\n        \"\\n\",\n        \"# crop to fit\\n\",\n        \"if \\\"original\\\" not in resolution:\\n\",\n        \"  print(\\\"Cropping to fit...\\\")\\n\",\n        \"  command = f\\\"ffmpeg -loglevel error -hwaccel cuda -y -i '{upscaled_video_path}' -c:v h264_nvenc -filter:v  'crop={final_width}:{final_height}:(in_w-{final_width})/2:(in_h-{final_height})/2' -c:v libx264 -pix_fmt yuv420p '{final_video_path}'\\\"\\n\",\n        \"  subprocess.run(command,shell=True)\\n\",\n        \"else:\\n\",\n        \"  # final video path = upscaled video path\\n\",\n        \"  command = f\\\"cp '{upscaled_video_path}' '{final_video_path}'\\\"\\n\",\n        \"  subprocess.run(command,shell=True)\\n\",\n        \"\\n\",\n        \"print(f\\\"Upscaled video saved to: {final_video_path}\\\")\\n\",\n        \"\\n\",\n        \"# save to drive\\n\",\n        \"if mount_drive:\\n\",\n        \"  drive_folder = \\\"MyDrive/Upscaled Videos (REAL-ESRGAN)\\\"\\n\",\n        \"  save_directory_drive = f\\\"/content/gdrive/{drive_folder}\\\"\\n\",\n        \"  os.makedirs(save_directory_drive, exist_ok=True)\\n\",\n        \"\\n\",\n        \"  command = f\\\"cp '{final_video_path}' '{save_directory_drive}/{final_video_name}'\\\"\\n\",\n        \"  subprocess.run(command,shell=True)\\n\",\n        \"  print(f\\\"Saved to drive: /{drive_folder}/{final_video_name}\\\" )\\n\",\n        \"\\n\",\n        \"!rm \\\"{upscaled_video_path}\\\"\"\n      ]\n    },\n    {\n      \"cell_type\": \"markdown\",\n      \"source\": [\n        \"# 4. Disconnect runtime\"\n      ],\n      \"metadata\": {\n        \"id\": \"XydAN-xZT0AS\"\n      }\n    },\n    {\n      \"cell_type\": \"code\",\n      \"source\": [\n        \"from google.colab import runtime\\n\",\n        \"\\n\",\n        \"disconnect_when_finish = False  #@param{type:\\\"boolean\\\"}\\n\",\n        \"\\n\",\n        \"if disconnect_when_finish:\\n\",\n        \"  runtime.unassign()\"\n      ],\n      \"metadata\": {\n        \"cellView\": \"form\",\n        \"id\": \"rIfUT_e9TqLp\"\n      },\n      \"execution_count\": null,\n      \"outputs\": []\n    }\n  ]\n}"
  },
  {
    "path": "README.md",
    "content": "# 4k-Video-Upscaler-Colab-Real-ESRGAN\n\nUpscale your videos up to 4k on free google colab using [Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN)\n \n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/yuvraj108c/4k-video-upscaler-colab/blob/main/4k_Video_Upscaler_Colab_(Real_ESRGAN).ipynb)\n[![Try a demo on Replicate](https://replicate.com/lucataco/real-esrgan-video/badge)](https://replicate.com/lucataco/real-esrgan-video)\n\n![Screenshot 2025-05-01 at 10 03 17](https://github.com/user-attachments/assets/7a0cce5d-bb2d-426a-afd6-468c7a77b82b)\n\n \n## Models\n- RealESRGAN_x4plus\n- RealESRGAN_x4plus_anime_6B\n- realesr-animevideov3\n\n\n## ⭐ Support\nIf you like this project and wish to see updates and new features, please consider supporting me. It helps a lot! \n\n[![buy-me-coffees](https://i.imgur.com/3MDbAtw.png)](https://www.buymeacoffee.com/yuvraj108cZ)\n[![paypal-donation](https://i.imgur.com/w5jjubk.png)](https://paypal.me/yuvraj108c)\n"
  }
]