From c9f7a1d058ac6ba0ab552ebc43f85874302865ec Mon Sep 17 00:00:00 2001 From: xufulong Date: Mon, 28 Feb 2022 14:17:05 +0800 Subject: [PATCH] Add: metadata of pixel format --- .../cpp/metadata/ffmpeg_media_retriever.c | 1 + app/src/main/cpp/metadata/metadata_util.c | 21 ++++++++++++------- app/src/main/cpp/metadata/metadata_util.h | 2 ++ .../ffmpeg/activity/ProbeFormatActivity.kt | 4 ++++ .../ffmpeg/metadata/FFmpegMediaRetriever.java | 10 ++++++--- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/app/src/main/cpp/metadata/ffmpeg_media_retriever.c b/app/src/main/cpp/metadata/ffmpeg_media_retriever.c index 4dae289..7a4327f 100755 --- a/app/src/main/cpp/metadata/ffmpeg_media_retriever.c +++ b/app/src/main/cpp/metadata/ffmpeg_media_retriever.c @@ -199,6 +199,7 @@ int set_data_source_inner(State **state_ptr, const char* path) { set_file_size(state->pFormatCtx); set_frame_rate(state->pFormatCtx, state->video_st); set_sample_rate(state->pFormatCtx, state->audio_st); + set_pixel_format(state->pFormatCtx, state->video_st); set_channel_count(state->pFormatCtx, state->audio_st); set_channel_layout(state->pFormatCtx, state->audio_st); set_video_resolution(state->pFormatCtx, state->video_st); diff --git a/app/src/main/cpp/metadata/metadata_util.c b/app/src/main/cpp/metadata/metadata_util.c index 8953198..d880f8e 100755 --- a/app/src/main/cpp/metadata/metadata_util.c +++ b/app/src/main/cpp/metadata/metadata_util.c @@ -6,9 +6,10 @@ #include "metadata_util.h" #include +#include "include/libavutil/pixdesc.h" void set_duration(AVFormatContext *ic) { - char value[30] = "0"; + char value[20] = "0"; long duration = 0; if (ic) { @@ -22,7 +23,7 @@ void set_duration(AVFormatContext *ic) { } void set_file_size(AVFormatContext *ic) { - char value[30] = "0"; + char value[20] = "0"; int64_t size = ic->pb ? avio_size(ic->pb) : -1; sprintf(value, "%"PRId64, size); @@ -69,17 +70,23 @@ void set_channel_count(AVFormatContext *ic, AVStream *stream) { } void set_channel_layout(AVFormatContext *ic, AVStream *stream) { - char value[10] = "0"; + char value[20] = "0"; if (stream) { - av_get_channel_layout_string(value, 10, + av_get_channel_layout_string(value, 20, stream->codecpar->channels, stream->codecpar->channel_layout); av_dict_set(&ic->metadata, CHANNEL_LAYOUT, value, 0); } } -void set_video_resolution(AVFormatContext *ic, AVStream *video_st) { - char value[30] = "0"; +void set_pixel_format(AVFormatContext *ic, AVStream *stream) { + if (stream) { + const char *name = av_get_pix_fmt_name(stream->codecpar->format); + av_dict_set(&ic->metadata, PIXEL_FORMAT, name, 0); + } +} +void set_video_resolution(AVFormatContext *ic, AVStream *video_st) { + char value[20] = "0"; if (video_st) { sprintf(value, "%d", video_st->codecpar->width); av_dict_set(&ic->metadata, VIDEO_WIDTH, value, 0); @@ -102,7 +109,7 @@ void set_rotation(AVFormatContext *ic, AVStream *audio_st, AVStream *video_st) { } void set_frame_rate(AVFormatContext *ic, AVStream *video_st) { - char value[30] = "0"; + char value[20] = "0"; if (video_st && video_st->avg_frame_rate.den && video_st->avg_frame_rate.num) { double d = av_q2d(video_st->avg_frame_rate); diff --git a/app/src/main/cpp/metadata/metadata_util.h b/app/src/main/cpp/metadata/metadata_util.h index a8c34b7..dad069d 100755 --- a/app/src/main/cpp/metadata/metadata_util.h +++ b/app/src/main/cpp/metadata/metadata_util.h @@ -23,6 +23,7 @@ static const char *MIME_TYPE = "mime_type"; static const char *SAMPLE_RATE = "sample_rate"; static const char *CHANNEL_COUNT = "channel_count"; static const char *CHANNEL_LAYOUT = "channel_layout"; +static const char *PIXEL_FORMAT = "pixel_format"; static const int SUCCESS = 0; static const int FAILURE = -1; @@ -34,6 +35,7 @@ void set_codec(AVFormatContext *ic, int i); void set_sample_rate(AVFormatContext *ic, AVStream *stream); void set_channel_count(AVFormatContext *ic, AVStream *stream); void set_channel_layout(AVFormatContext *ic, AVStream *stream); +void set_pixel_format(AVFormatContext *ic, AVStream *stream); void set_video_resolution(AVFormatContext *ic, AVStream *video_st); void set_rotation(AVFormatContext *ic, AVStream *audio_st, AVStream *video_st); void set_frame_rate(AVFormatContext *ic, AVStream *video_st); diff --git a/app/src/main/java/com/frank/ffmpeg/activity/ProbeFormatActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/ProbeFormatActivity.kt index 5f65de0..7875abf 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/ProbeFormatActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/ProbeFormatActivity.kt @@ -177,6 +177,10 @@ class ProbeFormatActivity : BaseActivity() { if (channelLayout != null) resultBuilder.append("channelLayout:$channelLayout\n") + val pixelFormat = retriever.extractMetadata(FFmpegMediaRetriever.METADATA_KEY_PIXEL_FORMAT) + if (pixelFormat != null) + resultBuilder.append("pixelFormat:$pixelFormat\n") + mHandler.obtainMessage(MSG_INFO, resultBuilder.toString()).sendToTarget() // Retrieve frame with timeUs diff --git a/app/src/main/java/com/frank/ffmpeg/metadata/FFmpegMediaRetriever.java b/app/src/main/java/com/frank/ffmpeg/metadata/FFmpegMediaRetriever.java index c2b400b..b9ac574 100755 --- a/app/src/main/java/com/frank/ffmpeg/metadata/FFmpegMediaRetriever.java +++ b/app/src/main/java/com/frank/ffmpeg/metadata/FFmpegMediaRetriever.java @@ -313,16 +313,20 @@ public class FFmpegMediaRetriever { */ public static String METADATA_KEY_MIME_TYPE = "mime_type"; /** - * The metadata key to retrieve the mime type. + * The metadata key to retrieve sample rate. */ public static String METADATA_KEY_SAMPLE_RATE = "sample_rate"; /** - * The metadata key to retrieve the mime type. + * The metadata key to retrieve channel count. */ public static String METADATA_KEY_CHANNEL_COUNT = "channel_count"; /** - * The metadata key to retrieve the mime type. + * The metadata key to retrieve channel layout. */ public static String METADATA_KEY_CHANNEL_LAYOUT = "channel_layout"; + /** + * The metadata key to retrieve pixel format. + */ + public static String METADATA_KEY_PIXEL_FORMAT = "pixel_format"; }