Skip to content

Commit 7a9f634

Browse files
Merge branch 'dev_audioTranscoder' into develop
2 parents 1aa162d + 6fec788 commit 7a9f634

24 files changed

+514
-80
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
avTranscoder
2-
============
1+
# avTranscoder
32

43
C++ API for LibAV / FFMpeg
4+
5+
Based on library to support various video formats, avTrasncoder provide the high level API to re-wrap or transcode easilly medias.
6+
7+
You can also use it in Java & Python for simpliest integration in projects.
8+
9+
#### Continuous Integration
10+
11+
###### Drone.io
12+
[![Build Status](https://drone.io/github.com/MarcAntoine-Arnaud/avTranscoder/status.png)](https://drone.io/github.com/MarcAntoine-Arnaud/avTranscoder/latest)
13+
14+
#### Packaging
15+
16+
###### Build openSUSE
17+
comming soon
18+

SConstruct

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ envJava.Replace(
8383
libavLibDir,
8484
"#src",
8585
],
86-
JARCHDIR = env.Dir('#build/src').get_abspath(),
86+
JARCHDIR = env.Dir('#build/src/AvTranscoder').get_abspath(),
8787
)
8888

8989
envJava.Append(

app/SConscript

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,37 @@ avtransform = env.Program(
3333
]
3434
)
3535

36+
avprocessor = env.Program(
37+
'avprocessor',
38+
Glob( 'genericProcessor/*.cpp' ),
39+
LIBS = [
40+
sAvTranscoder,
41+
'avutil',
42+
'avformat',
43+
'avcodec',
44+
'swscale',
45+
],
46+
CXXFLAGS = [
47+
'-std=c++0x'
48+
],
49+
)
50+
51+
audioRewrapper = env.Program(
52+
'audioWrap',
53+
Glob( 'audioRewrapper/*.cpp' ),
54+
LIBS = [
55+
sAvTranscoder,
56+
'avutil',
57+
'avformat',
58+
'avcodec',
59+
'swscale',
60+
]
61+
)
62+
3663
env.Depends( avmeta, sAvTranscoder )
64+
env.Depends( audioRewrapper, sAvTranscoder )
3765

3866
env.Alias( "install", env.Install(os.path.join( installPrefix, "bin" ), avmeta ) )
67+
env.Alias( "install", env.Install(os.path.join( installPrefix, "bin" ), avprocessor ) )
3968
env.Alias( "install", env.Install(os.path.join( installPrefix, "bin" ), avtransform ) )
69+
env.Alias( "install", env.Install(os.path.join( installPrefix, "bin" ), audioRewrapper ) )

app/audioRewrapper/audioRewrapper.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
4+
#include <AvTranscoder/InputFile.hpp>
5+
#include <AvTranscoder/InputStream.hpp>
6+
#include <AvTranscoder/InputStreamAudio.hpp>
7+
8+
#include <AvTranscoder/OutputFile.hpp>
9+
10+
void rewrapAudio( const char* inputfilename, const char* outputFilename )
11+
{
12+
using namespace avtranscoder;
13+
14+
av_log_set_level( AV_LOG_FATAL );
15+
av_log_set_level( AV_LOG_DEBUG );
16+
17+
InputFile inputFile( inputfilename );
18+
inputFile.analyse();
19+
20+
OutputFile outputFile( outputFilename );
21+
22+
outputFile.setup();
23+
24+
outputFile.addAudioStream( inputFile.getStream( 0 ).getAudioDesc() );
25+
26+
DataStream data;
27+
28+
// Encodage/transcodage
29+
std::cout << "start re-wrapping" << std::endl;
30+
31+
size_t frame = 0;
32+
33+
while( inputFile.getStream( 0 ).readNextPacket( data ) )
34+
{
35+
std::cout << "\rprocess frame " << (int)frame - 1 << std::flush;
36+
37+
outputFile.wrap( data, 0 );
38+
39+
++frame;
40+
}
41+
std::cout << std::endl;
42+
}
43+
44+
void transcodeAudio( const char* inputfilename, const char* outputFilename )
45+
{
46+
using namespace avtranscoder;
47+
48+
av_log_set_level( AV_LOG_FATAL );
49+
av_log_set_level( AV_LOG_DEBUG );
50+
51+
InputFile inputFile( inputfilename );
52+
inputFile.analyse();
53+
54+
// init audio decoders
55+
InputStreamAudio inputStreamAudio( inputFile.getStream( 0 ) );
56+
57+
OutputFile outputFile( outputFilename );
58+
59+
outputFile.setup();
60+
61+
outputFile.addAudioStream( inputFile.getStream( 0 ).getAudioDesc() );
62+
63+
DataStream data;
64+
65+
// Transcoding
66+
std::cout << "start transcoding" << std::endl;
67+
68+
size_t frame = 0;
69+
AudioFrameDesc audioFrameDesc;
70+
71+
AudioFrame audioFrame( audioFrameDesc );
72+
73+
74+
while( inputStreamAudio.readNextFrame( audioFrame ) )
75+
{
76+
std::cout << "\rprocess frame " << (int)frame - 1 << std::flush;
77+
78+
// outputFile.wrap( data, 0 );
79+
80+
++frame;
81+
}
82+
std::cout << std::endl;
83+
}
84+
85+
int main( int argc, char** argv )
86+
{
87+
if( argc <= 2 )
88+
{
89+
std::cout << "audiorewrapper require medias filename" << std::endl;
90+
std::cout << "example: audioWrap inputfile.ext outputfile.ext" << std::endl;
91+
return( -1 );
92+
}
93+
94+
std::cout << "start ..." << std::endl;
95+
96+
try
97+
{
98+
//rewrapAudio( argv[1], argv[2] );
99+
transcodeAudio( argv[1], argv[2] );
100+
}
101+
catch( std::exception &e )
102+
{
103+
std::cout << "[ERROR] " << e.what() << std::endl;
104+
}
105+
std::cout << "end ..." << std::endl;
106+
}

app/avTranscoder/avTranscoder.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
2929
InputStreamVideo inputStreamVideo( inputFile.getStream( 0 ) );
3030

3131
// init audio decoders
32-
InputStreamAudio inputStreamAudio( inputFile.getStream( 1 ) );
32+
//InputStreamAudio inputStreamAudio( inputFile.getStream( 1 ) );
3333
//InputStreamAudio inputStreamAudioRight( inputFile.getStream( 2 ) );
3434

3535
// init video & audio encoders
@@ -73,15 +73,13 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
7373
exit( -1 );
7474
}
7575

76-
DataStreamDesc dataStreamDesc;
77-
7876
Image imageToEncode( sourceImage );
79-
DataStream codedImage( dataStreamDesc );
77+
DataStream codedImage;
8078

8179

82-
OutputStreamAudio osAudioLeft ( ); // "AudioStreamEncoder" / "AudioOutputStream" ?
83-
OutputStreamAudio osAudioRight( );
84-
OutputStreamAudio osAudioLfe ( );
80+
// OutputStreamAudio osAudioLeft ( ); // "AudioStreamEncoder" / "AudioOutputStream" ?
81+
// OutputStreamAudio osAudioRight( );
82+
// OutputStreamAudio osAudioLfe ( );
8583

8684
// setup wrapper
8785
OutputFile of( outputFilename ); // "Format" ? to keep libav naming
@@ -108,8 +106,8 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
108106
109107
wrapper.createAudioEncoder( eAudioLeft, 2 );*/
110108

111-
AudioFrameDesc audioFrameDesc;
112-
AudioFrame sourceAudio( audioFrameDesc );
109+
// AudioFrameDesc audioFrameDesc;
110+
// AudioFrame sourceAudio( audioFrameDesc );
113111

114112

115113

@@ -121,7 +119,7 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
121119

122120
size_t frame = 0;
123121

124-
while( inputStreamVideo.readNextFrame( sourceImage ) && inputStreamAudio.readNextFrame( sourceAudio ) )
122+
while( inputStreamVideo.readNextFrame( sourceImage ) )
125123
{
126124
std::cout << "\rprocess frame " << frame << std::flush;
127125

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
#include <AvTranscoder/Transcoder.hpp>
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <fstream>
7+
#include <sstream>
8+
#include <cstdlib>
9+
10+
11+
void parseConfigFile( const std::string& configFilename, avtranscoder::Transcoder::StreamsDefinition& streams )
12+
{
13+
std::ifstream configFile( configFilename.c_str(), std::ifstream::in );
14+
15+
std::string line;
16+
while( std::getline( configFile, line ) )
17+
{
18+
std::istringstream is_line( line );
19+
std::string filename;
20+
if( std::getline( is_line, filename, '=' ) )
21+
{
22+
std::string streamId;
23+
if( std::getline( is_line, streamId ) )
24+
{
25+
std::cout << filename << "( " << streamId << " )" << std::endl;
26+
streams.push_back( std::pair< std::string, int >( filename, atoi( streamId.c_str() ) ) );
27+
}
28+
}
29+
}
30+
31+
configFile.close();
32+
}
33+
34+
int main( int argc, char** argv )
35+
{
36+
if( argc != 3 )
37+
{
38+
std::cout << "avprocessor require an input config file and an output media filename" << std::endl;
39+
return( -1 );
40+
}
41+
42+
av_log_set_level( AV_LOG_FATAL );
43+
44+
try
45+
{
46+
std::cout << "start ..." << std::endl;
47+
48+
std::string inputConfigFile( argv[1] );
49+
std::string outputFile( argv[2] );
50+
51+
avtranscoder::Transcoder::StreamsDefinition streams;
52+
53+
parseConfigFile( inputConfigFile, streams );
54+
55+
avtranscoder::Transcoder transcoder( outputFile );
56+
57+
transcoder.add( streams );
58+
59+
// video re-wrapping or transcoding if necessary
60+
transcoder.process();
61+
62+
std::cout << "end ..." << std::endl;
63+
}
64+
catch( std::exception& e )
65+
{
66+
std::cerr << "ERROR: during process, an error occured:" << std::endl << e.what() << std::endl;
67+
}
68+
69+
}

src/AvTranscoder/DatasStructures/AudioDesc.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ void AudioDesc::setAudioCodec( const AVCodecID codecId )
4949
initCodecContext();
5050
}
5151

52+
void AudioDesc::setAudioParameters( const size_t sampleRate, const size_t channels, const AVSampleFormat& sampleFormat )
53+
{
54+
m_codecContext->sample_rate = sampleRate;
55+
m_codecContext->channels = channels;
56+
m_codecContext->sample_fmt = sampleFormat;
57+
}
58+
5259
void AudioDesc::initCodecContext( )
5360
{
5461
if( m_codec == NULL )

src/AvTranscoder/DatasStructures/AudioDesc.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class AvExport AudioDesc
2727
void setAudioCodec( const std::string& codecName );
2828
void setAudioCodec( const AVCodecID codecId );
2929

30+
void setAudioParameters( const size_t sampleRate, const size_t channels, const AVSampleFormat& sampleFormat );
31+
3032
void set( const std::string& key, const std::string& flag, const bool enable );
3133
void set( const std::string& key, const bool value );
3234
void set( const std::string& key, const int value );

src/AvTranscoder/DatasStructures/DataStreamDesc.hpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,15 @@ extern "C" {
1818
namespace avtranscoder
1919
{
2020

21-
class DataStreamDesc
22-
{
23-
public:
24-
DataStreamDesc()
25-
{};
26-
27-
private:
28-
};
29-
30-
3121
class DataStream
3222
{
3323
public:
3424
typedef std::vector< unsigned char > DataBuffer;
3525

36-
37-
DataStream( const DataStreamDesc& ref )
26+
DataStream( )
3827
: m_dataBuffer( 0, 0 )
39-
, m_dataStreamDesc( ref )
4028
{ }
4129

42-
const DataStreamDesc& desc() const { return m_dataStreamDesc; }
4330
DataBuffer& getBuffer() { return m_dataBuffer; }
4431
unsigned char* getPtr() { return &m_dataBuffer[0]; }
4532
#ifndef SWIG
@@ -49,7 +36,6 @@ class DataStream
4936

5037
private:
5138
DataBuffer m_dataBuffer;
52-
const DataStreamDesc m_dataStreamDesc;
5339
};
5440

5541
}

src/AvTranscoder/DatasStructures/VideoDesc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void VideoDesc::initCodecContext( )
9595
{
9696
if( m_codec == NULL )
9797
{
98-
throw std::runtime_error( "unknown audio codec" );
98+
throw std::runtime_error( "unknown video codec" );
9999
}
100100

101101
if( ( m_codecContext = avcodec_alloc_context3( m_codec ) ) == NULL )

src/AvTranscoder/InputFile.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ InputFile::InputFile( const std::string& filename )
3232
av_register_all(); // Warning: should be called only once
3333
if( avformat_open_input( &m_formatContext, m_filename.c_str(), NULL, NULL ) < 0 )
3434
{
35-
throw std::runtime_error( "unable to open file" );
35+
std::string msg = "unable to open file: ";
36+
msg += m_filename;
37+
throw std::runtime_error( msg );
3638
}
3739

3840
// update format context informations from streams
@@ -123,6 +125,13 @@ InputFile& InputFile::analyse()
123125
return *this;
124126
}
125127

128+
AVMediaType InputFile::getStreamType( size_t index )
129+
{
130+
if( index >= m_formatContext->nb_streams )
131+
return AVMEDIA_TYPE_UNKNOWN;
132+
return m_formatContext->streams[index]->codec->codec_type;
133+
}
134+
126135
InputStream& InputFile::getStream( size_t index )
127136
{
128137
return m_inputStreams.at( index );

src/AvTranscoder/InputFile.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class AvExport InputFile
3232
/// get file properties
3333
const Properties& getProperties() const { return m_properties; }
3434

35+
AVMediaType getStreamType( size_t index );
36+
3537
::avtranscoder::InputStream& getStream( size_t index );
3638

3739
protected:

0 commit comments

Comments
 (0)