Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion av/stream.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,11 @@ cdef class Stream(object):

:type: str
"""
return lib.av_get_media_type_string(self.ptr.codecpar.codec_type)
cdef const char *s
s = lib.av_get_media_type_string(self.ptr.codecpar.codec_type)
# NULL if media_type is unknown (AVMEDIA_TYPE_UNKNOWN)
# Without this manual check, a segfault can occur
if s == NULL:
return None
else:
return s
19 changes: 19 additions & 0 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import av
from fractions import Fraction

from .common import TestCase, fate_suite

Expand Down Expand Up @@ -27,3 +28,21 @@ def test_selection(self):
self.assertEqual([video], container.streams.get(video=(0,)))

# TODO: Find something in the fate suite with video, audio, and subtitles.

def test_stream_properties(self):
# Ensure that all stream properties have sensible values even
# if writing to the stream hasn't started yet
with av.open(self.sandboxed("output.mp4"), "w") as container:
stream = container.add_stream('h264')

@jlaine jlaine Jan 23, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the type unknown when we add a stream?

Could this be related to #1062 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be. I believe that it is because the codec context is only "opened" on first use.

TBH, I only noticedthis since accessing the __repr__ when running my code interactively cause things to crash.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, with #1062 (and my "fix" removed) applied I should update this test with:

diff --git a/tests/test_streams.py b/tests/test_streams.py
index efee029..496d32d 100644
--- a/tests/test_streams.py
+++ b/tests/test_streams.py
@@ -45,4 +45,4 @@ class TestStreams(TestCase):
             assert stream.duration is None
             assert stream.frames == 0
             assert stream.language is None
-            assert stream.type is None
+            assert stream.type == 'video'

assert stream.id is not None
assert stream.profile is None
assert stream.index == 0
assert stream.time_base is None
assert stream.average_rate == Fraction(24, 1)
assert stream.base_rate is None
assert stream.guessed_rate is None
assert stream.start_time is None
assert stream.duration is None
assert stream.frames == 0
assert stream.language is None
assert stream.type is None