File: protocols.liq

package info (click to toggle)
liquidsoap 1.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,504 kB
  • sloc: ml: 37,149; python: 956; makefile: 624; sh: 458; perl: 322; lisp: 124; ansic: 53; ruby: 8
file content (337 lines) | stat: -rw-r--r-- 11,319 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
register(name="Protocol Settings","protocol",())

register(name="Replay_gain protocol settings","protocol.replay_gain",())
register(name="Replay_gain path","protocol.replay_gain.path","#{configure.libdir}/extract-replaygain")

# Register the replaygain protocol.
# @flag hidden
def replaygain_protocol(~rlog,~maxtime,arg)
 delay = maxtime - gettimeofday()
 # The extraction program
 extract_replaygain =
   get(default="#{configure.libdir}/extract-replaygain","protocol.replay_gain.path")
 x = get_process_lines(timeout=delay,"#{extract_replaygain} #{quote(arg)}")
 if list.hd(default="",x) != "" then
  ["annotate:replay_gain=\"#{list.hd(default='',x)}\":#{arg}"]
 else
  [arg]
 end
end
add_protocol("replay_gain", replaygain_protocol,
             syntax="replay_gain:uri",
             doc="Compute replaygain value using the extract-replaygain script. \
                  Adds returned value as @\"replay_gain\"@ metadata")

register(name="Process protocol settings","protocol.process",())
register(name="Process Environment",
         descr="List of environment variables \
                passed down to the executed process.",
         "protocol.process.env",
         [])
register(name="Inherit Environment",
         descr="Inherit calling process's environment when @env@ parameter is empty.",
         "protocol.process.inherit_env",
         true)

# Register the process protocol. Syntax:
# process:<output ext>,<cmd>:uri where <cmd> is interpolated with:
# [("input",<input file>),("output",<output file>),("colon",":")]
# See say: protocol for an example.
# @flag hidden
def process_protocol(~rlog,~maxtime,arg)
  log = log(label="protocol.process")

  def log(~level,s) =
    rlog(s)
    log(level=level,s)
  end

  log(level=4,"Processing #{arg}")

  x = string.split(separator=":",arg)
  uri = string.concat(separator=":",list.tl(x))
  x = string.split(separator=",",list.hd(default="",x))
  extname = list.hd(default="liq",x)
  cmd = string.concat(separator=",",list.tl(x))

  output = file.temp("liq-process", ".#{extname}")

  def resolve(input) =
    cmd = cmd % [("input",quote(input)),
                 ("output",quote(output)),
                 ("colon",":")]

    log(level=4,"Executing #{cmd}")

    env_vars = get(default=[],"protocol.process.env")
    env = environment()
    def get_env(k) =
      (k,env[k])
    end
    env = list.map(get_env,env_vars)
    inherit_env = get(default=true,"protocol.process.inherit_env")

    delay = maxtime - gettimeofday()
    ret = run_process(timeout=delay,env=env,inherit_env=inherit_env,cmd)
    if snd(ret) == ("exit","0") then
      [output]
    else
      log(level=3,"Failed to execute #{cmd}: #{snd(ret)}")
      []
    end
  end

  if uri == "" then
    resolve("")
  else
    r = request.create.raw(uri)
    delay = maxtime - gettimeofday()
    if request.resolve(timeout=delay,r) then
      res = resolve(request.filename(r))
      request.destroy(r)
      res
    else
      log(level=3,"Failed to resolve #{uri}")
      []
    end
  end
end
add_protocol(temporary=true, "process", process_protocol,
             doc="Resolve a request using an arbitrary process. \
                  @<cmd>@ is interpolated with: \
                  @[(\"input\",<input>),(\"output\",<output>),\
                  (\"colon\",\":\")]@. @uri@ is an optional child request, \
                  @<output>@ is the name of a fresh temporary file and has \
                  extension @.<extname>@. @<input>@ is an optional input \
                  file name as returned while resolving @uri@.",
             syntax="process:<extname>,<cmd>[:uri]")

# Create a process: uri, replacing @:@ with @$(colon)@
# @category Liquidsoap
# @param cmd Command line to execute
# @param ~extname Output file extension (with no leading '.')
# @param ~uri Input uri
def process_uri(~extname,~uri="",cmd) =
  cmd = string.replace(pattern=":",fun (_) -> "$(colon)",cmd)
  uri = if uri != "" then ":#{uri}" else "" end
  "process:#{extname},#{cmd}#{uri}"
end

register(name="External download protocol","protocol.external",true)
register(name="Path to curl","protocol.external.curl","curl")
register(name="External protocols","protocol.external.protocols",["http","https","ftp"])

# Resolve download protocols using curl
# @flag hidden
def download_protocol(proto,~rlog,~maxtime,arg) =
  curl = get(default="curl","protocol.external.curl")
  uri = "#{proto}:#{arg}"

  log = log(label="procol.external")

  def log(~level,s) =
    rlog(s)
    log(level=level,s)
  end

  env_vars = get(default=[],"protocol.process.env")
  env = environment()
  def get_env(k) =
    (k,env[k])
  end
  env = list.map(get_env,env_vars)
  inherit_env = get(default=true,"protocol.process.inherit_env")

  cmd = "#{curl} -sLI -X HEAD #{quote(uri)} | grep -i '^content-type' | tail -n 1 | cut -d':' -f 2 | cut -d';' -f 1"
  delay = maxtime - gettimeofday()
  log(level=4,"Running #{cmd}")
  x = run_process(timeout=delay,env=env,inherit_env=inherit_env,cmd)
  extname =
    if fst(snd(x)) != "exit" or snd(snd(x)) != "0" then
      log(level=3,"Failed to fetch mime-type for #{uri}.")
      "osb"
    else
      lines = string.split(separator="\\n",fst(fst(x)))
      mime = string.case(lower=true,string.trim(list.hd(default="",lines)))
      if list.mem(mime, ["audio/mpeg", "audio/mp3"]) then
        "mp3"
      elsif list.mem(mime,["application/ogg", "application/x-ogg",
                           "audio/x-ogg", "audio/ogg", "video/ogg"]) then
        "ogg"
      elsif mime == "audio/x-flac" then
        "flac"
      elsif list.mem(mime,["audio/mp4", "application/mp4"]) then
        "mp4"
      elsif list.mem(mime,["audio/vnd.wave", "audio/wav",
                           "audio/wave", "audio/x-wav"]) then
        "wav"
      else
        log(level=3,"No known file extension for mime: #{mime}")
        "osb"
      end
    end
  [process_uri(extname=extname,"#{curl} -sL #{quote(uri)} -o $(output)")]
end

# Register download protocol
# @flag hidden
def add_download_protocol(proto) =
  add_protocol(syntax="#{proto}://...",doc="Download files using curl",proto,download_protocol(proto))
end
if get(default=true,"protocol.external") then
  list.iter(add_download_protocol,get(default=["http","https","ftp"],"protocol.external.protocols"))
end

register(name="Youtube_dl protocol settings","protocol.youtube-dl",())
register(name="Youtube-dl path","protocol.youtube-dl.path","youtube-dl")

# Register the youtube-dl protocol, using youtube-dl.
# Syntax: youtube-dl:<ID>
# @flag hidden
def youtube_dl_protocol(~rlog,~maxtime,arg)
  binary = get(default="youtube-dl","protocol.youtube-dl.path")

  delay = maxtime - gettimeofday()
  x = get_process_lines(timeout=delay,"#{binary} --get-title --get-filename #{quote(arg)}")

  x =
    if list.length(x) >= 2 then
      x
    else
      ["",".osb"]
    end

  title = list.hd(default="",x)
  ext   = file.extension(list.nth(default="",x,1))
  ext   = string.sub(ext,start=1,length=string.length(ext)-1)

  cmd   = "rm -f $(output) && #{binary} -q -f best --no-playlist -o $(output) #{quote(arg)}"
  process = process_uri(extname=ext,cmd)

  if title != "" then
    ["annotate:title=#{quote(title)}:#{process}"]
  else
    [process]
  end
end
add_protocol("youtube-dl", youtube_dl_protocol,
              doc="Resolve a request using youtube-dl.",
              syntax="youtube-dl:uri")

register(name="ffmpeg2wav protocol settings","protocol.ffmpeg2wav",())
register(name="Path to ffmpeg","protocol.ffmpeg2wav.path","ffmpeg")
register(name="Number of channels","protocol.ffmpeg2wav.channels",2)

# Register ffmpeg2wav
# @flag hidden
def ffmpeg2wav_protocol(~rlog,~maxtime,arg) =
  ffmpeg   = get(default="ffmpeg","protocol.ffmpeg2wav.path")
  channels = get(default=2,"protocol.ffmpeg2wav.channels")
  cmd      = "#{ffmpeg} -y -i $(input) -ac #{channels} $(output)"

  [process_uri(extname="wav",uri=arg,cmd)]
end
add_protocol("ffmpeg2wav",ffmpeg2wav_protocol,
             doc="Decode any file to wave using ffmpeg",
             syntax="ffmpeg2wav:uri")

register(name="Text2wave protocol settings","protocol.text2wave",())
register(name="Text2wave path","protocol.text2wave.path","text2wave")

# Register the text2wave: protocol using text2wav
# @flag hidden
def text2wave_protocol(~rlog,~maxtime,arg) =
  binary = get(default="text2wave","protocol.text2wave.path")
  [process_uri(extname="wav","echo #{quote(arg)} | #{binary} -scale 1.9 > $(output)")]
end
add_protocol(static=true,"text2wave",text2wave_protocol,
             doc="Generate speech synthesis using text2wave. Result may be mono.",
             syntax="text2wav:Text to read")

register(name="Say protocol settings","protocol.say",())
register(name="Sox path","protocol.say.sox_path","sox")

# Register the legacy say: protocol using text2wav and sox
# @flag hidden
def say_protocol(~rlog,~maxtime,arg) =
  sox = get(default="sox","protocol.say.sox_path")
  [process_uri(extname="wav",uri="text2wave:#{arg}","#{sox} $(input) -c 2 $(output)")]
end
add_protocol(static=true,"say",say_protocol,
             doc="Generate speech synthesis using text2wave and sox. Result is always stereo.",
             syntax="say:Text to read")

register(name="AWS protocols settings","protocol.aws",())
register(name="Profile",descr="Use a specific profile from your credential file.",
         "protocol.aws.profile","")
register(name="Region",descr="AWS Region",
         "protocol.aws.region","")
register(name="Binary",descr="Path to aws CLI binary",
         "protocol.aws.path","aws")
register(name="Polly protocol settings","protocol.aws.polly",())
register(name="Format",descr="Output format",
         "protocol.aws.polly.format","mp3")
register(name="Voice",descr="Voice ID",
         "protocol.aws.polly.voice","Joanna")

# Build a aws base call
# @flag hidden
def aws_base() =
  aws = get(default="aws","protocol.aws.path")

  region = get(default="","protocol.aws.region")

  aws =
    if region !="" then
      "#{aws} --region #{region}"
    else
      aws
    end

  profile = get(default="","protocol.aws.profile")

  if profile !="" then
    "#{aws} --profile #{quote(profile)}"
  else
    aws
  end
end

# Register the s3:// protocol 
# @flag hidden
def s3_protocol(~rlog,~maxtime,arg) =
  extname = file.extension(dir_sep="/",arg)
  [process_uri(extname=extname,"#{aws_base()} s3 cp s3:#{arg} $(output)")]
end
add_protocol("s3",s3_protocol,doc="Fetch files from s3 using the AWS CLI",
             syntax="s3://uri")

# Register the polly: protocol using AWS Polly
# speech synthesis services. Syntax: polly:<text>
# @flag hidden
def polly_protocol(~rlog,~maxtime,text) =
  aws = aws_base()

  format = get(default="mp3","protocol.aws.polly.format")

  extname =
    if format == "mp3" then
      "mp3"
    elsif format == "ogg_vorbis" then
      "ogg"
    else
      "wav"
    end

  aws = "#{aws} polly synthesize-speech --output-format #{format}"

  voice_id = get(default="Joanna","protocol.aws.polly.voice")

  cmd = "#{aws} --text #{quote(text)} --voice-id #{quote(voice_id)} $(output)"

  [process_uri(extname=extname,cmd)]
end
add_protocol(static=true,"polly",polly_protocol,
             doc="Generate speech synthesis using AWS polly service. \
                  Result might be mono, needs aws binary in the path.",
             syntax="polly:Text to read")