Skip to content

Commit da5438e

Browse files
author
Clement Champetier
committed
InputFile: refactoring seekAtFrame
1 parent efb8120 commit da5438e

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/AvTranscoder/file/InputFile.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,18 @@ bool InputFile::readNextPacket( CodedData& data, const size_t streamIndex )
143143

144144
void InputFile::seekAtFrame( const size_t frame )
145145
{
146-
double fps = 1;
147-
// Get Fps from first video stream
148-
if( _properties.getNbVideoStreams() )
149-
fps = _properties.getVideoProperties().at( 0 ).getFps();
150-
151-
uint64_t pos = frame / fps * AV_TIME_BASE;
146+
uint64_t position = frame / getFps() * AV_TIME_BASE;
147+
seek( position );
148+
}
152149

153-
// Offset of start time
150+
void InputFile::seek( uint64_t position )
151+
{
154152
if( (int)_formatContext.getStartTime() != AV_NOPTS_VALUE )
155-
pos += _formatContext.getStartTime();
153+
position += _formatContext.getStartTime();
156154

157-
if( av_seek_frame( &_formatContext.getAVFormatContext(), -1, pos, AVSEEK_FLAG_BACKWARD ) < 0 )
155+
if( av_seek_frame( &_formatContext.getAVFormatContext(), -1, position, AVSEEK_FLAG_BACKWARD ) < 0 )
158156
{
159-
std::cerr << "Error during seek at " << frame << " (" << pos << ") in file" << std::endl;
157+
std::cerr << "Error during seek at " << position << " seconds in file" << std::endl;
160158
}
161159

162160
for( std::vector<InputStream*>::iterator it = _inputStreams.begin(); it != _inputStreams.end(); ++it )
@@ -186,6 +184,14 @@ InputStream& InputFile::getStream( size_t index )
186184
}
187185
}
188186

187+
double InputFile::getFps()
188+
{
189+
double fps = 1;
190+
if( _properties.getNbVideoStreams() )
191+
fps = _properties.getVideoProperties().at( 0 ).getFps();
192+
return fps;
193+
}
194+
189195
void InputFile::setProfile( const ProfileLoader::Profile& profile )
190196
{
191197
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )

src/AvTranscoder/file/InputFile.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ class AvExport InputFile
9797
**/
9898
static FileProperties analyseFile( const std::string& filename, IProgress& progress, const EAnalyseLevel level = eAnalyseLevelFirstGop );
9999

100+
private:
101+
/**
102+
* @brief Get Fps from first video stream
103+
* @note if there is no video stream, return 1.
104+
*/
105+
double getFps();
106+
107+
/**
108+
* @brief Seek at a specific position (in AV_TIME_BASE units)
109+
* @note before seek, add offset of start time
110+
* @note after seek, clear buffering of streams
111+
*/
112+
void seek( uint64_t position );
113+
100114
protected:
101115
FormatContext _formatContext;
102116
FileProperties _properties;

0 commit comments

Comments
 (0)