[Libav-user] Preview video and start recording with exact same stream settings

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

[Libav-user] Preview video and start recording with exact same stream settings

Audric Ackermann
Hi,

I think I already know the answer but I want to play UDP stream (h264 & klv) in an android app, and be able to toggle a "recording button" to save the data as a file with the exact same input parameters than what is sent to be.
The only way to do this is  to decode the whole frame (to display it) and reencode it when the button is pressed, right?

Because of the h264 I am not sure when the button is pressed I have a keyframe already, so there is no other way than doing this way?

Thanks for this clarification,
Audric

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Preview video and start recording with exact same stream settings

Việt Hùng Nguyễn
If your setting differs from what is sent, there is no way except transcode.

If your setting identical to what is sent, you can start recording by waiting for key frame. This will lead the latency in your dump file compares to the time you toggle record button on the UDP stream. This latency is depended on GOP size of original stream

On Tue, Dec 31, 2019 at 08:29 Audric Ackermann <[hidden email]> wrote:
Hi,

I think I already know the answer but I want to play UDP stream (h264 & klv) in an android app, and be able to toggle a "recording button" to save the data as a file with the exact same input parameters than what is sent to be.
The only way to do this is  to decode the whole frame (to display it) and reencode it when the button is pressed, right?

Because of the h264 I am not sure when the button is pressed I have a keyframe already, so there is no other way than doing this way?

Thanks for this clarification,
Audric
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Preview video and start recording with exact same stream settings

Audric Ackermann
Ok thank you.
As I do not control the input parameters I will go for the transcode option. Is there an easy way to duplicate all required decoder parameters to the encoder?

Like doing this, but with all important params in one step:

Thanks

outst->codec->width = this->codec_ctx->width;
outst->codec->height = this->codec_ctx->height;
outst->codec->pix_fmt = this->codec_ctx->pix_fmt;
outst->codec->time_base = this->codec_ctx->time_base;

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Preview video and start recording with exact same stream settings

Strahinja Radman
If you only want to copy the input, there is no need to transcode. You can simply duplicate the input packets, one packet goes to the decoder so it can be shown, the other 
goes to the muxer for the recorder when someone presses the record button. You can save to whatever format you want but input may not be playable from the start until
it reaches the key frame. Deep packet copy can be achieved with the following code:

AVPacket packet;
av_init_packet(&packet);
av_packet_ref(packet, src);
av_packet_make_writable(&packet);

You can based on the decoded frames, mark a key frame packet and start saving from there.

On Wed, Jan 1, 2020 at 10:06 AM Audric Ackermann <[hidden email]> wrote:
Ok thank you.
As I do not control the input parameters I will go for the transcode option. Is there an easy way to duplicate all required decoder parameters to the encoder?

Like doing this, but with all important params in one step:

Thanks

outst->codec->width = this->codec_ctx->width;
outst->codec->height = this->codec_ctx->height;
outst->codec->pix_fmt = this->codec_ctx->pix_fmt;
outst->codec->time_base = this->codec_ctx->time_base;
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Preview video and start recording with exact same stream settings

Audric Ackermann
But if I want to avoid missing the first few frames  which are not a key frame, I have to reencode the data, so the first frame recorded is a key frame, right?

Le mer. 1 janv. 2020 à 20:49, Strahinja Radman <[hidden email]> a écrit :
If you only want to copy the input, there is no need to transcode. You can simply duplicate the input packets, one packet goes to the decoder so it can be shown, the other 
goes to the muxer for the recorder when someone presses the record button. You can save to whatever format you want but input may not be playable from the start until
it reaches the key frame. Deep packet copy can be achieved with the following code:

AVPacket packet;
av_init_packet(&packet);
av_packet_ref(packet, src);
av_packet_make_writable(&packet);

You can based on the decoded frames, mark a key frame packet and start saving from there.

On Wed, Jan 1, 2020 at 10:06 AM Audric Ackermann <[hidden email]> wrote:
Ok thank you.
As I do not control the input parameters I will go for the transcode option. Is there an easy way to duplicate all required decoder parameters to the encoder?

Like doing this, but with all important params in one step:

Thanks

outst->codec->width = this->codec_ctx->width;
outst->codec->height = this->codec_ctx->height;
outst->codec->pix_fmt = this->codec_ctx->pix_fmt;
outst->codec->time_base = this->codec_ctx->time_base;
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Preview video and start recording with exact same stream settings

Việt Hùng Nguyễn
Yes, exactly!

On Wed, Jan 1, 2020, 17:10 Audric Ackermann <[hidden email]> wrote:
But if I want to avoid missing the first few frames  which are not a key frame, I have to reencode the data, so the first frame recorded is a key frame, right?

Le mer. 1 janv. 2020 à 20:49, Strahinja Radman <[hidden email]> a écrit :
If you only want to copy the input, there is no need to transcode. You can simply duplicate the input packets, one packet goes to the decoder so it can be shown, the other 
goes to the muxer for the recorder when someone presses the record button. You can save to whatever format you want but input may not be playable from the start until
it reaches the key frame. Deep packet copy can be achieved with the following code:

AVPacket packet;
av_init_packet(&packet);
av_packet_ref(packet, src);
av_packet_make_writable(&packet);

You can based on the decoded frames, mark a key frame packet and start saving from there.

On Wed, Jan 1, 2020 at 10:06 AM Audric Ackermann <[hidden email]> wrote:
Ok thank you.
As I do not control the input parameters I will go for the transcode option. Is there an easy way to duplicate all required decoder parameters to the encoder?

Like doing this, but with all important params in one step:

Thanks

outst->codec->width = this->codec_ctx->width;
outst->codec->height = this->codec_ctx->height;
outst->codec->pix_fmt = this->codec_ctx->pix_fmt;
outst->codec->time_base = this->codec_ctx->time_base;
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Preview video and start recording with exact same stream settings

Audric Ackermann
And so, how can I copy all the input parameters? To get the same parameters in the recorded file? 

Le mer. 1 janv. 2020 à 21:36, Việt Hùng Nguyễn <[hidden email]> a écrit :
Yes, exactly!

On Wed, Jan 1, 2020, 17:10 Audric Ackermann <[hidden email]> wrote:
But if I want to avoid missing the first few frames  which are not a key frame, I have to reencode the data, so the first frame recorded is a key frame, right?

Le mer. 1 janv. 2020 à 20:49, Strahinja Radman <[hidden email]> a écrit :
If you only want to copy the input, there is no need to transcode. You can simply duplicate the input packets, one packet goes to the decoder so it can be shown, the other 
goes to the muxer for the recorder when someone presses the record button. You can save to whatever format you want but input may not be playable from the start until
it reaches the key frame. Deep packet copy can be achieved with the following code:

AVPacket packet;
av_init_packet(&packet);
av_packet_ref(packet, src);
av_packet_make_writable(&packet);

You can based on the decoded frames, mark a key frame packet and start saving from there.

On Wed, Jan 1, 2020 at 10:06 AM Audric Ackermann <[hidden email]> wrote:
Ok thank you.
As I do not control the input parameters I will go for the transcode option. Is there an easy way to duplicate all required decoder parameters to the encoder?

Like doing this, but with all important params in one step:

Thanks

outst->codec->width = this->codec_ctx->width;
outst->codec->height = this->codec_ctx->height;
outst->codec->pix_fmt = this->codec_ctx->pix_fmt;
outst->codec->time_base = this->codec_ctx->time_base;
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Preview video and start recording with exact same stream settings

Việt Hùng Nguyễn
I don’t remember exactly but you can use av_copy_context api to copy all needed parameters 

On Wed, Jan 1, 2020 at 17:44 Audric Ackermann <[hidden email]> wrote:
And so, how can I copy all the input parameters? To get the same parameters in the recorded file? 

Le mer. 1 janv. 2020 à 21:36, Việt Hùng Nguyễn <[hidden email]> a écrit :
Yes, exactly!

On Wed, Jan 1, 2020, 17:10 Audric Ackermann <[hidden email]> wrote:
But if I want to avoid missing the first few frames  which are not a key frame, I have to reencode the data, so the first frame recorded is a key frame, right?

Le mer. 1 janv. 2020 à 20:49, Strahinja Radman <[hidden email]> a écrit :
If you only want to copy the input, there is no need to transcode. You can simply duplicate the input packets, one packet goes to the decoder so it can be shown, the other 
goes to the muxer for the recorder when someone presses the record button. You can save to whatever format you want but input may not be playable from the start until
it reaches the key frame. Deep packet copy can be achieved with the following code:

AVPacket packet;
av_init_packet(&packet);
av_packet_ref(packet, src);
av_packet_make_writable(&packet);

You can based on the decoded frames, mark a key frame packet and start saving from there.

On Wed, Jan 1, 2020 at 10:06 AM Audric Ackermann <[hidden email]> wrote:
Ok thank you.
As I do not control the input parameters I will go for the transcode option. Is there an easy way to duplicate all required decoder parameters to the encoder?

Like doing this, but with all important params in one step:

Thanks

outst->codec->width = this->codec_ctx->width;
outst->codec->height = this->codec_ctx->height;
outst->codec->pix_fmt = this->codec_ctx->pix_fmt;
outst->codec->time_base = this->codec_ctx->time_base;
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Preview video and start recording with exact same stream settings

Strahinja Radman
In reply to this post by Audric Ackermann
Well, you can do this. Keep last N number of packets in a buffer, where N is guaranteed to contain keyframe packet. When you encounter new keyframe packet
you can free the packets that are not needed anymore. When someone starts to record, you can mux from the last known key frame packet. 

On Wed, Jan 1, 2020 at 11:10 AM Audric Ackermann <[hidden email]> wrote:
But if I want to avoid missing the first few frames  which are not a key frame, I have to reencode the data, so the first frame recorded is a key frame, right?

Le mer. 1 janv. 2020 à 20:49, Strahinja Radman <[hidden email]> a écrit :
If you only want to copy the input, there is no need to transcode. You can simply duplicate the input packets, one packet goes to the decoder so it can be shown, the other 
goes to the muxer for the recorder when someone presses the record button. You can save to whatever format you want but input may not be playable from the start until
it reaches the key frame. Deep packet copy can be achieved with the following code:

AVPacket packet;
av_init_packet(&packet);
av_packet_ref(packet, src);
av_packet_make_writable(&packet);

You can based on the decoded frames, mark a key frame packet and start saving from there.

On Wed, Jan 1, 2020 at 10:06 AM Audric Ackermann <[hidden email]> wrote:
Ok thank you.
As I do not control the input parameters I will go for the transcode option. Is there an easy way to duplicate all required decoder parameters to the encoder?

Like doing this, but with all important params in one step:

Thanks

outst->codec->width = this->codec_ctx->width;
outst->codec->height = this->codec_ctx->height;
outst->codec->pix_fmt = this->codec_ctx->pix_fmt;
outst->codec->time_base = this->codec_ctx->time_base;
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Preview video and start recording with exact same stream settings

Audric Ackermann
OK thanks, that's a good idea. 
I don't know much but I can basically write the last keyframe to the file and then write the last packets received after pressing record? I mean, between two key frames, all frames are dependent on the key frame only? 

How can I check if the keyframe I got is a keyframe?

Audric

Le mer. 1 janv. 2020 à 22:43, Strahinja Radman <[hidden email]> a écrit :
Well, you can do this. Keep last N number of packets in a buffer, where N is guaranteed to contain keyframe packet. When you encounter new keyframe packet
you can free the packets that are not needed anymore. When someone starts to record, you can mux from the last known key frame packet. 

On Wed, Jan 1, 2020 at 11:10 AM Audric Ackermann <[hidden email]> wrote:
But if I want to avoid missing the first few frames  which are not a key frame, I have to reencode the data, so the first frame recorded is a key frame, right?

Le mer. 1 janv. 2020 à 20:49, Strahinja Radman <[hidden email]> a écrit :
If you only want to copy the input, there is no need to transcode. You can simply duplicate the input packets, one packet goes to the decoder so it can be shown, the other 
goes to the muxer for the recorder when someone presses the record button. You can save to whatever format you want but input may not be playable from the start until
it reaches the key frame. Deep packet copy can be achieved with the following code:

AVPacket packet;
av_init_packet(&packet);
av_packet_ref(packet, src);
av_packet_make_writable(&packet);

You can based on the decoded frames, mark a key frame packet and start saving from there.

On Wed, Jan 1, 2020 at 10:06 AM Audric Ackermann <[hidden email]> wrote:
Ok thank you.
As I do not control the input parameters I will go for the transcode option. Is there an easy way to duplicate all required decoder parameters to the encoder?

Like doing this, but with all important params in one step:

Thanks

outst->codec->width = this->codec_ctx->width;
outst->codec->height = this->codec_ctx->height;
outst->codec->pix_fmt = this->codec_ctx->pix_fmt;
outst->codec->time_base = this->codec_ctx->time_base;
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Preview video and start recording with exact same stream settings

Strahinja Radman
That is not entirely true, frames can be dependent on past and/or forward frames, depending on a frame type, P or B. So you can just simply write the key frame 
to the file. You need to save the last GOP (group of frames between two I frames). Here is the link to SO where you can read on how to extract frame type

On Wed, Jan 1, 2020 at 12:57 PM Audric Ackermann <[hidden email]> wrote:
OK thanks, that's a good idea. 
I don't know much but I can basically write the last keyframe to the file and then write the last packets received after pressing record? I mean, between two key frames, all frames are dependent on the key frame only? 

How can I check if the keyframe I got is a keyframe?

Audric

Le mer. 1 janv. 2020 à 22:43, Strahinja Radman <[hidden email]> a écrit :
Well, you can do this. Keep last N number of packets in a buffer, where N is guaranteed to contain keyframe packet. When you encounter new keyframe packet
you can free the packets that are not needed anymore. When someone starts to record, you can mux from the last known key frame packet. 

On Wed, Jan 1, 2020 at 11:10 AM Audric Ackermann <[hidden email]> wrote:
But if I want to avoid missing the first few frames  which are not a key frame, I have to reencode the data, so the first frame recorded is a key frame, right?

Le mer. 1 janv. 2020 à 20:49, Strahinja Radman <[hidden email]> a écrit :
If you only want to copy the input, there is no need to transcode. You can simply duplicate the input packets, one packet goes to the decoder so it can be shown, the other 
goes to the muxer for the recorder when someone presses the record button. You can save to whatever format you want but input may not be playable from the start until
it reaches the key frame. Deep packet copy can be achieved with the following code:

AVPacket packet;
av_init_packet(&packet);
av_packet_ref(packet, src);
av_packet_make_writable(&packet);

You can based on the decoded frames, mark a key frame packet and start saving from there.

On Wed, Jan 1, 2020 at 10:06 AM Audric Ackermann <[hidden email]> wrote:
Ok thank you.
As I do not control the input parameters I will go for the transcode option. Is there an easy way to duplicate all required decoder parameters to the encoder?

Like doing this, but with all important params in one step:

Thanks

outst->codec->width = this->codec_ctx->width;
outst->codec->height = this->codec_ctx->height;
outst->codec->pix_fmt = this->codec_ctx->pix_fmt;
outst->codec->time_base = this->codec_ctx->time_base;
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".


--

Regards
Strahinja Radman

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".