-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b90b449
commit 57830b1
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule HlsPlaylist.Segments do | ||
|
||
def generate(duration, segment_length \\ 3) when duration > 0 and segment_length > 0 do | ||
spread_duration(duration, segment_length, []) | ||
end | ||
|
||
defp spread_duration(duration, _segment_length, acc) when duration <= 0 do | ||
Enum.map(acc, fn x -> Float.round((x * 1.0), 2) end) | ||
|> Enum.reverse() | ||
end | ||
|
||
defp spread_duration(duration, segment_length, acc) do | ||
next_duration = min(duration, segment_length) | ||
|
||
spread_duration(duration - next_duration, segment_length, [next_duration | acc]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule HlsPlaylist.Segments.Test do | ||
use ExUnit.Case | ||
|
||
test "it should generate" do | ||
assert HlsPlaylist.Segments.generate(32.21, 3) == [ | ||
3.0, | ||
3.0, | ||
3.0, | ||
3.0, | ||
3.0, | ||
3.0, | ||
3.0, | ||
3.0, | ||
3.0, | ||
3.0, | ||
2.21 | ||
] | ||
end | ||
end |