Skip to content

Instantly share code, notes, and snippets.

@core-code
Created November 23, 2016 17:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save core-code/027bab78a24e0b937a2a6561ea8a8ab9 to your computer and use it in GitHub Desktop.
Save core-code/027bab78a24e0b937a2a6561ea8a8ab9 to your computer and use it in GitHub Desktop.
static void enable_tracks(AVFormatContext *s)
{
MOVMuxContext *mov = s->priv_data;
int i;
int enabled[AVMEDIA_TYPE_NB];
int first[AVMEDIA_TYPE_NB];
for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
enabled[i] = 0;
first[i] = -1;
}
for (i = 0; i < s->nb_streams; i++) {
AVStream *st = s->streams[i];
if (st->codecpar->codec_type <= AVMEDIA_TYPE_UNKNOWN ||
st->codecpar->codec_type >= AVMEDIA_TYPE_NB)
continue;
// note the first stream number for each type
if (first[st->codecpar->codec_type] < 0)
first[st->codecpar->codec_type] = i;
// note whether each media type has any enabled streams
if (st->disposition & AV_DISPOSITION_DEFAULT)
enabled[st->codecpar->codec_type] = 1;
// clear the enables for each stream and track
st->disposition &= ~AV_DISPOSITION_DEFAULT;
mov->tracks[i].flags &= ~MOV_TRACK_ENABLED;
}
// for each media type, if no stream was explicitly enabled
// then enable the first one found for that type
for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
switch (i) {
case AVMEDIA_TYPE_VIDEO:
case AVMEDIA_TYPE_AUDIO:
case AVMEDIA_TYPE_SUBTITLE:
if (!enabled[i] && first[i] >= 0)
enabled[i] = 1;
break;
}
}
// translate the first enabled stream of each media type
// into its stream being default and its output track being enabled
for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
switch (i) {
case AVMEDIA_TYPE_VIDEO:
case AVMEDIA_TYPE_AUDIO:
case AVMEDIA_TYPE_SUBTITLE:
if (enabled[i]) {
s->streams[first[i]]->disposition |= AV_DISPOSITION_DEFAULT;
mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment