MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ extract_metadata()

void MayaFlux::IO::SoundFileReader::extract_metadata ( const std::shared_ptr< FFmpegContext > &  ctx)
private

Extract metadata from the file.

Definition at line 345 of file SoundFileReader.cpp.

346{
347 if (!ctx || !ctx->is_valid()) {
348 return;
349 }
350
351 std::lock_guard<std::mutex> meta_lock(m_metadata_mutex);
352
353 FileMetadata metadata;
354
355 metadata.format = ctx->format_context->iformat->name;
356 metadata.mime_type = ctx->format_context->iformat->mime_type
357 ? ctx->format_context->iformat->mime_type
358 : "audio/" + std::string(ctx->format_context->iformat->name);
359
360 metadata.file_size = std::filesystem::file_size(m_filepath);
361 auto ftime = std::filesystem::last_write_time(m_filepath);
362 metadata.modification_time = std::chrono::system_clock::time_point(
363 std::chrono::seconds(std::chrono::duration_cast<std::chrono::seconds>(
364 ftime.time_since_epoch())));
365
366 metadata.attributes["codec"] = avcodec_get_name(ctx->codec_context->codec_id);
367 metadata.attributes["codec_long_name"] = ctx->codec_context->codec->long_name;
368 metadata.attributes["total_frames"] = ctx->total_frames;
369 metadata.attributes["sample_rate"] = ctx->sample_rate;
370 metadata.attributes["channels"] = ctx->channels;
371
372 char layout_desc[256];
373 av_channel_layout_describe(&ctx->codec_context->ch_layout, layout_desc, sizeof(layout_desc));
374 metadata.attributes["channel_layout"] = std::string(layout_desc);
375 metadata.attributes["bit_rate"] = ctx->codec_context->bit_rate;
376
377 if (ctx->format_context->duration != AV_NOPTS_VALUE) {
378 metadata.attributes["duration_seconds"] = ctx->format_context->duration / static_cast<double>(AV_TIME_BASE);
379 } else if (ctx->total_frames > 0) {
380 metadata.attributes["duration_seconds"] = ctx->total_frames / static_cast<double>(ctx->sample_rate);
381 }
382
383 AVDictionaryEntry* tag = nullptr;
384 while ((tag = av_dict_get(ctx->format_context->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
385 metadata.attributes[std::string("tag_") + tag->key] = tag->value;
386 }
387
388 AVStream* stream = ctx->format_context->streams[ctx->audio_stream_index];
389 tag = nullptr;
390 while ((tag = av_dict_get(stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
391 metadata.attributes[std::string("stream_") + tag->key] = tag->value;
392 }
393
394 m_cached_metadata = metadata;
395}
std::mutex m_metadata_mutex
Mutex for thread-safe metadata access.
std::optional< FileMetadata > m_cached_metadata
Cached file metadata.
std::string m_filepath
Path to the currently open file.

References MayaFlux::IO::FileMetadata::attributes, MayaFlux::IO::FileMetadata::file_size, MayaFlux::IO::FileMetadata::format, m_cached_metadata, m_filepath, m_metadata_mutex, MayaFlux::IO::FileMetadata::mime_type, and MayaFlux::IO::FileMetadata::modification_time.

Referenced by open().

+ Here is the caller graph for this function: