File: demuxing_decoding.ml

package info (click to toggle)
ocaml-ffmpeg 1.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 908 kB
  • sloc: ansic: 6,412; ml: 6,166; makefile: 3
file content (80 lines) | stat: -rw-r--r-- 2,803 bytes parent folder | download | duplicates (2)
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
open Avutil
module AudioConverter = Swresample.Make (Swresample.Frame) (Swresample.S32Bytes)
module VideoConverter = Swscale.Make (Swscale.Frame) (Swscale.BigArray)

(* module VideoConverter = Swscale.Make (Swscale.Frame) (Swscale.Frame) *)

let () =
  if Array.length Sys.argv < 4 then (
    Printf.eprintf
      "      usage: %s input_file video_output_file audio_output_file\n\
      \      API example program to show how to read frames from an input file. \n\
      \      This program reads frames from a file, decodes them, and writes \
       decoded\n\
      \      video frames to a rawvideo file named video_output_file, and \
       decoded\n\
      \      audio frames to a rawaudio file named audio_output_file.\n"
      Sys.argv.(0);
    exit 1);

  Avutil.Log.set_level `Debug;
  Avutil.Log.set_callback print_string;

  let input_filename = Sys.argv.(1) in
  let video_output_filename = Sys.argv.(2) in
  let audio_output_filename = Sys.argv.(3) in

  let src = Av.open_input input_filename in
  Av.get_input_metadata src
  |> List.iter (fun (k, v) -> print_endline (k ^ " : " ^ v));

  let audio_index, audio_stream, audio_codec = Av.find_best_audio_stream src in

  let a_ctx =
    AudioConverter.from_codec audio_codec Avutil.Channel_layout.stereo 44100
  in
  let audio_output_file = open_out_bin audio_output_filename in

  let video_index, video_stream, _ = Av.find_best_video_stream src in

  (* let v_ctx = VideoConverter.from_codec video_codec 800 600 `Yuv420p in *)
  let v_ctx = VideoConverter.create [] 352 288 `Yuv420p 800 600 `Yuv420p in
  let video_output_file = open_out_bin video_output_filename in

  let rec decode () =
    match
      Av.read_input ~audio_frame:[audio_stream] ~video_frame:[video_stream] src
    with
      | `Audio_frame (idx, af) ->
          if idx = audio_index then
            AudioConverter.convert a_ctx af |> output_bytes audio_output_file;
          decode ()
      | `Video_frame (idx, vf) ->
          if idx = video_index then VideoConverter.convert v_ctx vf |> ignore
            (*output_video video_output_file*);
          decode ()
      | `Subtitle_frame (_, sf) ->
          let _, _, lines = Subtitle.frame_to_lines sf in
          lines |> List.iter print_endline;
          decode ()
      | exception Error `Eof -> ()
      | exception Error err -> prerr_endline (Avutil.string_of_error err)
      | _ -> assert false
  in
  decode ();

  Av.close src;

  close_out video_output_file;
  close_out audio_output_file;

  Printf.printf "Demuxing succeeded.\n";
  Printf.printf
    "Play the output audio file with the command:\n\
     ffplay -f %s -ac 2 -ar 44100 %s\n"
    (Option.get (Sample_format.get_name `S32)
    ^ if Sys.big_endian then "be" else "le")
    audio_output_filename;

  Gc.full_major ();
  Gc.full_major ()