-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprogress_collector.ex
More file actions
59 lines (47 loc) · 1.57 KB
/
progress_collector.ex
File metadata and controls
59 lines (47 loc) · 1.57 KB
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
defmodule Circle.Videos.FFMpeg.ProgressCollector do
@moduledoc """
Module which implements the `Collectable` protocol
to parse the progress from stdout when running FFMpeg with `System.cmd`.
It then broadcasts via pubsub the progress.
"""
require Logger
alias Circle.Videos
defstruct [:video_id, :total_frames]
def new(video_id, total_frames), do: %__MODULE__{video_id: video_id, total_frames: total_frames}
defimpl Collectable, for: __MODULE__ do
def into(coll) do
Logger.metadata(video_id: coll.video_id)
# Initial state (empty buffer)
{:ok,
fn
_, {:cont, output} when is_binary(output) ->
# Send output to the pid whenever there's new output
case Regex.run(~r/frame\=(\d+)/, output) do
[_, frame_str] ->
Videos.pubsub_broadcast(
coll.video_id,
{:postprocessing, coll.video_id, {:progress, progress(coll, frame_str)}}
)
:ok
error ->
Logger.error("ProgressCollector.into/1 error: #{inspect(error)}")
error
end
_, :done ->
# When done, we can also send a :done message to the pid
Videos.pubsub_broadcast(
coll.video_id,
{:postprocessing, coll.video_id, {:progress, :done}}
)
:ok
_, :halt ->
:ok
_, _ ->
:ok
end}
end
defp progress(coll, frame_str) do
round(String.to_integer(frame_str) * 100 / coll.total_frames)
end
end
end