Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] ffmpeg: Carry streamid as metadata key 'id'
@ 2024-04-12  9:40 Tomas Härdin
  2024-04-12 23:25 ` Michael Niedermayer
  2024-04-16 12:38 ` Anton Khirnov
  0 siblings, 2 replies; 11+ messages in thread
From: Tomas Härdin @ 2024-04-12  9:40 UTC (permalink / raw)
  To: ffmpeg-devel

[-- Attachment #1: Type: text/plain, Size: 567 bytes --]

This idea could be extended to other fields not presently considered to
be metadata, that would be handy to treat as such.

I use the key "id" because ffprobe outputs id= for streamid. Another
option could be to collect these types of metadata that go into
AVStream fields under a namespace like FFMPEG: or AVSTREAM: or
something, then delete all of them using AV_DICT_IGNORE_SUFFIX near the
end of of_open() since they're for internal ffmpeg use.

The FATE change is just because av_dict() changes the order of things
when elements are deleted.

/Tomas

[-- Attachment #2: 0001-ffmpeg-Carry-streamid-as-metadata-key-id.patch --]
[-- Type: text/x-patch, Size: 4888 bytes --]

From 7799f1b2eb8ab02e58118565f3e889fbe0d568a7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Fri, 12 Apr 2024 10:34:12 +0200
Subject: [PATCH] ffmpeg: Carry streamid as metadata key 'id'

This allows using -map_metadata and -metadata to copy/set streamids (PIDs).
---
 fftools/ffmpeg_demux.c              |  5 +++
 fftools/ffmpeg_mux_init.c           | 56 +++++++++++++++++++++--------
 tests/ref/fate/matroska-stereo_mode |  6 ++--
 3 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index cba63dab5f..1b0ef91abb 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -1454,6 +1454,11 @@ static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
 
     ds->codec_desc = avcodec_descriptor_get(ist->par->codec_id);
 
+    // carry streamid as metadata so that streamids can be be handled like any other metadata
+    // be careful not to overwrite any existing value, just in case
+    if ((ret = av_dict_set_int(&st->metadata, "id", st->id, AV_DICT_DONT_OVERWRITE)) < 0)
+        av_log(ist, AV_LOG_ERROR, "error %i setting \"id\" to %i. id already set?\n", ret, st->id);
+
     return 0;
 }
 
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 6d8bd5bcdf..dbe16e8d0f 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -1073,21 +1073,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
 
     ost = &ms->ost;
 
-    if (o->streamid) {
-        AVDictionaryEntry *e;
-        char idx[16], *p;
-        snprintf(idx, sizeof(idx), "%d", ost->index);
-
-        e = av_dict_get(o->streamid, idx, NULL, 0);
-        if (e) {
-            st->id = strtol(e->value, &p, 0);
-            if (!e->value[0] || *p) {
-                av_log(ost, AV_LOG_FATAL, "Invalid stream id: %s\n", e->value);
-                return AVERROR(EINVAL);
-            }
-        }
-    }
-
     ost->par_in = avcodec_parameters_alloc();
     if (!ost->par_in)
         return AVERROR(ENOMEM);
@@ -3016,6 +3001,43 @@ static Muxer *mux_alloc(void)
     return mux;
 }
 
+static int of_set_streamid(Muxer *mux, const OptionsContext *o)
+{
+    for (int i = 0; i < mux->fc->nb_streams; i++) {
+        OutputStream *ost = mux->of.streams[i];
+        AVDictionaryEntry *e = NULL;
+
+        // take -streamid if explicitly set
+        if (o->streamid) {
+            char idx[16];
+            snprintf(idx, sizeof(idx), "%d", ost->index);
+
+            e = av_dict_get(o->streamid, idx, NULL, 0);
+        }
+
+        // if -streamid not set then try to grab it from metadata
+        // this maintains backward compatibility
+        if (!e)
+            e = av_dict_get(ost->st->metadata, "id", NULL, 0);
+
+        if (e) {
+            char *p;
+            ost->st->id = strtol(e->value, &p, 0);
+            if (!e->value[0] || *p) {
+                av_log(ost, AV_LOG_FATAL, "Invalid stream id: %s\n", e->value);
+                return AVERROR(EINVAL);
+            } else {
+                // delete id now that we've picked it up,
+                // so that it doesn't make it into the output
+                int ret = av_dict_set(&ost->st->metadata, "id", NULL, 0);
+                if (ret < 0)
+                    return ret;
+            }
+        }
+    }
+    return 0;
+}
+
 int of_open(const OptionsContext *o, const char *filename, Scheduler *sch)
 {
     Muxer *mux;
@@ -3149,6 +3171,10 @@ int of_open(const OptionsContext *o, const char *filename, Scheduler *sch)
     if (err < 0)
         return err;
 
+    err = of_set_streamid(mux, o);
+    if (err < 0)
+        return err;
+
     err = set_dispositions(mux, o);
     if (err < 0) {
         av_log(mux, AV_LOG_FATAL, "Error setting output stream dispositions\n");
diff --git a/tests/ref/fate/matroska-stereo_mode b/tests/ref/fate/matroska-stereo_mode
index 739b789fea..e46f75cbb4 100644
--- a/tests/ref/fate/matroska-stereo_mode
+++ b/tests/ref/fate/matroska-stereo_mode
@@ -1,4 +1,4 @@
-a7a220a77001e81685ec807ce5ac3bc6 *tests/data/fate/matroska-stereo_mode.matroska
+40d2771cf74d378476cc4764b87af156 *tests/data/fate/matroska-stereo_mode.matroska
 1470764 tests/data/fate/matroska-stereo_mode.matroska
 #extradata 0:     3510, 0x560c3919
 #extradata 1:     3510, 0x560c3919
@@ -125,8 +125,8 @@ DISPOSITION:dub=0
 DISPOSITION:original=0
 TAG:language=ger-at
 TAG:stereo_mode=left_right
-TAG:DESCRIPTION-ger=Deutsch
 TAG:DESCRIPTION-fre=Francais
+TAG:DESCRIPTION-ger=Deutsch
 TAG:DURATION=00:00:10.000000000
 [SIDE_DATA]
 side_data_type=Stereo 3D
@@ -140,8 +140,8 @@ DISPOSITION:dub=0
 DISPOSITION:original=0
 TAG:language=eng
 TAG:stereo_mode=bottom_top
-TAG:DESCRIPTION-ger=Deutsch
 TAG:DESCRIPTION-fre=Francais
+TAG:DESCRIPTION-ger=Deutsch
 TAG:DURATION=00:00:10.000000000
 [SIDE_DATA]
 side_data_type=Stereo 3D
-- 
2.39.2


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-07-01 14:58 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-12  9:40 [FFmpeg-devel] [PATCH] ffmpeg: Carry streamid as metadata key 'id' Tomas Härdin
2024-04-12 23:25 ` Michael Niedermayer
2024-04-15  8:35   ` Tomas Härdin
2024-04-16  0:30     ` Michael Niedermayer
2024-04-16  0:33       ` James Almer
2024-04-16 14:00         ` Tomas Härdin
2024-04-16 12:38 ` Anton Khirnov
2024-04-16 12:52   ` James Almer
2024-04-16 14:12     ` Tomas Härdin
2024-07-01 14:51       ` Anton Khirnov
2024-07-01 14:58         ` Tomas Härdin

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git