Skip to content

Commit 4f107ab

Browse files
author
Daniel Flores
committed
Add file_like tests similar to AudioEncoder
1 parent 050d91a commit 4f107ab

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

test/test_ops.py

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,8 +1438,7 @@ def test_video_encoder_round_trip(self, tmp_path, format, method):
14381438
file_like=file_like,
14391439
**params,
14401440
)
1441-
file_like.seek(0)
1442-
round_trip_frames = self.decode(file_like).data
1441+
round_trip_frames = self.decode(file_like.getvalue()).data
14431442
else:
14441443
raise ValueError(f"Unknown method: {method}")
14451444

@@ -1586,6 +1585,82 @@ def test_video_encoder_against_ffmpeg_cli(self, tmp_path, format):
15861585
ff_frame, enc_frame, percentage=percentage, atol=2
15871586
)
15881587

1588+
def test_to_file_like_custom_file_object(self):
1589+
"""Test with a custom file-like object that implements write and seek."""
1590+
1591+
class CustomFileObject:
1592+
def __init__(self):
1593+
self._file = io.BytesIO()
1594+
1595+
def write(self, data):
1596+
return self._file.write(data)
1597+
1598+
def seek(self, offset, whence=0):
1599+
return self._file.seek(offset, whence)
1600+
1601+
def get_encoded_data(self):
1602+
return self._file.getvalue()
1603+
1604+
source_frames = self.decode(TEST_SRC_2_720P.path).data
1605+
file_like = CustomFileObject()
1606+
encode_video_to_file_like(
1607+
source_frames, frame_rate=30, crf=0, format="mp4", file_like=file_like
1608+
)
1609+
decoded_samples = self.decode(file_like.get_encoded_data())
1610+
1611+
torch.testing.assert_close(
1612+
decoded_samples.data,
1613+
source_frames,
1614+
atol=2,
1615+
rtol=0,
1616+
)
1617+
1618+
def test_to_file_like_real_file(self, tmp_path):
1619+
"""Test to_file_like with a real file opened in binary write mode."""
1620+
source_frames = self.decode(TEST_SRC_2_720P.path).data
1621+
file_path = tmp_path / "test_file_like.mp4"
1622+
1623+
with open(file_path, "wb") as file_like:
1624+
encode_video_to_file_like(
1625+
source_frames, frame_rate=30, crf=0, format="mp4", file_like=file_like
1626+
)
1627+
decoded_samples = self.decode(str(file_path))
1628+
1629+
torch.testing.assert_close(
1630+
decoded_samples.data,
1631+
source_frames,
1632+
atol=2,
1633+
rtol=0,
1634+
)
1635+
1636+
def test_to_file_like_bad_methods(self):
1637+
source_frames = self.decode(TEST_SRC_2_720P.path).data
1638+
1639+
class NoWriteMethod:
1640+
def seek(self, offset, whence=0):
1641+
return 0
1642+
1643+
with pytest.raises(
1644+
RuntimeError, match="File like object must implement a write method"
1645+
):
1646+
encode_video_to_file_like(
1647+
source_frames,
1648+
frame_rate=30,
1649+
format="mp4",
1650+
file_like=NoWriteMethod(),
1651+
)
1652+
1653+
class NoSeekMethod:
1654+
def write(self, data):
1655+
return len(data)
1656+
1657+
with pytest.raises(
1658+
RuntimeError, match="File like object must implement a seek method"
1659+
):
1660+
encode_video_to_file_like(
1661+
source_frames, frame_rate=30, format="mp4", file_like=NoSeekMethod()
1662+
)
1663+
15891664

15901665
if __name__ == "__main__":
15911666
pytest.main()

0 commit comments

Comments
 (0)