-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathremuxing.ml
107 lines (96 loc) · 3.38 KB
/
remuxing.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
let () =
if Array.length Sys.argv < 3 then (
Printf.printf
" usage: %s input output\n\
\ API example program to remux a media file with libavformat and \
libavcodec.\n\
\ The output format is guessed according to the file extension.\n"
Sys.argv.(0);
exit 0);
Avutil.Log.set_level `Debug;
Avutil.Log.set_callback print_string;
let src = Av.open_input Sys.argv.(1) in
let dst = Av.open_output Sys.argv.(2) in
let iass = Av.get_audio_streams src in
let oass =
iass
|> List.map (fun (i, stream, _) ->
let params = Av.get_codec_params stream in
(i, Av.new_stream_copy ~params dst))
in
let ivss = Av.get_video_streams src in
let ovss =
ivss
|> List.map (fun (i, stream, _) ->
let params = Av.get_codec_params stream in
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
let osss =
isss
|> List.map (fun (i, stream, _) ->
let params = Av.get_codec_params stream in
(i, Av.new_stream_copy ~params dst))
in
let rec f () =
match
Av.read_input
~audio_packet:(List.map (fun (_, s, _) -> s) iass)
~video_packet:(List.map (fun (_, s, _) -> s) ivss)
~subtitle_packet:(List.map (fun (_, s, _) -> s) isss)
src
with
| `Audio_packet (i, pkt) ->
let time_base = Av.get_time_base (List.assoc i oass) in
Avcodec.Packet.add_side_data pkt
(`Strings_metadata [("foo", "bar"); ("gni", "gno")]);
Avcodec.Packet.add_side_data pkt
(`Metadata_update [("foo", "bar"); ("gni", "gno")]);
Avcodec.Packet.add_side_data pkt
(`Replaygain
{
Avcodec.Packet.track_gain = 1;
track_peak = 2;
album_gain = 3;
album_peak = 4;
});
List.iter
(function
| `Strings_metadata m ->
Printf.printf "[side data] string metadata: %s\n%!"
(String.concat ", "
(List.map (fun (k, v) -> k ^ ": " ^ v) m))
| `Metadata_update m ->
Printf.printf "[side data] metadata update: %s\n%!"
(String.concat ", "
(List.map (fun (k, v) -> k ^ ": " ^ v) m))
| `Replaygain
{
Avcodec.Packet.track_gain;
track_peak;
album_gain;
album_peak;
} ->
Printf.printf "[side data] Replaygain: %d/%d/%d/%d\n%!"
track_gain track_peak album_gain album_peak)
(Avcodec.Packet.side_data pkt);
Av.write_packet (List.assoc i oass) time_base pkt;
f ()
| `Video_packet (i, pkt) ->
let time_base = Av.get_time_base (List.assoc i ovss) in
Av.write_packet (List.assoc i ovss) time_base pkt;
f ()
| `Subtitle_packet (i, pkt) ->
let time_base = Av.get_time_base (List.assoc i osss) in
Av.write_packet (List.assoc i osss) time_base pkt;
f ()
| exception Avutil.Error `Eof -> ()
| _ -> assert false
in
f ();
Av.close src;
Av.close dst;
Gc.full_major ();
Gc.full_major ()