Skip to content

Commit

Permalink
Fix Video Unit Tests (#248)
Browse files Browse the repository at this point in the history
Summary:

A reason our OSS repo tests are failing is because on OSS our vidgear dependency was on v0.3.2 and we were on v0.2.4

They changed the `output_filename` parameter to be `output` between these versions, so updating our code

Reviewed By: mayaliliya

Differential Revision: D50663349
  • Loading branch information
jbitton authored and facebook-github-bot committed Oct 27, 2023
1 parent 93a0b90 commit 77d0f5b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion augly/audio/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ librosa>=0.8.1
numpy>=1.19.5
SoundFile>=0.10.3.post1
torch>=1.9.0
torchaudio>=0.9.0
torchaudio==2.0.2
32 changes: 24 additions & 8 deletions augly/image/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,28 +937,44 @@ def meme_format(
local_font_path = utils.pathmgr.get_local_path(font_file)
font_size = caption_height - 10

meme = Image.new("RGB", (width, height + caption_height), meme_bg_color)
meme.paste(image, (0, caption_height))
draw = ImageDraw.Draw(meme)

x_pos, y_pos = 5, 5
ascender_adjustment = 40
while True:
font = ImageFont.truetype(local_font_path, font_size)
text_width, text_height = font.getsize_multiline(text)
text_bbox = draw.multiline_textbbox(
(x_pos, y_pos),
text,
# pyre-fixme[6]: Expected `Optional[ImageFont._Font]` for 3rd param but got
# `FreeTypeFont`.
font=font,
anchor="la",
align="center",
)

text_width, text_height = (
text_bbox[2] - text_bbox[0],
text_bbox[3] - text_bbox[1],
)

x_pos = round((width - text_width) / 2)
y_pos = round((caption_height - text_height) / 2) - ascender_adjustment

if text_width <= (width - 10) and text_height <= (caption_height - 10):
break

font_size -= 5

meme = Image.new("RGB", (width, height + caption_height), meme_bg_color)
meme.paste(image, (0, caption_height))

x_pos = round((width - text_width) / 2)
y_pos = round((caption_height - text_height) / 2)

draw = ImageDraw.Draw(meme)
draw.multiline_text(
(x_pos, y_pos),
text,
# pyre-fixme[6]: Expected `Optional[ImageFont._Font]` for 3rd param but got
# `FreeTypeFont`.
font=font,
anchor="la",
fill=(text_color[0], text_color[1], text_color[2], round(opacity * 255)),
align="center",
)
Expand Down
7 changes: 7 additions & 0 deletions augly/tests/video_tests/base_unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def check_equal_metadata(
)
if not condition(actual_path, expected_path):
return False
elif isinstance(act_v, float) and isinstance(act_v, float):
self.assertAlmostEqual(
act_v,
exp_v,
places=3,
msg=f"Error comparing values: actual={act_v}, expected={exp_v}, actual_metadata={actual_meta}.",
)
else:
self.assertTrue(
condition(act_v, exp_v),
Expand Down
2 changes: 1 addition & 1 deletion augly/video/augmenters/ffmpeg/base_augmenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def add_augmenter(
if video_path == output_path:
shutil.copyfile(video_path, tmpfile.name)
video_path = tmpfile.name
writer = WriteGear(output_filename=output_path, logging=True)
writer = WriteGear(output=output_path, logging=True)
writer.execute_ffmpeg_cmd(self.get_command(video_path, output_path))
writer.close()

Expand Down
2 changes: 1 addition & 1 deletion augly/video/helpers/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def combine_frames_and_audio_to_file(


def execute_vidgear_command(output_path: str, ffmpeg_command: List[str]) -> None:
writer = WriteGear(output_filename=output_path, logging=True)
writer = WriteGear(output=output_path, logging=True)
writer.execute_ffmpeg_cmd(ffmpeg_command)
writer.close()

Expand Down
8 changes: 3 additions & 5 deletions augly/video/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ def create_video_from_image(output_path: str, image_path: str, duration: float)
utils.validate_image_path(image_path)
assert duration > 0, "Duration of the video must be a positive value"

im_stream = ffmpeg.input(image_path)
video = (
im_stream.filter("loop", 1)
.filter("framerate", utils.DEFAULT_FRAME_RATE)
.filter("pad", **{"width": "ceil(iw/2)*2", "height": "ceil(ih/2)*2"})
im_stream = ffmpeg.input(image_path, stream_loop=-1)
video = im_stream.filter("framerate", utils.DEFAULT_FRAME_RATE).filter(
"pad", **{"width": "ceil(iw/2)*2", "height": "ceil(ih/2)*2"}
)

silent_audio_path = utils.pathmgr.get_local_path(utils.SILENT_AUDIO_PATH)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ dataclasses-json>=0.5.2
iopath>=0.1.8
python-magic>=0.4.22
regex>=2021.4.4
setuptools>=65.5.1

0 comments on commit 77d0f5b

Please sign in to comment.