Skip to content

Commit

Permalink
feat: add segments generate
Browse files Browse the repository at this point in the history
  • Loading branch information
trueChazza committed Mar 20, 2024
1 parent b90b449 commit 57830b1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/hls_playlist_segments.ex
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
19 changes: 19 additions & 0 deletions test/hls_playlist_segments_test.exs
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

0 comments on commit 57830b1

Please sign in to comment.