Skip to content

Commit c8ddd63

Browse files
author
Clement Champetier
committed
StreamProperties: update getDuration method
Throw a runtime_error if the duration is unknown.
1 parent 4cee3de commit c8ddd63

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/AvTranscoder/properties/StreamProperties.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ Rational StreamProperties::getTimeBase() const
5353
float StreamProperties::getDuration() const
5454
{
5555
const Rational timeBase = getTimeBase();
56-
return av_q2d(timeBase) * _formatContext->streams[_streamIndex]->duration;
56+
const size_t streamDurationInStreamTimeBase = _formatContext->streams[_streamIndex]->duration;
57+
if(streamDurationInStreamTimeBase == (size_t)AV_NOPTS_VALUE)
58+
throw std::runtime_error("unknown stream duration");
59+
return av_q2d(timeBase) * streamDurationInStreamTimeBase;
5760
}
5861

5962
AVMediaType StreamProperties::getStreamType() const

src/AvTranscoder/properties/StreamProperties.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class AvExport StreamProperties
1818
size_t getStreamIndex() const { return _streamIndex; }
1919
size_t getStreamId() const;
2020
Rational getTimeBase() const;
21+
22+
/**
23+
* @return duration of the stream in seconds
24+
* @throw runtime_error if the duration is unknown
25+
* @note If a source file does not specify a duration, but does specify
26+
* a bitrate, this value will be estimated from bitrate and file size.
27+
*/
2128
float getDuration() const; ///< in seconds
2229
AVMediaType getStreamType() const;
2330

0 commit comments

Comments
 (0)