Skip to content

Commit 53bd868

Browse files
authored
Add resolution option (#21)
* added preferred resolution to profiles * Add resolution to download option builder
1 parent c5fcb8f commit 53bd868

File tree

7 files changed

+61
-2
lines changed

7 files changed

+61
-2
lines changed

lib/pinchflat/profiles/media_profile.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ defmodule Pinchflat.Profiles.MediaProfile do
2121
embed_metadata
2222
shorts_behaviour
2323
livestream_behaviour
24+
preferred_resolution
2425
)a
2526

2627
@required_fields ~w(name output_path_template)a
@@ -48,8 +49,10 @@ defmodule Pinchflat.Profiles.MediaProfile do
4849
# livestreams _only_ and ignore regular videos. The redundant case
4950
# is when one is set to :only and the other is set to :exclude.
5051
# See `build_format_clauses` in the Media context for more.
51-
field :shorts_behaviour, Ecto.Enum, values: [:include, :exclude, :only], default: :include
52-
field :livestream_behaviour, Ecto.Enum, values: [:include, :exclude, :only], default: :include
52+
field :shorts_behaviour, Ecto.Enum, values: ~w(include exclude only)a, default: :include
53+
field :livestream_behaviour, Ecto.Enum, values: ~w(include exclude only)a, default: :include
54+
55+
field :preferred_resolution, Ecto.Enum, values: ~w(2160p 1080p 720p 480p 360p)a, default: :"1080p"
5356

5457
has_many :sources, Source
5558

lib/pinchflat/profiles/options/yt_dlp/download_option_builder.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ defmodule Pinchflat.Profiles.Options.YtDlp.DownloadOptionBuilder do
2626
subtitle_options(media_profile) ++
2727
thumbnail_options(media_profile) ++
2828
metadata_options(media_profile) ++
29+
quality_options(media_profile) ++
2930
output_options(media_profile)
3031

3132
{:ok, built_options}
@@ -87,6 +88,19 @@ defmodule Pinchflat.Profiles.Options.YtDlp.DownloadOptionBuilder do
8788
end)
8889
end
8990

91+
defp quality_options(media_profile) do
92+
codec_options = "+codec:avc:m4a"
93+
94+
case media_profile.preferred_resolution do
95+
:"360p" -> [format_sort: "res:360,#{codec_options}"]
96+
:"480p" -> [format_sort: "res:480,#{codec_options}"]
97+
:"720p" -> [format_sort: "res:720,#{codec_options}"]
98+
:"1080p" -> [format_sort: "res:1080,#{codec_options}"]
99+
:"1440p" -> [format_sort: "res:1440,#{codec_options}"]
100+
:"2160p" -> [format_sort: "res:2160,#{codec_options}"]
101+
end
102+
end
103+
90104
defp output_options(media_profile) do
91105
{:ok, output_path} = OutputPathBuilder.build(media_profile.output_path_template)
92106

lib/pinchflat_web/controllers/media_profiles/media_profile_html.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ defmodule PinchflatWeb.MediaProfiles.MediaProfileHTML do
1818
{"Only", :only}
1919
]
2020
end
21+
22+
def friendly_resolution_options do
23+
[
24+
{"2160p", "4k"},
25+
{"1080p", "1080p"},
26+
{"720p", "720p"},
27+
{"480p", "480p"},
28+
{"360p", "360p"}
29+
]
30+
end
2131
end

lib/pinchflat_web/controllers/media_profiles/media_profile_html/index.html.heex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
<:col :let={media_profile} label="Output Template">
2020
<code class="text-sm"><%= media_profile.output_path_template %></code>
2121
</:col>
22+
<:col :let={media_profile} label="Preferred Resolution">
23+
<%= media_profile.preferred_resolution %>
24+
</:col>
2225
<:col :let={media_profile} label="" class="flex place-content-evenly">
2326
<.link
2427
navigate={~p"/media_profiles/#{media_profile.id}"}

lib/pinchflat_web/controllers/media_profiles/media_profile_html/media_profile_form.html.heex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@
6666
label="Include Livestreams?"
6767
/>
6868

69+
<h3 class="mb-4 mt-8 text-2xl text-black dark:text-white">
70+
Quality Options
71+
</h3>
72+
73+
<.input
74+
field={f[:preferred_resolution]}
75+
options={friendly_resolution_options()}
76+
type="select"
77+
label="Preferred Resolution"
78+
help="Will grab the closest available resolution if your preferred is not available"
79+
/>
80+
6981
<:actions>
7082
<.button class="mt-15 mb-5 sm:mb-7.5">Save Media profile</.button>
7183
</:actions>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule Pinchflat.Repo.Migrations.AddPreferredResolutionToMediaProfiles do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:media_profiles) do
6+
add :preferred_resolution, :string, null: false, default: "1080p"
7+
end
8+
end
9+
end

test/pinchflat/profiles/options/yt_dlp/download_option_builder_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,12 @@ defmodule Pinchflat.Profiles.Options.YtDlp.DownloadOptionBuilderTest do
158158
refute :embed_metadata in res
159159
end
160160
end
161+
162+
describe "build/1 when testing quality options" do
163+
test "it includes quality options" do
164+
assert {:ok, res} = DownloadOptionBuilder.build(@media_profile)
165+
166+
assert {:format_sort, "res:1080,+codec:avc:m4a"} in res
167+
end
168+
end
161169
end

0 commit comments

Comments
 (0)