[FFmpeg-devel] [PATCH] avformat/rtsp: Add -first_rtcp_ntp_time_path option to write the first NTP time to a file.

2019-05-22 Thread Jonathan Viney
Hi,

The NTP time from the first RTCP packet is currently extracted in
libavformat/rtsp.c and stored in AVFormatContext.start_time_realtime.
However, there is no way to access this value when using ffmpeg from the
commandline.

This patch adds an option when using an RTSP input to write the value to a
file when it is received.

Eg:
ffmpeg -first_rtcp_ntp_time_path out.mp4.ntp -i rtsp://10.0.0.1/ -c copy
out.mp4

Regards,
-Jonathan.


0001-avformat-rtsp-add-first_rtcp_ntp_time_path-option-to.patch
Description: Binary data
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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

Re: [FFmpeg-devel] [PATCH] avformat/rtsp: Add -first_rtcp_ntp_time_path option to write the first NTP time to a file.

2019-05-22 Thread Jonathan Viney
Apologies, here is the patch in text/plain.

On Thu, May 23, 2019 at 4:44 PM Jonathan Viney 
wrote:

> Hi,
>
> The NTP time from the first RTCP packet is currently extracted in
> libavformat/rtsp.c and stored in AVFormatContext.start_time_realtime.
> However, there is no way to access this value when using ffmpeg from the
> commandline.
>
> This patch adds an option when using an RTSP input to write the value to a
> file when it is received.
>
> Eg:
> ffmpeg -first_rtcp_ntp_time_path out.mp4.ntp -i rtsp://10.0.0.1/ -c copy
> out.mp4
>
> Regards,
> -Jonathan.
>
From e16826736640f132f0d3a6f170337ab9696e0038 Mon Sep 17 00:00:00 2001
From: Jonathan Viney 
Date: Thu, 23 May 2019 14:24:16 +1200
Subject: [PATCH] avformat/rtsp: add -first_rtcp_ntp_time_path option to write
 the first NTP time to a file.

Signed-off-by: Jonathan Viney 
---
 libavformat/rtsp.c | 16 
 libavformat/rtsp.h |  6 ++
 2 files changed, 22 insertions(+)

diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index c153cac88b..4b048701e2 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -96,6 +96,7 @@ const AVOption ff_rtsp_options[] = {
 { "min_port", "set minimum local UDP port", OFFSET(rtp_port_min), 
AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MIN}, 0, 65535, DEC|ENC },
 { "max_port", "set maximum local UDP port", OFFSET(rtp_port_max), 
AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MAX}, 0, 65535, DEC|ENC },
 { "listen_timeout", "set maximum timeout (in seconds) to wait for incoming 
connections (-1 is infinite, imply flag listen)", OFFSET(initial_timeout), 
AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, DEC },
+{ "first_rtcp_ntp_time_path", "path to write first NTP time (in 
microseconds) received in RTCP packet", OFFSET(first_rtcp_ntp_time_path), 
AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
 #if FF_API_OLD_RTSP_OPTIONS
 { "timeout", "set maximum timeout (in seconds) to wait for incoming 
connections (-1 is infinite, imply flag listen) (deprecated, use 
listen_timeout)", OFFSET(initial_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, 
INT_MIN, INT_MAX, DEC },
 { "stimeout", "set timeout (in microseconds) of socket TCP I/O 
operations", OFFSET(stimeout), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, 
DEC },
@@ -2256,6 +2257,21 @@ redo:
 (uint64_t) rtpctx->st->time_base.num * 
100,
rtpctx->st->time_base.den);
 }
+
+// Write the NTP start time
+if (rt->first_rtcp_ntp_time_path) {
+AVIOContext *ioctx = NULL;
+int ret;
+ret = avio_open(&ioctx, rt->first_rtcp_ntp_time_path, 
AVIO_FLAG_WRITE);
+if (ret < 0) {
+av_log(s, AV_LOG_WARNING, "unable to open %s to 
write first rtcp ntp time\n", rt->first_rtcp_ntp_time_path);
+} else {
+char buf[21] = "";
+av_strlcatf(buf, sizeof(buf), "%lld", 
s->start_time_realtime);
+avio_write(ioctx, buf, strlen(buf));
+avio_closep(&ioctx);
+}
+}
 }
 }
 if (ret == -RTCP_BYE) {
diff --git a/libavformat/rtsp.h b/libavformat/rtsp.h
index 54a9a30c16..4df24b743a 100644
--- a/libavformat/rtsp.h
+++ b/libavformat/rtsp.h
@@ -408,6 +408,12 @@ typedef struct RTSPState {
  */
 char *user_agent;
 
+/**
+ * Path to write the first RTCP unix time in microseconds, if
+ * it is received as part of the stream.
+ */
+char *first_rtcp_ntp_time_path;
+
 char default_lang[4];
 int buffer_size;
 int pkt_size;
-- 
2.21.0
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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

Re: [FFmpeg-devel] [PATCH] avformat/rtsp: Add -first_rtcp_ntp_time_path option to write the first NTP time to a file.

2019-05-23 Thread Jonathan Viney
On Thu, May 23, 2019 at 7:14 PM Moritz Barsnick  wrote:

> On Thu, May 23, 2019 at 16:51:16 +1200, Jonathan Viney wrote:
> > > The NTP time from the first RTCP packet is currently extracted in
> > > libavformat/rtsp.c and stored in AVFormatContext.start_time_realtime.
> > > However, there is no way to access this value when using ffmpeg from
> the
> > > commandline.
> > >
> > > This patch adds an option when using an RTSP input to write the value
> to a
> > > file when it is received.
>
> Is this useful for anything? Would it be more useful in a metadata key?
> (Just wondering, not critisizing.)
>

I agree having it transferred to a metadata key in the output would be
useful. That was my first thought, but seems like it would be a larger
patch and I wasn't sure if it would be able to write to the metadata for
streaming formats like fragmented MP4 (which is what I'm using) because the
RTCP packet arrives some time after the output metadata would have be
written (from what I understand). Please correct me if I'm wrong about that.


>
> > +av_strlcatf(buf, sizeof(buf), "%lld",
> s->start_time_realtime);
>
> start_time_realtime is int64_t, so the format identifier should be
> '"%" PRIi64'.
>

Thanks - will update the patch.

Regards,
-Jonathan.


>
> Moritz
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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

Re: [FFmpeg-devel] [PATCH] avformat/rtsp: Add -first_rtcp_ntp_time_path option to write the first NTP time to a file.

2019-05-29 Thread Jonathan Viney
On Thu, May 23, 2019 at 8:10 PM Jonathan Viney 
wrote:

> On Thu, May 23, 2019 at 7:14 PM Moritz Barsnick  wrote:
>
>> On Thu, May 23, 2019 at 16:51:16 +1200, Jonathan Viney wrote:
>> > > The NTP time from the first RTCP packet is currently extracted in
>> > > libavformat/rtsp.c and stored in AVFormatContext.start_time_realtime.
>> > > However, there is no way to access this value when using ffmpeg from
>> the
>> > > commandline.
>> > >
>> > > This patch adds an option when using an RTSP input to write the value
>> to a
>> > > file when it is received.
>>
>> Is this useful for anything? Would it be more useful in a metadata key?
>> (Just wondering, not critisizing.)
>>
>
> I agree having it transferred to a metadata key in the output would be
> useful. That was my first thought, but seems like it would be a larger
> patch and I wasn't sure if it would be able to write to the metadata for
> streaming formats like fragmented MP4 (which is what I'm using) because the
> RTCP packet arrives some time after the output metadata would have be
> written (from what I understand). Please correct me if I'm wrong about that.
>
>
>>
>> > +av_strlcatf(buf, sizeof(buf), "%lld",
>> s->start_time_realtime);
>>
>> start_time_realtime is int64_t, so the format identifier should be
>> '"%" PRIi64'.
>>
>
> Thanks - will update the patch.
>

Updated patch.

Regards,
-Jonathan.
From d2e1b11161e825bc8d40a4a368180c8a69ed6a75 Mon Sep 17 00:00:00 2001
From: Jonathan Viney 
Date: Thu, 23 May 2019 14:24:16 +1200
Subject: [PATCH] avformat/rtsp: add -first_rtcp_ntp_time_path option to write
 the first NTP time to a file.

Signed-off-by: Jonathan Viney 
---
 libavformat/rtsp.c | 16 
 libavformat/rtsp.h |  6 ++
 2 files changed, 22 insertions(+)

diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index c153cac88b..cda92be162 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -96,6 +96,7 @@ const AVOption ff_rtsp_options[] = {
 { "min_port", "set minimum local UDP port", OFFSET(rtp_port_min), 
AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MIN}, 0, 65535, DEC|ENC },
 { "max_port", "set maximum local UDP port", OFFSET(rtp_port_max), 
AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MAX}, 0, 65535, DEC|ENC },
 { "listen_timeout", "set maximum timeout (in seconds) to wait for incoming 
connections (-1 is infinite, imply flag listen)", OFFSET(initial_timeout), 
AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, DEC },
+{ "first_rtcp_ntp_time_path", "path to write first NTP time (in 
microseconds) received in RTCP packet", OFFSET(first_rtcp_ntp_time_path), 
AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
 #if FF_API_OLD_RTSP_OPTIONS
 { "timeout", "set maximum timeout (in seconds) to wait for incoming 
connections (-1 is infinite, imply flag listen) (deprecated, use 
listen_timeout)", OFFSET(initial_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, 
INT_MIN, INT_MAX, DEC },
 { "stimeout", "set timeout (in microseconds) of socket TCP I/O 
operations", OFFSET(stimeout), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, 
DEC },
@@ -2256,6 +2257,21 @@ redo:
 (uint64_t) rtpctx->st->time_base.num * 
100,
rtpctx->st->time_base.den);
 }
+
+// Write the NTP start time
+if (rt->first_rtcp_ntp_time_path) {
+AVIOContext *ioctx = NULL;
+int ret;
+ret = avio_open(&ioctx, rt->first_rtcp_ntp_time_path, 
AVIO_FLAG_WRITE);
+if (ret < 0) {
+av_log(s, AV_LOG_WARNING, "unable to open %s to 
write first rtcp ntp time\n", rt->first_rtcp_ntp_time_path);
+} else {
+char buf[21] = "";
+av_strlcatf(buf, sizeof(buf), "%"PRIi64, 
s->start_time_realtime);
+avio_write(ioctx, buf, strlen(buf));
+avio_closep(&ioctx);
+}
+}
 }
 }
 if (ret == -RTCP_BYE) {
diff --git a/libavformat/rtsp.h b/libavformat/rtsp.h
index 54a9a30c16..4df24b743a 100644
--- a/libavformat/rtsp.h
+++ b/libavformat/rtsp.h
@@ -408,6 +408,12 @@ typedef struct RTSPState {
  */
 char *user_agent;
 
+/**
+ * Path to write the first RTCP unix time in microseconds, if
+ * it is received as part of the stream.
+ */
+char *first_rtcp_ntp_time_path;
+
 char default_lang[4];
 int buffer_size;
 int pkt_size;
-- 
2.21.0

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

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

[FFmpeg-devel] [PATCH] libavformat/keyframes: output keyframe indexes

2015-05-03 Thread Jonathan Viney
Hi,

First patch here. I wrote this format to be able to get the keyframe
indexes faster than seemed possible with ffprobe, and also to get more
familiar with ffmpeg.

ffmpeg -i input.mkv -c copy -f keyframes -keyframes_filename keyframes.txt
/dev/null

Could something like this be included in ffmpeg? Or is this already
achievable with the current code?

Cheers,
-Jonathan.


---
 libavformat/Makefile |  1 +
 libavformat/allformats.c |  1 +
 libavformat/keyframes.c  | 96

 3 files changed, 98 insertions(+)
 create mode 100644 libavformat/keyframes.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 8d9a770..b78a753 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -221,6 +221,7 @@ OBJS-$(CONFIG_IVF_MUXER) += ivfenc.o
 OBJS-$(CONFIG_JACOSUB_DEMUXER)   += jacosubdec.o subtitles.o
 OBJS-$(CONFIG_JACOSUB_MUXER) += jacosubenc.o rawenc.o
 OBJS-$(CONFIG_JV_DEMUXER)+= jvdec.o
+OBJS-$(CONFIG_KEYFRAMES_MUXER)   += keyframes.o
 OBJS-$(CONFIG_LATM_DEMUXER)  += rawdec.o
 OBJS-$(CONFIG_LATM_MUXER)+= latmenc.o rawenc.o
 OBJS-$(CONFIG_LMLM4_DEMUXER) += lmlm4.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index e6a9d01..f1da759 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -164,6 +164,7 @@ void av_register_all(void)
 REGISTER_MUXDEMUX(IVF,  ivf);
 REGISTER_MUXDEMUX(JACOSUB,  jacosub);
 REGISTER_DEMUXER (JV,   jv);
+REGISTER_MUXER   (KEYFRAMES,keyframes);
 REGISTER_MUXDEMUX(LATM, latm);
 REGISTER_DEMUXER (LMLM4,lmlm4);
 REGISTER_DEMUXER (LOAS, loas);
diff --git a/libavformat/keyframes.c b/libavformat/keyframes.c
new file mode 100644
index 000..da6e054
--- /dev/null
+++ b/libavformat/keyframes.c
@@ -0,0 +1,96 @@
+/*
+ * Output keyframe indexes.
+ *
+ * Copyright (c) 2015 Jonathan Viney
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
+ */
+
+#include "avformat.h"
+#include "libavutil/opt.h"
+
+typedef struct {
+const AVClass *class;
+int frame_count;
+char *filename;
+FILE *file;
+} KeyFramesContext;
+
+av_cold static int kf_init(AVFormatContext *s)
+{
+KeyFramesContext *kf = s->priv_data;
+
+kf->file = fopen(kf->filename, "w");
+
+if (kf->file) {
+av_log(s, AV_LOG_ERROR, "failed opening key frames file: %s\n",
kf->filename);
+return -1;
+}
+
+return 0;
+}
+
+static int kf_write_packet(struct AVFormatContext *s, AVPacket *pkt)
+{
+KeyFramesContext *kf = s->priv_data;
+
+if (pkt->flags & AV_PKT_FLAG_KEY) {
+av_log(s, AV_LOG_DEBUG, "key frame at index %d\n",
kf->frame_count);
+
+if (fprintf(kf->file, "%d\n", kf->frame_count) < 0) {
+av_log(s, AV_LOG_ERROR, "failed writing to key frames file\n");
+return -1;
+}
+}
+
+kf->frame_count++;
+
+return 0;
+}
+
+av_cold static int kf_uninit(AVFormatContext *s)
+{
+KeyFramesContext *kf = s->priv_data;
+fclose(kf->file);
+kf->file = NULL;
+return 0;
+}
+
+#define OFFSET(x) offsetof(KeyFramesContext, x)
+static const AVOption options[] = {
+{ "keyframes_filename",  "output file for key frame indexes",
OFFSET(filename), AV_OPT_TYPE_STRING, { .str = "keyframes.txt" }, CHAR_MIN,
CHAR_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+{ NULL }
+};
+
+static const AVClass keyframes_class = {
+.class_name = "keyframes",
+.item_name  = av_default_item_name,
+.option = options,
+.version= LIBAVUTIL_VERSION_INT,
+};
+
+AVOutputFormat ff_keyframes_muxer = {
+.name   = "keyframes",
+.long_name  = NULL_IF_CONFIG_SMALL("Output keyframe indexes."),
+.priv_data_size = sizeof(KeyFramesContext),
+.flags  = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
+.video_codec= AV_CODEC_ID_RAWVIDEO,
+.write_header   = kf_init,
+.write_packet   = kf_write_packet,
+.write_trailer  = kf_uninit,
+.priv_cla

Re: [FFmpeg-devel] [PATCH] libavformat/keyframes: output keyframe indexes

2015-05-04 Thread Jonathan Viney
Hi,

Thanks for the feedback. After having another look, this can actually
already be done with ffprobe. I was incorrectly using -show_frames
instead of -show_packets, causing a big performance hit.

This works well, with the same performance:

ffprobe -show_packets -print_format compact input.mkv | grep -n
flags=K | cut -f1 -d:

Thanks,
-Jonathan.


On Mon, May 4, 2015 at 11:01 PM, Michael Niedermayer  wrote:
> On Mon, May 04, 2015 at 02:35:45AM +1200, Jonathan Viney wrote:
>> Hi,
>>
>> First patch here. I wrote this format to be able to get the keyframe
>> indexes faster than seemed possible with ffprobe, and also to get more
>> familiar with ffmpeg.
>>
>> ffmpeg -i input.mkv -c copy -f keyframes -keyframes_filename keyframes.txt
>> /dev/null
>>
>> Could something like this be included in ffmpeg? Or is this already
>> achievable with the current code?
>
> iam a bit undecided if this is a good idea but
>
> why is this faster than ffprobe ? or rather why is ffprobe slow ?
>
> [...]
>> +
>> +#include "avformat.h"
>> +#include "libavutil/opt.h"
>> +
>> +typedef struct {
>> +const AVClass *class;
>> +int frame_count;
>> +char *filename;
>> +FILE *file;
>> +} KeyFramesContext;
>> +
>> +av_cold static int kf_init(AVFormatContext *s)
>> +{
>> +KeyFramesContext *kf = s->priv_data;
>> +
>> +kf->file = fopen(kf->filename, "w");
>
> this should use the io context like any other muxer not bypass it
> with fopen/f*...
>
>
>> +
>> +if (kf->file) {
>> +av_log(s, AV_LOG_ERROR, "failed opening key frames file: %s\n",
>> kf->filename);
>> +return -1;
>> +}
>> +
>> +return 0;
>> +}
>> +
>> +static int kf_write_packet(struct AVFormatContext *s, AVPacket *pkt)
>> +{
>> +KeyFramesContext *kf = s->priv_data;
>> +
>> +if (pkt->flags & AV_PKT_FLAG_KEY) {
>> +av_log(s, AV_LOG_DEBUG, "key frame at index %d\n",
>> kf->frame_count);
>
> patch is corrupted by linebreaks
>
> [...]
> --
> Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
>
> He who knows, does not speak. He who speaks, does not know. -- Lao Tsu
>
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel