Crop aspect-mismatch reflection bands from HDR output

#3
by linoyts HF Staff - opened
Files changed (1) hide show
  1. app.py +25 -0
app.py CHANGED
@@ -290,6 +290,31 @@ def generate_video(
290
  high_quality_hdr=high_quality_hdr,
291
  )
292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  # hdr_video is [f, h, w, c] linear HDR float. Write EXR frames, then
294
  # tonemap to a libx264 mp4 for in-browser preview.
295
  work_dir = Path(tempfile.mkdtemp(prefix="ltx-hdr-"))
 
290
  high_quality_hdr=high_quality_hdr,
291
  )
292
 
293
+ # The pipeline's internal resize_and_reflect_pad adds bottom/right
294
+ # reflection when source aspect != target aspect. Its built-in crop
295
+ # (_decode_video's `out[:, :crop_size[1], :crop_size[0], :]`) only
296
+ # undoes the 64-divisor alignment padding — it leaves aspect-mismatch
297
+ # reflection bands in the decoded output. Apply the same top-left
298
+ # slice here with the un-reflected content region.
299
+ try:
300
+ src_meta = get_videostream_metadata(video_path)
301
+ src_aspect = src_meta.width / src_meta.height
302
+ tgt_aspect = target_w / target_h
303
+ if src_aspect > tgt_aspect:
304
+ content_h = int(round(target_w / src_aspect))
305
+ content_h -= content_h % 2 # libx264 yuv420p needs even dims
306
+ if 0 < content_h < hdr_video.shape[1]:
307
+ print(f"[HDR] Cropping reflected bottom: {hdr_video.shape[1]} -> {content_h}")
308
+ hdr_video = hdr_video[:, :content_h, :, :]
309
+ elif src_aspect < tgt_aspect:
310
+ content_w = int(round(target_h * src_aspect))
311
+ content_w -= content_w % 2 # libx264 yuv420p needs even dims
312
+ if 0 < content_w < hdr_video.shape[2]:
313
+ print(f"[HDR] Cropping reflected right: {hdr_video.shape[2]} -> {content_w}")
314
+ hdr_video = hdr_video[:, :, :content_w, :]
315
+ except Exception as e:
316
+ print(f"[HDR] Post-crop skipped: {type(e).__name__}: {e}")
317
+
318
  # hdr_video is [f, h, w, c] linear HDR float. Write EXR frames, then
319
  # tonemap to a libx264 mp4 for in-browser preview.
320
  work_dir = Path(tempfile.mkdtemp(prefix="ltx-hdr-"))