Skip to content

Commit d7c7cad

Browse files
author
Clement Champetier
committed
Transcoder: transcode stream if user wants to demultiplexing and copy a stream
* Same behavior as ffmpeg: transcode with the same "profile" as input. * Need to update 'sampleFormat' of AudioStreamProperty: use av_get_sample_fmt_name (ffmpeg function insted of creating our specific strings).
1 parent 3fe0cf4 commit d7c7cad

File tree

2 files changed

+58
-23
lines changed

2 files changed

+58
-23
lines changed

src/AvTranscoder/mediaProperty/AudioStreamProperty.hpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,9 @@ avtranscoder::AudioProperties audioStreamInfo( const AVFormatContext* formatCont
4343
if( channelDescription )
4444
ap.channelDescription = std::string( channelDescription );
4545
#endif
46-
47-
std::string sampleFormat = "";
48-
switch( codec_context->sample_fmt )
49-
{
50-
case AV_SAMPLE_FMT_NONE : ap.sampleFormat = "none"; break;
51-
case AV_SAMPLE_FMT_U8 : ap.sampleFormat = "unsigned 8 bits"; break;
52-
case AV_SAMPLE_FMT_S16 : ap.sampleFormat = "signed 16 bits"; break;
53-
case AV_SAMPLE_FMT_S32 : ap.sampleFormat = "signed 32 bits"; break;
54-
case AV_SAMPLE_FMT_FLT : ap.sampleFormat = "float"; break;
55-
case AV_SAMPLE_FMT_DBL : ap.sampleFormat = "double"; break;
56-
case AV_SAMPLE_FMT_U8P : ap.sampleFormat = "unsigned 8 bits, planar"; break;
57-
case AV_SAMPLE_FMT_S16P : ap.sampleFormat = "signed 16 bits, planar"; break;
58-
case AV_SAMPLE_FMT_S32P : ap.sampleFormat = "signed 32 bits, planar"; break;
59-
case AV_SAMPLE_FMT_FLTP : ap.sampleFormat = "float, planar"; break;
60-
case AV_SAMPLE_FMT_DBLP : ap.sampleFormat = "double, planar"; break;
61-
case AV_SAMPLE_FMT_NB : ap.sampleFormat = "Number of sample formats."; break;
62-
}
46+
const char* fmtName = av_get_sample_fmt_name( codec_context->sample_fmt );
47+
if( fmtName )
48+
ap.sampleFormat = std::string( fmtName );
6349

6450
return ap;
6551
}

src/AvTranscoder/transcoder/Transcoder.cpp

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include "Transcoder.hpp"
22

3+
#include <AvTranscoder/progress/NoDisplayProgress.hpp>
4+
35
#include <limits>
46
#include <iostream>
57
#include <algorithm>
8+
#include <sstream>
69

710
namespace avtranscoder
811
{
@@ -83,9 +86,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, Pro
8386

8487
// Check filename
8588
if( ! filename.length() )
86-
{
8789
throw std::runtime_error( "Can't transcode a stream without filename indicated" );
88-
}
8990

9091
if( _verbose )
9192
std::cout << "Add transcoded stream" << std::endl;
@@ -132,8 +133,33 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
132133
addRewrapStream( filename, streamIndex );
133134
return;
134135
}
136+
// Transcode (transparent for the user)
135137
else
136-
throw std::runtime_error( "Can't demultiplexing and re-wrap a stream" );
138+
{
139+
if( _verbose )
140+
std::cout << "Add transcoded stream (because of demultiplexing)" << std::endl;
141+
// Create profile as input configuration
142+
InputFile inputFile( filename );
143+
NoDisplayProgress progress;
144+
inputFile.analyse( progress, InputFile::eAnalyseLevelFast );
145+
AudioProperties audioProperties = inputFile.getProperties().audioStreams.at( streamIndex ); // Warning: only manage audio case
146+
147+
ProfileLoader::Profile profile;
148+
profile[ constants::avProfileIdentificator ] = "presetRewrap";
149+
profile[ constants::avProfileIdentificatorHuman ] = "Preset rewrap";
150+
profile[ constants::avProfileType ] = avtranscoder::constants::avProfileTypeAudio;
151+
profile[ constants::avProfileCodec ] = audioProperties.codecName;
152+
profile[ constants::avProfileSampleFormat ] = audioProperties.sampleFormat;
153+
std::stringstream ss;
154+
ss << audioProperties.sampleRate;
155+
profile[ constants::avProfileSampleRate ] = ss.str();
156+
profile[ constants::avProfileChannel ] = "1";
157+
158+
// Add profile
159+
_profileLoader.update( profile );
160+
161+
add( filename, streamIndex, subStreamIndex, profile, offset );
162+
}
137163
}
138164
// Transcode
139165
else
@@ -163,8 +189,33 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
163189
addRewrapStream( filename, streamIndex );
164190
return;
165191
}
192+
// Transcode (transparent for the user)
166193
else
167-
throw std::runtime_error( "Can't demultiplexing and re-wrap a stream" );
194+
{
195+
if( _verbose )
196+
std::cout << "Add transcoded stream (because of demultiplexing)" << std::endl;
197+
// Create profile as input configuration
198+
InputFile inputFile( filename );
199+
NoDisplayProgress progress;
200+
inputFile.analyse( progress, InputFile::eAnalyseLevelFast );
201+
AudioProperties audioProperties = inputFile.getProperties().audioStreams.at( streamIndex ); // Warning: only manage audio case
202+
203+
ProfileLoader::Profile profile;
204+
profile[ constants::avProfileIdentificator ] = "presetRewrap";
205+
profile[ constants::avProfileIdentificatorHuman ] = "Preset rewrap";
206+
profile[ constants::avProfileType ] = avtranscoder::constants::avProfileTypeAudio;
207+
profile[ constants::avProfileCodec ] = audioProperties.codecName;
208+
profile[ constants::avProfileSampleFormat ] = audioProperties.sampleFormat;
209+
std::stringstream ss;
210+
ss << audioProperties.sampleRate;
211+
profile[ constants::avProfileSampleRate ] = ss.str();
212+
profile[ constants::avProfileChannel ] = "1";
213+
214+
// Add profile
215+
_profileLoader.update( profile );
216+
217+
add( filename, streamIndex, subStreamIndex, profile, offset );
218+
}
168219
}
169220
// Transcode
170221
else
@@ -188,9 +239,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
188239

189240
// Check filename
190241
if( ! filename.length() )
191-
{
192242
throw std::runtime_error( "Can't transcode a stream without filename indicated" );
193-
}
194243

195244
if( _verbose )
196245
std::cout << "Add transcoded for substream " << subStreamIndex << std::endl;

0 commit comments

Comments
 (0)