FFmpeg: Handle multiple attached pictures properly

Now it handles multiple attached pictures and tries to pick out the one
which may be the front cover picture, or otherwise picks the first one.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
This commit is contained in:
Christopher Snowhill 2025-03-09 20:06:41 -07:00
parent b0fd359388
commit 0a849fc1d9

View file

@ -192,6 +192,8 @@ static uint8_t reverse_bits[0x100];
attachedPicIndex = -1;
AVCodecParameters *codecPar;
NSMutableArray *pictures = [[NSMutableArray alloc] init];
for(i = 0; i < formatCtx->nb_streams; i++) {
stream = formatCtx->streams[i];
codecPar = stream->codecpar;
@ -201,7 +203,7 @@ static uint8_t reverse_bits[0x100];
} else if(codecPar->codec_id == AV_CODEC_ID_TIMED_ID3) {
metadataIndex = i;
} else if(stream->disposition & AV_DISPOSITION_ATTACHED_PIC) {
attachedPicIndex = i;
[pictures addObject:@(i)];
} else {
stream->discard = AVDISCARD_ALL;
}
@ -212,6 +214,31 @@ static uint8_t reverse_bits[0x100];
return NO;
}
if([pictures count]) {
if([pictures count] == 1) {
attachedPicIndex = [pictures[0] intValue];
} else {
// Find the first attached picture with "front" or "cover" in its filename
for(NSNumber *picture in pictures) {
i = [picture intValue];
stream = formatCtx->streams[i];
AVDictionary *metadata = stream->metadata;
AVDictionaryEntry *filename = av_dict_get(metadata, "filename", NULL, 0);
if(filename) {
if(strcasestr(filename->value, "front") ||
strcasestr(filename->value, "cover")) {
attachedPicIndex = i;
break;
}
}
}
// Or else find the first attached picture
if(attachedPicIndex < 0) {
attachedPicIndex = [pictures[0] intValue];
}
}
}
stream = formatCtx->streams[streamIndex];
codecPar = stream->codecpar;