Source file metadata.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
module CharEncoding = MetadataCharEncoding

module MIME = MetadataMIME

module Make (E : CharEncoding.T) = struct
  include MetadataBase
  module ID3v1 = MetadataID3v1
  module ID3v2 = MetadataID3v2
  module OGG = MetadataOGG
  module FLAC = MetadataFLAC
  module JPEG = MetadataJPEG
  module PNG = MetadataPNG
  module AVI = MetadataAVI
  module MP4 = MetadataMP4
  module WAV = MetadataWAV
  module RIFF = MetadataRIFF

  (** Charset conversion function. *)
  let recode = E.convert

  module ID3 = struct
    let parse f =
      let failure, v2 =
        try (false, ID3v2.parse ~recode f) with _ -> (true, [])
      in
      let v1 =
        try
          Reader.reset f;
          ID3v1.parse ~recode f
        with _ -> if failure then raise Invalid else []
      in
      v2 @ v1

    let parse_file ?custom_parser file =
      Reader.with_file ?custom_parser parse file
  end

  let rec first_valid l file =
    match l with
      | f :: l -> (
          try f file
          with Invalid ->
            Reader.reset file;
            first_valid l file)
      | [] -> raise Invalid

  module Audio = struct
    let parsers = [ID3.parse; OGG.parse; FLAC.parse; WAV.parse]
    let parse = first_valid parsers

    let parse_file ?custom_parser file =
      Reader.with_file ?custom_parser parse file
  end

  module Image = struct
    let parsers = [JPEG.parse; PNG.parse]
    let parse = first_valid parsers

    let parse_file ?custom_parser file =
      Reader.with_file ?custom_parser parse file
  end

  module Video = struct
    let parsers = [AVI.parse; MP4.parse]
    let parse = first_valid parsers

    let parse_file ?custom_parser file =
      Reader.with_file ?custom_parser parse file
  end

  module Any = struct
    let parsers = Audio.parsers @ Image.parsers @ Video.parsers @ [RIFF.parse]

    (** Genering parsing of metadata. *)
    let parse = first_valid parsers

    let parse_file ?custom_parser file =
      Reader.with_file ?custom_parser parse file

    (** Parse the metadatas of a string. *)
    let parse_string ?custom_parser file =
      Reader.with_string ?custom_parser parse file
  end

  include Any
end

include Make (CharEncoding.Naive)