Skip to content

Commit

Permalink
process-video: fps video filter for invalid fps metadata videos
Browse files Browse the repository at this point in the history
  • Loading branch information
chapmanjacobd committed Nov 18, 2024
1 parent 4307e63 commit f95c493
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
21 changes: 1 addition & 20 deletions xklb/createdb/av.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,6 @@ def munge_av_tags(args, media) -> dict:

streams = probe.streams

def parse_framerate(string) -> float | None:
top, bot = string.split("/")
bot = float(bot)
if bot == 0:
return None
return float(top) / bot

fps = iterables.safe_unpack(
[
parse_framerate(s.get("avg_frame_rate"))
for s in streams
if s.get("avg_frame_rate") is not None and "/0" not in s.get("avg_frame_rate")
]
+ [
parse_framerate(s.get("r_frame_rate"))
for s in streams
if s.get("r_frame_rate") is not None and "/0" not in s.get("r_frame_rate")
],
)
width = iterables.safe_unpack([s.get("width") for s in streams])
height = iterables.safe_unpack([s.get("height") for s in streams])

Expand Down Expand Up @@ -279,7 +260,7 @@ def parse_framerate(string) -> float | None:
"other_count": other_count,
"width": width,
"height": height,
"fps": fps,
"fps": probe.fps,
"duration": nums.safe_int(duration),
"language": language,
"corruption": None if corruption is None else int(corruption * 100),
Expand Down
32 changes: 28 additions & 4 deletions xklb/mediafiles/process_ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,39 @@ def process_path(args, path, **kwargs):
]
)

video_filters = []
if probe.fps and probe.fps > 240:
log.info("fps>240 is not supported by AV1 and it seems suspect. Ignoring metadata %s", path)
frames = nums.safe_int(
processes.cmd(
"ffprobe",
"-v",
"fatal",
"-select_streams",
"v:0",
"-count_frames",
"-show_entries",
"stream=nb_read_frames",
"-of",
"default=noprint_wrappers=1:nokey=1",
path,
).stdout
)
if frames and probe.duration:
actual_fps = frames / probe.duration
video_filters.append(f"fps={actual_fps}")

width = int(video_stream.get("width"))
height = int(video_stream.get("height"))

if width > (args.max_width * (1 + args.max_width_buffer)):
ff_opts.extend(["-vf", f"scale={args.max_width}:-2"])
video_filters.append(f"scale={args.max_width}:-2")
elif height > (args.max_height * (1 + args.max_height_buffer)):
ff_opts.extend(["-vf", f"scale=-2:{args.max_height}"])
video_filters.append(f"scale=-2:{args.max_height}")
else: # make sure input raster is even for YUV_420 colorspace
ff_opts.extend(["-vf", "pad='if(mod(iw,2),iw+1,iw)':'if(mod(ih,2),ih+1,ih)'"])
video_filters.append("pad='if(mod(iw,2),iw+1,iw)':'if(mod(ih,2),ih+1,ih)'")

ff_opts.extend(["-vf", ",".join(video_filters)])

elif album_art_stream:
ff_opts.extend(["-map", "0:v", "-c:v", "copy"])
Expand Down Expand Up @@ -214,7 +238,7 @@ def process_path(args, path, **kwargs):
opus_rate = 24000
else:
opus_rate = 16000
ff_opts.extend(["-c:a", "libopus", "-ar", str(opus_rate), "-filter:a", "loudnorm=i=-18:tp=-3:lra=17"])
ff_opts.extend(["-c:a", "libopus", "-ar", str(opus_rate), "-af", "loudnorm=i=-18:tp=-3:lra=17"])

if is_split:
try:
Expand Down
21 changes: 21 additions & 0 deletions xklb/utils/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,27 @@ def __init__(self, path, *args):
else:
self.duration -= start

self.fps = iterables.safe_unpack(
[
self.parse_framerate(s.get("avg_frame_rate"))
for s in self.streams
if s.get("avg_frame_rate") is not None and "/0" not in s.get("avg_frame_rate")
]
+ [
self.parse_framerate(s.get("r_frame_rate"))
for s in self.streams
if s.get("r_frame_rate") is not None and "/0" not in s.get("r_frame_rate")
],
)

@staticmethod
def parse_framerate(string) -> float | None:
top, bot = string.split("/")
bot = float(bot)
if bot == 0:
return None
return float(top) / bot


def unar_out_path(archive_path):
output_path = str(Path(archive_path).with_suffix(""))
Expand Down

0 comments on commit f95c493

Please sign in to comment.