Skip to content

Commit 5f3d7ee

Browse files
Setup output streams for audio
1 parent c937ad1 commit 5f3d7ee

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/AvTranscoder/OutputFile.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,25 @@ void OutputFile::addVideoStream( const VideoDesc& videoDesc )
8686
}
8787
}
8888

89-
bool OutputFile::addAudioStream( )
89+
void OutputFile::addAudioStream( const AudioDesc& audioDesc )
9090
{
91-
if( formatContext == NULL )
92-
return false;
93-
94-
av_register_all(); // Warning: should be called only once
95-
AVCodec* codec = NULL;
96-
AVStream* stream = NULL;
97-
98-
if( ( codec = avcodec_find_encoder_by_name( "wav" ) ) == NULL )
99-
return false;
91+
assert( formatContext != NULL );
10092

101-
if( ( stream = avformat_new_stream( formatContext, codec ) ) == NULL )
102-
return false;
93+
if( ( stream = avformat_new_stream( formatContext, audioDesc.getCodec() ) ) == NULL )
94+
{
95+
throw std::runtime_error( "unable to add new audio stream" );
96+
}
10397

104-
//stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
98+
stream->codec->sample_rate = audioDesc.getCodecContext()->sample_rate;
99+
stream->codec->channels = audioDesc.getCodecContext()->channels;
100+
stream->codec->bit_rate = audioDesc.getCodecContext()->bit_rate;
101+
stream->codec->sample_fmt = audioDesc.getCodecContext()->sample_fmt;
105102

106-
return true;
103+
// to move in endSetup
104+
if( avformat_write_header( formatContext, NULL ) != 0 )
105+
{
106+
throw std::runtime_error( "could not write header" );
107+
}
107108
}
108109

109110
bool OutputFile::wrap( const DataStream& data, const size_t streamId )

src/AvTranscoder/OutputFile.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "DatasStructures/Image.hpp"
55
#include "DatasStructures/DataStreamDesc.hpp"
66
#include "DatasStructures/VideoDesc.hpp"
7+
#include "DatasStructures/AudioDesc.hpp"
78

89
#include <string>
910
#include <vector>
@@ -25,8 +26,7 @@ class AvExport OutputFile
2526
bool setup();
2627

2728
void addVideoStream( const VideoDesc& videoDesc );
28-
29-
bool addAudioStream( );
29+
void addAudioStream( const AudioDesc& audioDesc );
3030

3131
bool wrap( const DataStream& data, const size_t streamId );
3232

0 commit comments

Comments
 (0)