Skip to content

Commit f477447

Browse files
committed
Merge pull request #177 from cchampet/dev_transcoderShortcutFunctionToAddAllStreamsOfFile
Transcoder: add a shortcut function to add all streams of file
2 parents a975088 + 603a415 commit f477447

File tree

6 files changed

+106
-21
lines changed

6 files changed

+106
-21
lines changed

app/pyRewrap/pyrewrap.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,9 @@
7070

7171
# create transcoder
7272
transcoder = av.Transcoder( outputFile )
73-
74-
def addStreamsToTranscoder(transcoder, streams):
75-
for st in streams:
76-
stIndex = st.getStreamIndex()
77-
transcoder.add( args.inputFileName, stIndex )
78-
79-
addStreamsToTranscoder(transcoder, inputFile.getProperties().getVideoProperties())
80-
addStreamsToTranscoder(transcoder, inputFile.getProperties().getAudioProperties())
81-
# addStreamsToTranscoder(transcoder, inputFile.getProperties().getDataProperties())
73+
transcoder.add( args.inputFileName )
8274

8375
# launch process
8476
progress = av.ConsoleProgress()
8577
transcoder.process(progress)
78+

src/AvTranscoder/file/OutputFile.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ IOutputStream& OutputFile::addVideoStream( const VideoCodec& videoDesc )
4040
stream.codec->pix_fmt = videoDesc.getAVCodecContext().pix_fmt;
4141
stream.codec->profile = videoDesc.getAVCodecContext().profile;
4242
stream.codec->level = videoDesc.getAVCodecContext().level;
43+
stream.codec->field_order = videoDesc.getAVCodecContext().field_order;
4344

4445
// some codecs need/can use extradata to decode
4546
uint8_t* srcExtradata = videoDesc.getAVCodecContext().extradata;

src/AvTranscoder/transcoder/StreamTranscoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ void StreamTranscoder::needToSwitchToGenerator( const bool needToSwitch )
541541
if( needToSwitch && ! canSwitchToGenerator() )
542542
{
543543
std::stringstream os;
544-
os << "The stream " << _inputStream->getStreamIndex() << " needs to switch to a generator during the process, but it cannot.";
544+
os << "The stream " << _inputStream->getStreamIndex() << " has a duration of "<< getDuration() << "s. It needs to switch to a generator during the process, but it cannot.";
545545
throw std::runtime_error( os.str() );
546546
}
547547
_needToSwitchToGenerator = needToSwitch;

src/AvTranscoder/transcoder/Transcoder.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ Transcoder::~Transcoder()
3333
}
3434
}
3535

36+
void Transcoder::add( const std::string& filename )
37+
{
38+
const int streamIndex = -1;
39+
const float offset = 0;
40+
const InputFile* referenceFile = addInputFile( filename, streamIndex, offset );
41+
const std::vector< avtranscoder::StreamProperties* >& inputStreams = referenceFile->getProperties().getStreamProperties();
42+
for( size_t index = 0; index < inputStreams.size(); ++index )
43+
{
44+
const AVMediaType streamType = referenceFile->getProperties().getStreamPropertiesWithIndex( index ).getStreamType();
45+
// skip the stream if it is not video nor audio
46+
if( streamType == AVMEDIA_TYPE_VIDEO || streamType == AVMEDIA_TYPE_AUDIO )
47+
addRewrapStream( filename, index, offset );
48+
}
49+
}
50+
3651
void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName, const float offset )
3752
{
3853
// Re-wrap
@@ -348,18 +363,21 @@ void Transcoder::addDummyStream( const ProfileLoader::Profile& profile, const IC
348363
_streamTranscoders.push_back( _streamTranscodersAllocated.back() );
349364
}
350365

351-
InputFile* Transcoder::addInputFile( const std::string& filename, const size_t streamIndex, const float offset )
366+
InputFile* Transcoder::addInputFile( const std::string& filename, const int streamIndex, const float offset )
352367
{
353368
InputFile* referenceFile = NULL;
354369

355-
for( std::vector< InputFile* >::iterator it = _inputFiles.begin(); it != _inputFiles.end(); ++it )
370+
if( streamIndex >= 0 )
356371
{
357-
if( ( (*it)->getFilename() == filename ) &&
358-
! (*it)->getStream( streamIndex ).isActivated() )
372+
for( std::vector< InputFile* >::iterator it = _inputFiles.begin(); it != _inputFiles.end(); ++it )
359373
{
360-
referenceFile = (*it);
361-
LOG_DEBUG( "Get instance of InputFile from '" << filename << "'" )
362-
break;
374+
if( ( (*it)->getFilename() == filename ) &&
375+
! (*it)->getStream( streamIndex ).isActivated() )
376+
{
377+
referenceFile = (*it);
378+
LOG_DEBUG( "Get instance of InputFile from '" << filename << "'" )
379+
break;
380+
}
363381
}
364382
}
365383

@@ -371,7 +389,13 @@ InputFile* Transcoder::addInputFile( const std::string& filename, const size_t s
371389
referenceFile = _inputFiles.back();
372390
}
373391

374-
referenceFile->activateStream( streamIndex );
392+
if( streamIndex >= 0 )
393+
referenceFile->activateStream( streamIndex );
394+
else
395+
{
396+
for( size_t index = 0; index < referenceFile->getProperties().getNbStreams(); ++index )
397+
referenceFile->activateStream( index );
398+
}
375399

376400
// If negative offset, move forward in the input stream
377401
if( offset < 0 )

src/AvTranscoder/transcoder/Transcoder.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ class AvExport Transcoder
5151

5252
~Transcoder();
5353

54+
/**
55+
* @brief Add all streams of the file with the given filename.
56+
* All the streams will be rewrapped.
57+
* @note Currently we rewrap only the video and the audio streams. The streams with an other type are skipped.
58+
*/
59+
void add( const std::string& filename );
60+
5461
/**
5562
* @brief Add a stream and set a profile
5663
* @note If profileName is empty, rewrap.
@@ -159,7 +166,10 @@ class AvExport Transcoder
159166

160167
void addDummyStream( const ProfileLoader::Profile& profile, const ICodec& codec );
161168

162-
InputFile* addInputFile( const std::string& filename, const size_t streamIndex, const float offset );
169+
/**
170+
* @note If streamIndex is negative, activate all streams of the file.
171+
*/
172+
InputFile* addInputFile( const std::string& filename, const int streamIndex, const float offset );
163173

164174
ProfileLoader::Profile getProfileFromFile( InputFile& inputFile, const size_t streamIndex ); ///< The function analyses the inputFile
165175

test/pyTest/testTranscoderAdd.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import os
22

33
# Check if environment is setup to run the tests
4-
if os.environ.get('AVTRANSCODER_TEST_AUDIO_WAVE_FILE') is None:
4+
if os.environ.get('AVTRANSCODER_TEST_AUDIO_WAVE_FILE') is None or \
5+
os.environ.get('AVTRANSCODER_TEST_AUDIO_MOV_FILE') is None:
56
from nose.plugins.skip import SkipTest
6-
raise SkipTest("Need to define environment variable AVTRANSCODER_TEST_AUDIO_WAVE_FILE")
7+
raise SkipTest("Need to define environment variables "
8+
"AVTRANSCODER_TEST_AUDIO_WAVE_FILE and "
9+
"AVTRANSCODER_TEST_AUDIO_MOV_FILE")
710

811
from nose.tools import *
912

@@ -31,3 +34,57 @@ def testAddStreamTranscoder():
3134
progress = av.NoDisplayProgress()
3235
transcoder.process( progress )
3336

37+
38+
@raises(IOError)
39+
def testAddAllStreamsOfFileWhichDoesNotExist():
40+
"""
41+
Add all streams from a given file.
42+
"""
43+
# input
44+
inputFileName = "fileWhichDoesNotExist.mov"
45+
46+
# output
47+
outputFileName = "testAddAllStreamsOfFileWhichDoesNotExist.mov"
48+
ouputFile = av.OutputFile( outputFileName )
49+
50+
transcoder = av.Transcoder( ouputFile )
51+
transcoder.add( inputFileName )
52+
53+
# process
54+
progress = av.ConsoleProgress()
55+
transcoder.process( progress )
56+
57+
58+
def testAddAllStreamsOfAGivenFile():
59+
"""
60+
Add all streams from a given file.
61+
"""
62+
# input
63+
inputFileName = os.environ['AVTRANSCODER_TEST_AUDIO_MOV_FILE']
64+
65+
# output
66+
outputFileName = "testAddAllStreamsOfAGivenFile.mov"
67+
ouputFile = av.OutputFile( outputFileName )
68+
69+
transcoder = av.Transcoder( ouputFile )
70+
transcoder.add( inputFileName )
71+
72+
# process
73+
progress = av.ConsoleProgress()
74+
transcoder.process( progress )
75+
76+
# get src file
77+
src_inputFile = av.InputFile( inputFileName )
78+
src_properties = src_inputFile.getProperties()
79+
src_streams_properties = src_properties.getStreamProperties()
80+
81+
# get dst file
82+
dst_inputFile = av.InputFile( outputFileName )
83+
dst_properties = dst_inputFile.getProperties()
84+
dst_streams_properties = dst_properties.getStreamProperties()
85+
86+
import testTranscoderRewrap
87+
# for each stream
88+
for src_stream, dst_stream in zip(src_streams_properties, dst_streams_properties):
89+
# check properties
90+
testTranscoderRewrap.checkStream(src_stream, dst_stream)

0 commit comments

Comments
 (0)