Allow subtitle downloading (#11)
* Added subtitle options to media profile model * Updated media profile form * Adds subtitle-based options in options builder * Updates metadata parser to include subtitles * Adds subtitle_filepaths to media_item * renamed video_filepath to media_filepath * Added more fields to media profile show page
This commit is contained in:
@@ -19,14 +19,14 @@ defmodule Pinchflat.Media do
|
||||
|
||||
@doc """
|
||||
Returns a list of pending media_items for a given channel, where
|
||||
pending means the `video_filepath` is `nil`.
|
||||
pending means the `media_filepath` is `nil`.
|
||||
|
||||
Returns [%MediaItem{}, ...].
|
||||
"""
|
||||
def list_pending_media_items_for(%Channel{} = channel) do
|
||||
from(
|
||||
m in MediaItem,
|
||||
where: m.channel_id == ^channel.id and is_nil(m.video_filepath)
|
||||
where: m.channel_id == ^channel.id and is_nil(m.media_filepath)
|
||||
)
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
@@ -11,12 +11,16 @@ defmodule Pinchflat.Media.MediaItem do
|
||||
alias Pinchflat.Media.MediaMetadata
|
||||
|
||||
@required_fields ~w(media_id channel_id)a
|
||||
@allowed_fields ~w(title media_id video_filepath channel_id)a
|
||||
@allowed_fields ~w(title media_id media_filepath channel_id subtitle_filepaths)a
|
||||
|
||||
schema "media_items" do
|
||||
field :title, :string
|
||||
field :media_id, :string
|
||||
field :video_filepath, :string
|
||||
field :media_filepath, :string
|
||||
# This is an array of [iso-2 language, filepath] pairs. Probably could
|
||||
# be an associated record, but I don't see the benefit right now.
|
||||
# Will very likely revisit because I can't leave well-enough alone.
|
||||
field :subtitle_filepaths, {:array, {:array, :string}}, default: []
|
||||
|
||||
belongs_to :channel, Channel
|
||||
|
||||
|
||||
@@ -16,12 +16,34 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.MetadataParser do
|
||||
Returns map()
|
||||
"""
|
||||
def parse_for_media_item(metadata) do
|
||||
%{
|
||||
title: metadata["title"],
|
||||
video_filepath: metadata["filepath"],
|
||||
metadata_attrs = %{
|
||||
metadata: %{
|
||||
client_response: metadata
|
||||
}
|
||||
}
|
||||
|
||||
metadata_attrs
|
||||
|> Map.merge(parse_media_metadata(metadata))
|
||||
|> Map.merge(parse_subtitle_metadata(metadata))
|
||||
end
|
||||
|
||||
defp parse_media_metadata(metadata) do
|
||||
%{
|
||||
title: metadata["title"],
|
||||
media_filepath: metadata["filepath"]
|
||||
}
|
||||
end
|
||||
|
||||
defp parse_subtitle_metadata(metadata) do
|
||||
subtitle_map = metadata["requested_subtitles"] || %{}
|
||||
# IDEA: if needed, consider filtering out subtitles that don't exist on-disk
|
||||
subtitle_filepaths =
|
||||
subtitle_map
|
||||
|> Enum.map(fn {lang, attrs} -> [lang, attrs["filepath"]] end)
|
||||
|> Enum.sort(fn [lang_a, _], [lang_b, _] -> lang_a < lang_b end)
|
||||
|
||||
%{
|
||||
subtitle_filepaths: subtitle_filepaths
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,9 +8,24 @@ defmodule Pinchflat.Profiles.MediaProfile do
|
||||
|
||||
alias Pinchflat.MediaSource.Channel
|
||||
|
||||
@allowed_fields ~w(
|
||||
name
|
||||
output_path_template
|
||||
download_subs
|
||||
download_auto_subs
|
||||
embed_subs
|
||||
sub_langs
|
||||
)a
|
||||
|
||||
@required_fields ~w(name output_path_template)a
|
||||
|
||||
schema "media_profiles" do
|
||||
field :name, :string
|
||||
field :output_path_template, :string
|
||||
field :download_subs, :boolean, default: true
|
||||
field :download_auto_subs, :boolean, default: true
|
||||
field :embed_subs, :boolean, default: true
|
||||
field :sub_langs, :string, default: "en"
|
||||
|
||||
has_many :channels, Channel
|
||||
|
||||
@@ -20,8 +35,8 @@ defmodule Pinchflat.Profiles.MediaProfile do
|
||||
@doc false
|
||||
def changeset(media_profile, attrs) do
|
||||
media_profile
|
||||
|> cast(attrs, [:name, :output_path_template])
|
||||
|> validate_required([:name, :output_path_template])
|
||||
|> cast(attrs, @allowed_fields)
|
||||
|> validate_required(@required_fields)
|
||||
|> unique_constraint(:name)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -15,22 +15,62 @@ defmodule Pinchflat.Profiles.Options.YtDlp.OptionBuilder do
|
||||
these options
|
||||
"""
|
||||
def build(%MediaProfile{} = media_profile) do
|
||||
{:ok, output_path} = OutputPathBuilder.build(media_profile.output_path_template)
|
||||
|
||||
# NOTE: I'll be hardcoding most things for now (esp. options to help me test) -
|
||||
# add more configuration later as I build out the models. Walk before you can run!
|
||||
|
||||
# NOTE: Looks like you can put different media types in different directories.
|
||||
# see: https://github.com/yt-dlp/yt-dlp#output-template
|
||||
{:ok,
|
||||
[
|
||||
:embed_metadata,
|
||||
:embed_thumbnail,
|
||||
:embed_subs,
|
||||
:no_progress,
|
||||
sub_langs: "en.*",
|
||||
output: Path.join(base_directory(), output_path)
|
||||
]}
|
||||
|
||||
built_options =
|
||||
default_options() ++
|
||||
subtitle_options(media_profile) ++
|
||||
output_options(media_profile)
|
||||
|
||||
{:ok, built_options}
|
||||
end
|
||||
|
||||
# This will be updated a lot as I add new options to profiles
|
||||
defp default_options do
|
||||
[
|
||||
:embed_metadata,
|
||||
:embed_thumbnail,
|
||||
:no_progress
|
||||
]
|
||||
end
|
||||
|
||||
defp subtitle_options(media_profile) do
|
||||
mapped_struct = Map.from_struct(media_profile)
|
||||
|
||||
Enum.reduce(mapped_struct, [], fn attr, acc ->
|
||||
case {attr, media_profile} do
|
||||
{{:download_subs, true}, _} ->
|
||||
# Force SRT for now - MAY provide as an option in the future
|
||||
acc ++ [:write_subs, convert_subs: "srt"]
|
||||
|
||||
{{:download_auto_subs, true}, %{download_subs: true}} ->
|
||||
acc ++ [:write_auto_subs]
|
||||
|
||||
{{:embed_subs, true}, _} ->
|
||||
acc ++ [:embed_subs]
|
||||
|
||||
{{:sub_langs, sub_langs}, %{download_subs: true}} ->
|
||||
acc ++ [sub_langs: sub_langs]
|
||||
|
||||
{{:sub_langs, sub_langs}, %{embed_subs: true}} ->
|
||||
acc ++ [sub_langs: sub_langs]
|
||||
|
||||
_ ->
|
||||
acc
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp output_options(media_profile) do
|
||||
{:ok, output_path} = OutputPathBuilder.build(media_profile.output_path_template)
|
||||
|
||||
[
|
||||
output: Path.join(base_directory(), output_path)
|
||||
]
|
||||
end
|
||||
|
||||
defp base_directory do
|
||||
|
||||
+4
@@ -4,6 +4,10 @@
|
||||
</.error>
|
||||
<.input field={f[:name]} type="text" label="Name" />
|
||||
<.input field={f[:output_path_template]} type="text" label="Output path template" />
|
||||
<.input field={f[:download_subs]} type="checkbox" label="Download Subs" />
|
||||
<.input field={f[:download_auto_subs]} type="checkbox" label="Download Autogenerated Subs" />
|
||||
<.input field={f[:embed_subs]} type="checkbox" label="Embed Subs" />
|
||||
<.input field={f[:sub_langs]} type="text" label="Sub Langs" />
|
||||
<:actions>
|
||||
<.button>Save Media profile</.button>
|
||||
</:actions>
|
||||
|
||||
@@ -9,8 +9,14 @@
|
||||
</.header>
|
||||
|
||||
<.list>
|
||||
<:item title="Name"><%= @media_profile.name %></:item>
|
||||
<:item title="Output path template"><%= @media_profile.output_path_template %></:item>
|
||||
<:item
|
||||
:for={
|
||||
attr <- ~w(name output_path_template download_subs download_auto_subs embed_subs sub_langs)a
|
||||
}
|
||||
title={attr}
|
||||
>
|
||||
<%= Map.get(@media_profile, attr) %>
|
||||
</:item>
|
||||
</.list>
|
||||
|
||||
<.back navigate={~p"/media_profiles"}>Back to media_profiles</.back>
|
||||
|
||||
Reference in New Issue
Block a user