defmodule AlgoraWeb.ClipperLive do
use AlgoraWeb, {:live_view, container: {:div, []}}
alias Algora.{Library, Clipper, Accounts}
on_mount {AlgoraWeb.UserAuth, :current_user}
@impl true
def render(assigns) do
~H"""
"""
end
@impl true
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:video, nil)
|> assign(:ffmpeg_cmd, nil), layout: false}
end
@impl true
def handle_event("video_loaded", %{"id" => id}, socket) do
{:noreply,
socket
|> assign(:video, Library.get_video!(id))}
end
def handle_event(
"clip",
%{
"from-hh" => from_hh,
"from-mm" => from_mm,
"from-ss" => from_ss,
"to-hh" => to_hh,
"to-mm" => to_mm,
"to-ss" => to_ss
},
socket
) do
from = to_time(from_hh, :hours) + to_time(from_mm, :minutes) + to_time(from_ss, :seconds)
to = to_time(to_hh, :hours) + to_time(to_mm, :minutes) + to_time(to_ss, :seconds)
cmd = Clipper.create_clip(socket.assigns.video, from, to)
{:noreply,
socket
|> assign(:ffmpeg_cmd, cmd)}
end
defp to_time("", _timeframe), do: 0
defp to_time(n, :seconds), do: String.to_integer(n)
defp to_time(n, :minutes), do: String.to_integer(n) * 60
defp to_time(n, :hours), do: String.to_integer(n) * 3600
end