Skip to content

Commit 40726ae

Browse files
committed
Add FFMS_GetTrackMetadata
1 parent adf56fb commit 40726ae

File tree

7 files changed

+52
-1
lines changed

7 files changed

+52
-1
lines changed

doc/ffms2-api.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,15 @@ In other words, does the same thing as [FFMS_GetTrackType][GetTrackType], but do
567567
If you have indexed the file, use [FFMS_GetTrackType][GetTrackType] instead since the `FFMS_Indexer` object is destructed when the index is created.
568568
Note that specifying an invalid track number may lead to undefined behavior.
569569
570+
### FFMS_GetTrackMetadata - gets the metadata of a given track
571+
572+
[GetTrackMetadata]: #ffms_gettrackmetadata---gets-the-metadata-of-a-given-track
573+
```c++
574+
const char *FFMS_GetTrackMetadata(FFMS_Track *T, const char *Key);
575+
```
576+
Returns the value of the metadata entry associated with `Key` for the given track `T`.
577+
For the list of commonly used metadata keys, see the [metadata_api Public Metadata API](https://code.ffmpeg.org/FFmpeg/FFmpeg/src/branch/master/libavformat/avformat.h).
578+
570579
### FFMS_GetCodecNameI - gets the name of the codec used for a given track
571580

572581
[GetCodecNameI]: #ffms_getcodecnamei---gets-the-name-of-the-codec-used-for-a-given-track

doc/ffms2-changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- FFmpeg 7.1 is now the minimum requirement.
44
- Added layered decoding support, for e.g. spatial MV-HEVC.
55
- Added LastEndPTS to FFMS_VideoProperties. This field is the equivalent of LastEndTime, but it is expressed in the video timebase.
6+
- Add FFMS_GetTrackMetadata.
67

78
- 5.0
89
- Fixed all issues with FFmpeg 6.1 which is now the minimum requirement

include/ffms.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ FFMS_API(int) FFMS_GetNumTracks(FFMS_Index *Index);
460460
FFMS_API(int) FFMS_GetNumTracksI(FFMS_Indexer *Indexer);
461461
FFMS_API(int) FFMS_GetTrackType(FFMS_Track *T);
462462
FFMS_API(int) FFMS_GetTrackTypeI(FFMS_Indexer *Indexer, int Track);
463+
FFMS_API(const char *) FFMS_GetTrackMetadata(FFMS_Track *T, const char *Key);
463464
FFMS_API(FFMS_IndexErrorHandling) FFMS_GetErrorHandling(FFMS_Index *Index);
464465
FFMS_API(const char *) FFMS_GetCodecNameI(FFMS_Indexer *Indexer, int Track);
465466
FFMS_API(const char *) FFMS_GetFormatNameI(FFMS_Indexer *Indexer);

src/core/ffms.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ FFMS_API(int) FFMS_GetTrackType(FFMS_Track *T) {
262262
return T->TT;
263263
}
264264

265+
FFMS_API(const char *) FFMS_GetTrackMetadata(FFMS_Track *T, const char *Key) {
266+
auto it = T->Metadata.find(Key);
267+
if (it == T->Metadata.end())
268+
return NULL;
269+
return it->second.c_str();
270+
}
271+
265272
FFMS_API(int) FFMS_GetTrackTypeI(FFMS_Indexer *Indexer, int Track) {
266273
return Indexer->GetTrackType(Track);
267274
}

src/core/indexing.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
extern "C" {
3333
#include <libavutil/avutil.h>
3434
#include <libavutil/sha.h>
35+
#include <libavutil/dict.h>
3536
}
3637

3738
#define INDEXID 0x53920873
38-
#define INDEX_VERSION 8
39+
#define INDEX_VERSION 9
3940

4041
SharedAVContext::~SharedAVContext() {
4142
avcodec_free_context(&CodecContext);
@@ -445,6 +446,13 @@ FFMS_Index *FFMS_Indexer::DoIndexing() {
445446
!!(FormatContext->iformat->flags & AVFMT_TS_DISCONT),
446447
UseDTS);
447448

449+
AVDictionary *StreamMetadata = FormatContext->streams[i]->metadata;
450+
if (StreamMetadata) {
451+
const AVDictionaryEntry *Entry = nullptr;
452+
while ((Entry = av_dict_iterate(StreamMetadata, Entry)))
453+
(*TrackIndices)[i].Metadata[Entry->key] = Entry->value;
454+
}
455+
448456
if (IndexMask.count(i) && FormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
449457
auto *VideoCodec = avcodec_find_decoder(FormatContext->streams[i]->codecpar->codec_id);
450458
if (!VideoCodec) {

src/core/track.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ FFMS_Track::FFMS_Track(ZipFile &stream)
9999
UseDTS = !!stream.Read<uint8_t>();
100100
HasTS = !!stream.Read<uint8_t>();
101101
HasDiscontTS = !!stream.Read<uint8_t>();
102+
uint32_t MetadataNumEntry = stream.Read<uint32_t>();
103+
std::vector<char> KeyBuffer;
104+
std::vector<char> ValueBuffer;
105+
for (uint32_t i = 0; i < MetadataNumEntry; i++) {
106+
uint32_t KeyLength = stream.Read<uint32_t>();
107+
KeyBuffer.resize(KeyLength);
108+
stream.Read(KeyBuffer.data(), KeyLength);
109+
110+
uint32_t ValueLength = stream.Read<uint32_t>();
111+
ValueBuffer.resize(ValueLength);
112+
stream.Read(ValueBuffer.data(), ValueLength);
113+
114+
Metadata[std::string(KeyBuffer.data(), KeyLength)] = std::string(ValueBuffer.data(), ValueLength);
115+
}
116+
102117
size_t FrameCount = static_cast<size_t>(stream.Read<uint64_t>());
103118

104119
if (!FrameCount) return;
@@ -122,6 +137,13 @@ void FFMS_Track::Write(ZipFile &stream) const {
122137
stream.Write<uint8_t>(UseDTS);
123138
stream.Write<uint8_t>(HasTS);
124139
stream.Write<uint8_t>(HasDiscontTS);
140+
stream.Write<uint32_t>(Metadata.size());
141+
for (const auto &iter : Metadata) {
142+
stream.Write<uint32_t>(iter.first.length());
143+
stream.Write(iter.first.data(), iter.first.length());
144+
stream.Write<uint32_t>(iter.second.length());
145+
stream.Write(iter.second.data(), iter.second.length());
146+
}
125147
stream.Write<uint64_t>(size());
126148

127149
if (empty()) return;

src/core/track.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <cstddef>
2727
#include <vector>
2828
#include <memory>
29+
#include <string>
30+
#include <map>
2931

3032
class ZipFile;
3133

@@ -72,6 +74,7 @@ struct FFMS_Track {
7274
bool HasDiscontTS = false;
7375
int64_t LastDuration = 0;
7476
int SampleRate = 0; // not persisted
77+
std::map<std::string, std::string> Metadata;
7578

7679
void AddVideoFrame(int64_t PTS, int64_t DTS, int RepeatPict, bool KeyFrame, int FrameType, int64_t FilePos = 0, bool Invisible = false, bool SecondField = false);
7780
void AddAudioFrame(int64_t PTS, int64_t DTS, int64_t SampleStart, uint32_t SampleCount, bool KeyFrame, int64_t FilePos = 0, bool Invisible = false);

0 commit comments

Comments
 (0)