-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring to use contexts managers, list comprehensions when more idiomatic, and minor renaming to help reader clarity #2335
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,27 +65,23 @@ def write_video(filename, video_array, fps, video_codec="libx264", options=None) | |
_check_av_available() | ||
video_array = torch.as_tensor(video_array, dtype=torch.uint8).numpy() | ||
|
||
container = av.open(filename, mode="w") | ||
|
||
stream = container.add_stream(video_codec, rate=fps) | ||
stream.width = video_array.shape[2] | ||
stream.height = video_array.shape[1] | ||
stream.pix_fmt = "yuv420p" if video_codec != "libx264rgb" else "rgb24" | ||
stream.options = options or {} | ||
|
||
for img in video_array: | ||
frame = av.VideoFrame.from_ndarray(img, format="rgb24") | ||
frame.pict_type = "NONE" | ||
for packet in stream.encode(frame): | ||
with av.open(filename, mode="w") as container: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks! PyAV only added support for using |
||
stream = container.add_stream(video_codec, rate=fps) | ||
stream.width = video_array.shape[2] | ||
stream.height = video_array.shape[1] | ||
stream.pix_fmt = "yuv420p" if video_codec != "libx264rgb" else "rgb24" | ||
stream.options = options or {} | ||
|
||
for img in video_array: | ||
frame = av.VideoFrame.from_ndarray(img, format="rgb24") | ||
frame.pict_type = "NONE" | ||
for packet in stream.encode(frame): | ||
container.mux(packet) | ||
|
||
# Flush stream | ||
for packet in stream.encode(): | ||
container.mux(packet) | ||
|
||
# Flush stream | ||
for packet in stream.encode(): | ||
container.mux(packet) | ||
|
||
# Close the file | ||
container.close() | ||
|
||
|
||
def _read_from_stream( | ||
container, start_offset, end_offset, pts_unit, stream, stream_name | ||
|
@@ -229,37 +225,35 @@ def read_video(filename, start_pts=0, end_pts=None, pts_unit="pts"): | |
audio_frames = [] | ||
|
||
try: | ||
container = av.open(filename, metadata_errors="ignore") | ||
with av.open(filename, metadata_errors="ignore") as container: | ||
if container.streams.video: | ||
video_frames = _read_from_stream( | ||
container, | ||
start_pts, | ||
end_pts, | ||
pts_unit, | ||
container.streams.video[0], | ||
{"video": 0}, | ||
) | ||
video_fps = container.streams.video[0].average_rate | ||
# guard against potentially corrupted files | ||
if video_fps is not None: | ||
info["video_fps"] = float(video_fps) | ||
|
||
if container.streams.audio: | ||
audio_frames = _read_from_stream( | ||
container, | ||
start_pts, | ||
end_pts, | ||
pts_unit, | ||
container.streams.audio[0], | ||
{"audio": 0}, | ||
) | ||
info["audio_fps"] = container.streams.audio[0].rate | ||
|
||
except av.AVError: | ||
# TODO raise a warning? | ||
pass | ||
else: | ||
if container.streams.video: | ||
video_frames = _read_from_stream( | ||
container, | ||
start_pts, | ||
end_pts, | ||
pts_unit, | ||
container.streams.video[0], | ||
{"video": 0}, | ||
) | ||
video_fps = container.streams.video[0].average_rate | ||
# guard against potentially corrupted files | ||
if video_fps is not None: | ||
info["video_fps"] = float(video_fps) | ||
|
||
if container.streams.audio: | ||
audio_frames = _read_from_stream( | ||
container, | ||
start_pts, | ||
end_pts, | ||
pts_unit, | ||
container.streams.audio[0], | ||
{"audio": 0}, | ||
) | ||
info["audio_fps"] = container.streams.audio[0].rate | ||
|
||
container.close() | ||
|
||
vframes = [frame.to_rgb().to_ndarray() for frame in video_frames] | ||
aframes = [frame.to_ndarray() for frame in audio_frames] | ||
|
@@ -288,6 +282,14 @@ def _can_read_timestamps_from_packets(container): | |
return False | ||
|
||
|
||
def _decode_video_timestamps(container): | ||
if _can_read_timestamps_from_packets(container): | ||
# fast path | ||
return [x.pts for x in container.demux(video=0) if x.pts is not None] | ||
else: | ||
return [x.pts for x in container.decode(video=0) if x.pts is not None] | ||
|
||
|
||
def read_video_timestamps(filename, pts_unit="pts"): | ||
""" | ||
List the video frames timestamps. | ||
|
@@ -321,26 +323,18 @@ def read_video_timestamps(filename, pts_unit="pts"): | |
pts = [] | ||
|
||
try: | ||
container = av.open(filename, metadata_errors="ignore") | ||
with av.open(filename, metadata_errors="ignore") as container: | ||
if container.streams.video: | ||
video_stream = container.streams.video[0] | ||
video_time_base = video_stream.time_base | ||
try: | ||
pts = _decode_video_timestamps(container) | ||
except av.AVError: | ||
warnings.warn(f"Failed decoding frames for file {filename}") | ||
video_fps = float(video_stream.average_rate) | ||
except av.AVError: | ||
# TODO add a warning | ||
pass | ||
else: | ||
if container.streams.video: | ||
video_stream = container.streams.video[0] | ||
video_time_base = video_stream.time_base | ||
try: | ||
if _can_read_timestamps_from_packets(container): | ||
# fast path | ||
pts = [x.pts for x in container.demux(video=0) if x.pts is not None] | ||
else: | ||
pts = [ | ||
x.pts for x in container.decode(video=0) if x.pts is not None | ||
] | ||
except av.AVError: | ||
warnings.warn(f"Failed decoding frames for file {filename}") | ||
video_fps = float(video_stream.average_rate) | ||
container.close() | ||
|
||
pts.sort() | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this is fine because this is a private class, but in general renaming methods is a BC-breaking change