Pythonの[RealESRGANer]ライブラリで動画ファイルを高解像度で明瞭化したコードを備忘録的に投稿しておきます。
動画を高解像度化する
Pythonをインストールした環境で[RealESRGANer]を利用すると、ある程度まで動画ファイルを高解像度化(明瞭化)することが可能でした。
流れ的には、動画をフレームレートに沿って静止画に書き出して[RealESRGANer]ライブラリで明瞭化してから再度動画ファイルとして再作成します。
作成したコードがこんな感じ。
import cv2
import os
import shutil
import subprocess
import torch
from realesrgan import RealESRGANer
from basicsr.archs.rrdbnet_arch import RRDBNet
source_path = "動画ファイルの場所"
weight_file = "[RealESRGANer]で利用する.pthファイル(RealESRGAN_x4plus.pthなど)"
"
#動画の長さを取得
def GetDuration(source_path):
cap = cv2.VideoCapture(source_path)
frameCount = cap.get(cv2.CAP_PROP_FRAME_COUNT)
fps = cap.get(cv2.CAP_PROP_FPS)
duration = frameCount / fps if fps > 0 else 0
cap.release()
return duration
#動画のフレームレートを取得
def GetFps(source_path):
cap = cv2.VideoCapture(source_path)
fps = cap.get(cv2.CAP_PROP_FPS)
cap.release()
return fps
#動画の音声部分を再保存
def SetupAudioFile(source_path):
cmd = [
"ffmpeg",
"-i", source_path,
"-acodec", "copy",
source_path + ".aac"
]
subprocess.run(cmd)
#動画から画像を保存
def SetupFrameImages(source_path, destination_path, digits):
cmd = [
"ffmpeg",
"-i", source_path,
destination_path + "\\image" + digits + ".png"
]
subprocess.run(cmd)
#画像から高解像度化
def SetupRefineImages(source_dir, destination_dir):
device = 'cuda' if torch.cuda.is_available() else 'cpu'
for filename in os.listdir(source_dir):
filepath = os.path.join(source_dir, filename)
if os.path.isfile(filepath):
output_path = destination_dir + "\\" + filename
print(f"変換中: {filepath}")
model = RRDBNet(
num_in_ch=3,
num_out_ch=3,
num_feat=64,
num_block=6,
num_grow_ch=32,
scale=4
)
model = model.to(device)
upsampler = RealESRGANer(
scale=4,
model_path=weight_file,
model=model,
tile=0,
half=False
)
img = cv2.imread(filepath, cv2.IMREAD_COLOR)
output, _ = upsampler.enhance(img, outscale=4)
cv2.imwrite(output_path, output)
#画像から動画ファイルを作成
def SetupRefineMovie(source_dir, audio_file, destination_path):
cmd = [
"ffmpeg",
"-i", source_dir,
"-i", audio_file,
"-vcodec", "libx264",
"-pix_fmt", "yuv420p",
destination_path
]
subprocess.run(cmd)
if not os.path.exists(source_path):
print("動画ファイルがありません:" + source_path)
exit()
fps = GetFps(source_path)
print("FPS:" + str(fps))
duration = GetDuration(source_path)
print("Duration:" + str(duration))
digits = "%0" + str(len(str(duration * fps))) + "d"
print("digits:" + digits)
source_dir = os.path.dirname(source_path) + "\\images"
os.makedirs(source_dir, exist_ok=True)
print("source_dir:" + source_dir)
SetupFrameImages(source_path, source_dir, digits)
destination_dir = os.path.dirname(source_path) + "\\mods"
os.makedirs(destination_dir, exist_ok=True)
print("destination_dir:" + destination_dir)
SetupRefineImages(source_dir, destination_dir)
SetupAudioFile(source_path)
output_path, ext = os.path.splitext(source_path)
SetupRefineMovie(destination_dir + "\\image" + digits + ".png", source_path + ".aac", output_path + "-hq.mp4")
os.remove(source_path + ".aac")
shutil.rmtree(source_dir)
shutil.rmtree(destination_dir)
print("動画ファイルの作成完了:" + output_path + "-hq.mp4")
実行すると[images]フォルダーに動画から静止画が出力されてから[mods]フォルダーに明瞭化したファイルが作成されます。
[RealESRGANer]の依存ファイル
[RealESRGANer]ライブラリには、PyTorchなど依存しているライブラリの細かなバージョンのインストールが必要になります。
必要なライブラリについては別記事をご覧ください
長い動画の場合はCUDAが必要
画像を明瞭化をする再にGPUを利用した方が処理が速く進みます。
CUDAが利用できる環境であれば、[RealESRGANer]ライブラリの処理をGPUに任せることで高速な処理が実現できます。
CUDAを利用した依存ファイルのインストールについては別記事をご覧ください。
まとめ
今回は短い記事ですが、Pythonの[RealESRGANer]ライブラリで動画ファイルを高解像度で明瞭化したコードについて書きました。
動画ファイルのフレームを画像として出力後に、高解像度化して再度動画ファイルを作成する手順で、明瞭化した動画ファイルの作成が可能でした。
動画ファイルの明瞭化を考えている人の参考になれば幸いです。
スポンサーリンク
最後までご覧いただき、ありがとうございます。
