Get metadata for the currently open file.
281{
283
285 return std::nullopt;
286 }
287
288 {
292 }
293 }
294
295 FileMetadata metadata;
297
298 metadata.format = ctx->format_context->iformat->name;
299 metadata.mime_type = ctx->format_context->iformat->mime_type
300 ? ctx->format_context->iformat->mime_type
301 : "audio/" + std::string(ctx->format_context->iformat->name);
302
303 metadata.file_size = std::filesystem::file_size(
m_filepath);
304 auto ftime = std::filesystem::last_write_time(
m_filepath);
305 metadata.modification_time = std::chrono::system_clock::time_point(
306 std::chrono::seconds(std::chrono::duration_cast<std::chrono::seconds>(
307 ftime.time_since_epoch())));
308
309 metadata.attributes["codec"] = avcodec_get_name(ctx->codec_context->codec_id);
310 metadata.attributes["codec_long_name"] = ctx->codec_context->codec->long_name;
311 metadata.attributes["total_frames"] = ctx->total_frames;
312 metadata.attributes["sample_rate"] = ctx->sample_rate;
313 metadata.attributes["channels"] = ctx->channels;
314
315 char layout_desc[256];
316 av_channel_layout_describe(&ctx->codec_context->ch_layout, layout_desc, sizeof(layout_desc));
317 metadata.attributes["channel_layout"] = std::string(layout_desc);
318 metadata.attributes["bit_rate"] = ctx->codec_context->bit_rate;
319
320 if (ctx->format_context->duration != AV_NOPTS_VALUE) {
321 metadata.attributes["duration_seconds"] = ctx->format_context->duration / static_cast<double>(AV_TIME_BASE);
322 } else if (ctx->total_frames > 0) {
323 metadata.attributes["duration_seconds"] = ctx->total_frames / static_cast<double>(ctx->sample_rate);
324 }
325
326 AVDictionaryEntry* tag = nullptr;
327 while ((tag = av_dict_get(ctx->format_context->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
328 metadata.attributes[std::string("tag_") + tag->key] = tag->value;
329 }
330
331 AVStream* stream = ctx->format_context->streams[ctx->audio_stream_index];
332 tag = nullptr;
333 while ((tag = av_dict_get(stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
334 metadata.attributes[std::string("stream_") + tag->key] = tag->value;
335 }
336
337 {
340 }
341
342 return metadata;
343}
std::shared_mutex m_context_mutex
std::mutex m_metadata_mutex
Mutex for thread-safe metadata access.
std::shared_ptr< FFmpegContext > m_context
std::optional< FileMetadata > m_cached_metadata
Cached file metadata.
std::string m_filepath
Path to the currently open file.