Skip to content

Commit cec6d00

Browse files
author
Clement Champetier
committed
AudioProperties: add functions to get properties about time
* getTicksPerFrame. * getTimeBase. * getFps. * getDuration.
1 parent 92440fa commit cec6d00

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/AvTranscoder/mediaProperty/AudioProperties.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,44 @@ size_t AudioProperties::getNbSamples() const
163163
return _formatContext->streams[_streamId]->nb_frames;
164164
}
165165

166+
size_t AudioProperties::getTicksPerFrame() const
167+
{
168+
if( ! _codecContext )
169+
throw std::runtime_error( "unknown codec context" );
170+
return _codecContext->ticks_per_frame;
171+
}
172+
173+
Rational AudioProperties::getTimeBase() const
174+
{
175+
if( ! _codecContext )
176+
throw std::runtime_error( "unknown codec context" );
177+
178+
Rational timeBase = {
179+
_formatContext->streams[_streamId]->time_base.num,
180+
_formatContext->streams[_streamId]->time_base.den,
181+
};
182+
return timeBase;
183+
}
184+
185+
double AudioProperties::getFps() const
186+
{
187+
Rational timeBase = getTimeBase();
188+
double fps = 1.0 * timeBase.den / ( timeBase.num * getTicksPerFrame() );
189+
if( isinf( fps ) )
190+
fps = 0.0;
191+
return fps;
192+
}
193+
194+
double AudioProperties::getDuration() const
195+
{
196+
if( ! _formatContext )
197+
throw std::runtime_error( "unknown format context" );
198+
199+
Rational timeBase = getTimeBase();
200+
double duration = ( timeBase.num / (double) timeBase.den ) * _formatContext->streams[_streamId]->duration;
201+
return duration;
202+
}
203+
166204
PropertiesMap AudioProperties::getPropertiesAsMap() const
167205
{
168206
PropertiesMap dataMap;
@@ -180,6 +218,10 @@ PropertiesMap AudioProperties::getPropertiesAsMap() const
180218
detail::add( dataMap, "channelLayout", getChannelLayout() );
181219
detail::add( dataMap, "channelName", getChannelName() );
182220
detail::add( dataMap, "channelDescription", getChannelDescription() );
221+
detail::add( dataMap, "ticksPerFrame", getTicksPerFrame() );
222+
detail::add( dataMap, "timeBase", getTimeBase() );
223+
detail::add( dataMap, "fps", getFps() );
224+
detail::add( dataMap, "duration", getDuration() );
183225

184226
for( size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex )
185227
{

src/AvTranscoder/mediaProperty/AudioProperties.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class AvExport AudioProperties
3232
size_t getBitRate() const;
3333
size_t getNbSamples() const;
3434

35+
size_t getTicksPerFrame() const;
36+
Rational getTimeBase() const;
37+
double getFps() const;
38+
double getDuration() const;
39+
3540
PropertiesMap& getMetadatas() { return _metadatas; }
3641

3742
#ifndef SWIG

0 commit comments

Comments
 (0)