Spaces:
Runtime error
Runtime error
File size: 7,344 Bytes
2b6ced0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | import os
import subprocess
import roop.globals
import roop.utilities as util
from typing import List, Any
def run_ffmpeg(args: List[str]) -> bool:
commands = [
"ffmpeg",
"-hide_banner",
"-hwaccel",
"auto",
"-y",
"-loglevel",
roop.globals.log_level,
]
commands.extend(args)
print("Running ffmpeg")
try:
subprocess.check_output(commands, stderr=subprocess.STDOUT)
return True
except Exception as e:
print("Running ffmpeg failed! Commandline:")
print(" ".join(commands))
return False
def cut_video(
original_video: str,
cut_video: str,
start_frame: int,
end_frame: int,
reencode: bool,
):
fps = util.detect_fps(original_video)
start_time = start_frame / fps
num_frames = end_frame - start_frame
if reencode:
run_ffmpeg(
[
"-ss",
format(start_time, ".2f"),
"-i",
original_video,
"-c:v",
roop.globals.video_encoder,
"-c:a",
"aac",
"-frames:v",
str(num_frames),
cut_video,
]
)
else:
run_ffmpeg(
[
"-ss",
format(start_time, ".2f"),
"-i",
original_video,
"-frames:v",
str(num_frames),
"-c:v",
"copy",
"-c:a",
"copy",
cut_video,
]
)
def join_videos(videos: List[str], dest_filename: str, simple: bool):
if simple:
txtfilename = util.resolve_relative_path("../temp")
txtfilename = os.path.join(txtfilename, "joinvids.txt")
with open(txtfilename, "w", encoding="utf-8") as f:
for v in videos:
v = v.replace("\\", "/")
f.write(f"file {v}\n")
commands = [
"-f",
"concat",
"-safe",
"0",
"-i",
f"{txtfilename}",
"-vcodec",
"copy",
f"{dest_filename}",
]
run_ffmpeg(commands)
else:
inputs = []
filter = ""
for i, v in enumerate(videos):
inputs.append("-i")
inputs.append(v)
filter += f"[{i}:v:0][{i}:a:0]"
run_ffmpeg(
[
" ".join(inputs),
"-filter_complex",
f'"{filter}concat=n={len(videos)}:v=1:a=1[outv][outa]"',
"-map",
'"[outv]"',
"-map",
'"[outa]"',
dest_filename,
]
)
# filter += f'[{i}:v:0][{i}:a:0]'
# run_ffmpeg([" ".join(inputs), '-filter_complex', f'"{filter}concat=n={len(videos)}:v=1:a=1[outv][outa]"', '-map', '"[outv]"', '-map', '"[outa]"', dest_filename])
def extract_frames(
target_path: str, trim_frame_start, trim_frame_end, fps: float
) -> bool:
util.create_temp(target_path)
temp_directory_path = util.get_temp_directory_path(target_path)
commands = [
"-i",
target_path,
"-q:v",
"1",
"-pix_fmt",
"rgb24",
]
if trim_frame_start is not None and trim_frame_end is not None:
commands.extend(
[
"-vf",
"trim=start_frame="
+ str(trim_frame_start)
+ ":end_frame="
+ str(trim_frame_end)
+ ",fps="
+ str(fps),
]
)
commands.extend(
[
"-vsync",
"0",
os.path.join(
temp_directory_path, "%06d." + roop.globals.CFG.output_image_format
),
]
)
return run_ffmpeg(commands)
def create_video(
target_path: str,
dest_filename: str,
fps: float = 24.0,
temp_directory_path: str = None,
) -> None:
if temp_directory_path is None:
temp_directory_path = util.get_temp_directory_path(target_path)
run_ffmpeg(
[
"-r",
str(fps),
"-i",
os.path.join(
temp_directory_path, f"%06d.{roop.globals.CFG.output_image_format}"
),
"-c:v",
roop.globals.video_encoder,
"-crf",
str(roop.globals.video_quality),
"-pix_fmt",
"yuv420p",
"-vf",
"colorspace=bt709:iall=bt601-6-625:fast=1",
"-y",
dest_filename,
]
)
return dest_filename
def create_gif_from_video(video_path: str, gif_path):
from roop.capturer import get_video_frame, release_video
fps = util.detect_fps(video_path)
frame = get_video_frame(video_path)
release_video()
scalex = frame.shape[0]
scaley = frame.shape[1]
if scalex >= scaley:
scaley = -1
else:
scalex = -1
run_ffmpeg(
[
"-i",
video_path,
"-vf",
f"fps={fps},scale={int(scalex)}:{int(scaley)}:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse",
"-loop",
"0",
gif_path,
]
)
def create_video_from_gif(gif_path: str, output_path):
fps = util.detect_fps(gif_path)
filter = """scale='trunc(in_w/2)*2':'trunc(in_h/2)*2',format=yuv420p,fps=10"""
run_ffmpeg(
[
"-i",
gif_path,
"-vf",
f'"{filter}"',
"-movflags",
"+faststart",
"-shortest",
output_path,
]
)
def repair_video(original_video: str, final_video: str):
run_ffmpeg(
[
"-i",
original_video,
"-movflags",
"faststart",
"-acodec",
"copy",
"-vcodec",
"copy",
final_video,
]
)
def restore_audio(
intermediate_video: str,
original_video: str,
trim_frame_start,
trim_frame_end,
final_video: str,
) -> None:
fps = util.detect_fps(original_video)
commands = ["-i", intermediate_video]
if trim_frame_start is None and trim_frame_end is None:
commands.extend(["-c:a", "copy"])
else:
# if trim_frame_start is not None:
# start_time = trim_frame_start / fps
# commands.extend([ '-ss', format(start_time, ".2f")])
# else:
# commands.extend([ '-ss', '0' ])
# if trim_frame_end is not None:
# end_time = trim_frame_end / fps
# commands.extend([ '-to', format(end_time, ".2f")])
# commands.extend([ '-c:a', 'aac' ])
if trim_frame_start is not None:
start_time = trim_frame_start / fps
commands.extend(["-ss", format(start_time, ".2f")])
else:
commands.extend(["-ss", "0"])
if trim_frame_end is not None:
end_time = trim_frame_end / fps
commands.extend(["-to", format(end_time, ".2f")])
commands.extend(["-i", original_video, "-c", "copy"])
commands.extend(["-map", "0:v:0", "-map", "1:a:0?", "-shortest", final_video])
run_ffmpeg(commands)
|