Skip to content

Commit

Permalink
Allow subtitle downloading (#11)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kieraneglin authored Jan 31, 2024
1 parent a5e7c48 commit d25e65f
Show file tree
Hide file tree
Showing 19 changed files with 5,182 additions and 915 deletions.
14 changes: 14 additions & 0 deletions .iex.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
alias Pinchflat.Repo

alias Pinchflat.Tasks.Task
alias Pinchflat.Media.MediaItem
alias Pinchflat.Media.MediaMetadata
alias Pinchflat.MediaSource.Channel
alias Pinchflat.Profiles.MediaProfile

alias Pinchflat.Tasks
alias Pinchflat.Media
alias Pinchflat.Profiles
alias Pinchflat.MediaSource

alias Pinchflat.MediaClient.{ChannelDetails, VideoDownloader}
4 changes: 2 additions & 2 deletions lib/pinchflat/media.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/pinchflat/media/media_item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 25 additions & 3 deletions lib/pinchflat/media_client/backends/yt_dlp/metadata_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 17 additions & 2 deletions lib/pinchflat/profiles/media_profile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
62 changes: 51 additions & 11 deletions lib/pinchflat/profiles/options/yt_dlp/option_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Pinchflat.Repo.Migrations.AddSubtitleOptionsToMediaProfiles do
use Ecto.Migration

def change do
alter table(:media_profiles) do
add :download_subs, :boolean, default: true, null: false
add :download_auto_subs, :boolean, default: true, null: false
add :embed_subs, :boolean, default: true, null: false
add :sub_langs, :string, default: "en", null: false
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Pinchflat.Repo.Migrations.AddSubtitleFilepathsToMediaItem do
use Ecto.Migration

def change do
alter table(:media_items) do
add :subtitle_filepaths, {:array, {:array, :string}}, default: []
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Pinchflat.Repo.Migrations.RenameVideoFilepathOnMediaItems do
use Ecto.Migration

def change do
rename table(:media_items), :video_filepath, to: :media_filepath
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.MediaParserTest do
}}
end

describe "parse_for_media_item/1" do
describe "parse_for_media_item/1 when testing media metadata" do
test "it extracts the video filepath", %{metadata: metadata} do
result = Parser.parse_for_media_item(metadata)

assert String.contains?(result.video_filepath, "bwRHIkYqYJo")
assert String.ends_with?(result.video_filepath, ".mkv")
assert String.contains?(result.media_filepath, "bwRHIkYqYJo")
assert String.ends_with?(result.media_filepath, ".mkv")
end

test "it extracts the title", %{metadata: metadata} do
Expand All @@ -42,4 +42,45 @@ defmodule Pinchflat.MediaClient.Backends.YtDlp.MediaParserTest do
assert result.metadata.client_response == metadata
end
end

describe "parse_for_media_item/1 when testing subtitle metadata" do
test "extracts the subtitle filepaths", %{metadata: metadata} do
result = Parser.parse_for_media_item(metadata)

assert [["de", german_filepath], ["en", english_filepath]] = result.subtitle_filepaths

assert String.ends_with?(english_filepath, ".en.srt")
assert String.ends_with?(german_filepath, ".de.srt")
end

test "sorts the subtitle filepaths by language", %{metadata: metadata} do
metadata =
Map.put(metadata, "requested_subtitles", %{
"en" => %{"filepath" => "en.srt"},
"za" => %{"filepath" => "za.srt"},
"de" => %{"filepath" => "de.srt"},
"al" => %{"filepath" => "al.srt"}
})

result = Parser.parse_for_media_item(metadata)

assert [["al", _], ["de", _], ["en", _], ["za", _]] = result.subtitle_filepaths
end

test "doesn't freak out if the video has no subtitles", %{metadata: metadata} do
metadata = Map.put(metadata, "requested_subtitles", %{})

result = Parser.parse_for_media_item(metadata)

assert result.subtitle_filepaths == []
end

test "doesn't freak out if the requested_subtitles key is missing", %{metadata: metadata} do
metadata = Map.delete(metadata, "requested_subtitles")

result = Parser.parse_for_media_item(metadata)

assert result.subtitle_filepaths == []
end
end
end
7 changes: 4 additions & 3 deletions test/pinchflat/media_client/video_downloader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Pinchflat.MediaClient.VideoDownloaderTest do
setup do
media_item =
Repo.preload(
media_item_fixture(%{title: nil, video_filepath: nil}),
media_item_fixture(%{title: nil, media_filepath: nil}),
[:metadata, channel: :media_profile]
)

Expand All @@ -33,10 +33,11 @@ defmodule Pinchflat.MediaClient.VideoDownloaderTest do
{:ok, render_metadata(:media_metadata)}
end)

assert %{video_filepath: nil, title: nil} = media_item
assert %{media_filepath: nil, title: nil, subtitle_filepaths: []} = media_item
assert {:ok, updated_media_item} = VideoDownloader.download_for_media_item(media_item)
assert updated_media_item.video_filepath
assert updated_media_item.media_filepath
assert updated_media_item.title
assert length(updated_media_item.subtitle_filepaths) > 0
end

test "it saves the metadata to the database", %{media_item: media_item} do
Expand Down
Loading

0 comments on commit d25e65f

Please sign in to comment.