Skip to content

Commit

Permalink
Added audio track counting and selection through options.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggarra13 committed Mar 25, 2024
1 parent 2bccb56 commit 97d76f4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/tlCore/Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ namespace tl
size_t channelCount = 0;
DataType dataType = DataType::None;
size_t sampleRate = 0;
unsigned trackCount = 1;

//! Is the audio valid?
bool isValid() const;
Expand Down
6 changes: 6 additions & 0 deletions lib/tlIO/FFmpegRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ namespace tl
std::stringstream ss(i->second);
ss >> p.options.audioConvertInfo.sampleRate;
}
i = options.find("FFmpeg/AudioTrack");
if (i != options.end())
{
std::stringstream ss(i->second);
ss >> p.options.audioTrack;
}
i = options.find("FFmpeg/ThreadCount");
if (i != options.end())
{
Expand Down
50 changes: 46 additions & 4 deletions lib/tlIO/FFmpegReadAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,56 @@ namespace tl
{
throw std::runtime_error(string::Format("{0}: {1}").arg(fileName).arg(getErrorLabel(r)));
}

// Count the tracks
int trackCount = 0;
for (unsigned int i = 0; i < _avFormatContext->nb_streams; ++i)
{
if (AVMEDIA_TYPE_AUDIO == _avFormatContext->streams[i]->codecpar->codec_type &&
AV_DISPOSITION_DEFAULT == _avFormatContext->streams[i]->disposition)
if (AVMEDIA_TYPE_AUDIO ==
_avFormatContext->streams[i]->codecpar->codec_type)
{
_avStream = i;
break;
if (options.audioTrack == trackCount)
{
_avStream = i;
}
++trackCount;
}
}

// If user selected specific track, use it.
if (options.audioTrack >= 0)
{
int idx = 0;
for (unsigned int i = 0; i < _avFormatContext->nb_streams; ++i)
{
if (AVMEDIA_TYPE_AUDIO ==
_avFormatContext->streams[i]->codecpar->codec_type)
{
if (options.audioTrack == idx)
{
_avStream = i;
break;
}
++idx;
}
}
}

// Else, use the disposition track.
if (-1 == _avStream)
{
for (unsigned int i = 0; i < _avFormatContext->nb_streams; ++i)
{
if (AVMEDIA_TYPE_AUDIO == _avFormatContext->streams[i]->codecpar->codec_type &&
AV_DISPOSITION_DEFAULT == _avFormatContext->streams[i]->disposition)
{
_avStream = i;
break;
}
}
}

// If all failed, use the first track we find.
if (-1 == _avStream)
{
for (unsigned int i = 0; i < _avFormatContext->nb_streams; ++i)
Expand Down Expand Up @@ -140,6 +181,7 @@ namespace tl
_info.channelCount = channelCount;
_info.dataType = dataType;
_info.sampleRate = sampleRate;
_info.trackCount = trackCount;

int64_t sampleCount = 0;
if (avAudioStream->duration != AV_NOPTS_VALUE)
Expand Down
1 change: 1 addition & 0 deletions lib/tlIO/FFmpegReadPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace tl
otime::RationalTime startTime = time::invalidTime;
bool yuvToRGBConversion = false;
audio::Info audioConvertInfo;
int audioTrack = -1;
size_t threadCount = ffmpeg::threadCount;
size_t requestTimeout = 5;
size_t videoBufferSize = 4;
Expand Down

0 comments on commit 97d76f4

Please sign in to comment.