Skip to content

Commit

Permalink
Manually set avg_frame_rate on stream copy.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Jul 23, 2024
1 parent f7d4055 commit 83c726b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
7 changes: 7 additions & 0 deletions av/av.ml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ let mk_stream container index = { container; index; decoder = None }
external get_codec_params : (_, 'm, _) stream -> 'm Avcodec.params
= "ocaml_av_get_stream_codec_parameters"

external get_avg_frame_rate : (_, video, _) stream -> Avutil.rational option
= "ocaml_av_get_stream_avg_frame_rate"

external set_avg_frame_rate :
(_, video, _) stream -> Avutil.rational option -> unit
= "ocaml_av_set_stream_avg_frame_rate"

external get_time_base : (_, _, _) stream -> Avutil.rational
= "ocaml_av_get_stream_time_base"

Expand Down
6 changes: 6 additions & 0 deletions av/av.mli
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ val get_index : (_, _, _) stream -> int
codec allocation failed. *)
val get_codec_params : (_, 'media, _) stream -> 'media Avcodec.params

(** [get_avg_frame_rate stream] returns the average frame rate, if set. *)
val get_avg_frame_rate : (_, video, _) stream -> Avutil.rational option

(** [set_avg_frame_rate stream rate] sets the average frame rate, if set. *)
val set_avg_frame_rate : (_, video, _) stream -> Avutil.rational option -> unit

(** [Av.get_time_base stream] return the time base of the [stream]. *)
val get_time_base : (_, _, _) stream -> Avutil.rational

Expand Down
38 changes: 37 additions & 1 deletion av/av_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include <libavutil/audio_fifo.h>
#include <libavutil/avstring.h>
#include <libavutil/opt.h>
#include <libavutil/timestamp.h>
#include <libavutil/parseutils.h>
#include <libavutil/timestamp.h>

#include "av_stubs.h"
#include "avcodec_stubs.h"
Expand Down Expand Up @@ -196,6 +196,42 @@ CAMLprim value ocaml_av_get_stream_codec_parameters(value _stream) {
CAMLreturn(ans);
}

CAMLprim value ocaml_av_get_stream_avg_frame_rate(value _stream) {
CAMLparam1(_stream);
CAMLlocal3(ans, ret, _av);
_av = Field(_stream, 0);
av_t *av = Av_val(_av);
int index = StreamIndex_val(_stream);
AVStream *st = av->format_context->streams[index];

if (!st->avg_frame_rate.num)
CAMLreturn(Val_none);

value_of_rational(&av->format_context->streams[index]->avg_frame_rate, &ans);

ret = caml_alloc_tuple(1);
Store_field(ret, 0, ans);

CAMLreturn(ret);
}

CAMLprim value ocaml_av_set_stream_avg_frame_rate(value _stream,
value _avg_frame_rate) {
CAMLparam2(_stream, _avg_frame_rate);
CAMLlocal1(_av);
_av = Field(_stream, 0);
av_t *av = Av_val(_av);
int index = StreamIndex_val(_stream);
AVStream *st = av->format_context->streams[index];

if (_avg_frame_rate == Val_none)
st->avg_frame_rate = (AVRational){0, 1};

st->avg_frame_rate = rational_of_value(Field(_avg_frame_rate, 0));

CAMLreturn(Val_unit);
}

CAMLprim value ocaml_av_get_stream_time_base(value _stream) {
CAMLparam1(_stream);
CAMLlocal2(ans, _av);
Expand Down
4 changes: 3 additions & 1 deletion examples/remuxing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ let () =
ivss
|> List.map (fun (i, stream, _) ->
let params = Av.get_codec_params stream in
(i, Av.new_stream_copy ~params dst))
let s = Av.new_stream_copy ~params dst in
Av.set_avg_frame_rate s (Av.get_avg_frame_rate stream);
(i, s))
in

let isss = Av.get_subtitle_streams src in
Expand Down

0 comments on commit 83c726b

Please sign in to comment.