diff --git a/lib/pinchflat/downloading/download_option_builder.ex b/lib/pinchflat/downloading/download_option_builder.ex index 0627bd6..5745012 100644 --- a/lib/pinchflat/downloading/download_option_builder.ex +++ b/lib/pinchflat/downloading/download_option_builder.ex @@ -124,9 +124,18 @@ defmodule Pinchflat.Downloading.DownloadOptionBuilder do [format_sort: "res:#{res},+codec:avc:m4a", remux_video: "mp4"] end + audio_format_precedence = [ + "bestaudio[ext=m4a]", + "bestaudio[ext=mp3]", + "bestaudio", + "best[ext=m4a]", + "best[ext=mp3]", + "best" + ] + case media_profile.preferred_resolution do # Also be aware that :audio disabled all embedding options for subtitles - :audio -> [:extract_audio, format: "bestaudio[ext=m4a]"] + :audio -> [:extract_audio, format: Enum.join(audio_format_precedence, "/")] :"360p" -> video_codec_option.("360") :"480p" -> video_codec_option.("480") :"720p" -> video_codec_option.("720") diff --git a/lib/pinchflat/metadata/metadata_file_helpers.ex b/lib/pinchflat/metadata/metadata_file_helpers.ex index a804ecd..f45d556 100644 --- a/lib/pinchflat/metadata/metadata_file_helpers.ex +++ b/lib/pinchflat/metadata/metadata_file_helpers.ex @@ -54,24 +54,36 @@ defmodule Pinchflat.Metadata.MetadataFileHelpers do @doc """ Downloads and stores a thumbnail for a media item, returning the filepath. - Chooses the highest quality jpg thumbnail available. + Chooses the highest quality thumbnail available (preferring jpg). Returns + nil if no thumbnails are available. - Returns binary() + Returns binary() | nil """ def download_and_store_thumbnail_for(database_record, metadata_map) do - thumbnail_url = - metadata_map["thumbnails"] - |> Enum.filter(&(&1["preference"] && String.ends_with?(&1["url"], ".jpg"))) - |> Enum.sort(&(&1["preference"] >= &2["preference"])) - |> List.first() - |> Map.get("url") + thumbnails = + (metadata_map["thumbnails"] || []) + # Give it a low preference if the `preference` key doesn't exist + |> Enum.map(&Map.put_new(&1, "preference", -1000)) + # Give it a low preference if image isn't a jpg + |> Enum.map(fn t -> + preference_weight = if String.ends_with?(t["url"], ".jpg"), do: t["preference"], else: t["preference"] - 1000 - filepath = generate_filepath_for(database_record, Path.basename(thumbnail_url)) - thumbnail_blob = fetch_thumbnail_from_url(thumbnail_url) + Map.put(t, "preference", preference_weight) + end) - :ok = FilesystemUtils.write_p!(filepath, thumbnail_blob) + case Enum.sort_by(thumbnails, & &1["preference"], :desc) do + [thumbnail_map | _] -> + thumbnail_url = thumbnail_map["url"] + filepath = generate_filepath_for(database_record, Path.basename(thumbnail_url)) + thumbnail_blob = fetch_thumbnail_from_url(thumbnail_url) - filepath + :ok = FilesystemUtils.write_p!(filepath, thumbnail_blob) + + filepath + + _ -> + nil + end end @doc """ diff --git a/lib/pinchflat/metadata/metadata_parser.ex b/lib/pinchflat/metadata/metadata_parser.ex index 1173001..ef048b8 100644 --- a/lib/pinchflat/metadata/metadata_parser.ex +++ b/lib/pinchflat/metadata/metadata_parser.ex @@ -30,7 +30,7 @@ defmodule Pinchflat.Metadata.MetadataParser do original_url: metadata["original_url"], description: metadata["description"], media_filepath: metadata["filepath"], - livestream: metadata["was_live"], + livestream: !!metadata["was_live"], duration_seconds: metadata["duration"] && round(metadata["duration"]) } end diff --git a/lib/pinchflat/yt_dlp/media.ex b/lib/pinchflat/yt_dlp/media.ex index 7a6c1c5..70a3e97 100644 --- a/lib/pinchflat/yt_dlp/media.ex +++ b/lib/pinchflat/yt_dlp/media.ex @@ -87,7 +87,7 @@ defmodule Pinchflat.YtDlp.Media do title: response["title"], description: response["description"], original_url: response["webpage_url"], - livestream: response["was_live"], + livestream: !!response["was_live"], duration_seconds: response["duration"] && round(response["duration"]), short_form_content: response["webpage_url"] && short_form_content?(response), upload_date: response["upload_date"] && MetadataFileHelpers.parse_upload_date(response["upload_date"]) diff --git a/lib/pinchflat_web/controllers/media_items/media_item_html/show.html.heex b/lib/pinchflat_web/controllers/media_items/media_item_html/show.html.heex index 8c481de..0ac630b 100644 --- a/lib/pinchflat_web/controllers/media_items/media_item_html/show.html.heex +++ b/lib/pinchflat_web/controllers/media_items/media_item_html/show.html.heex @@ -32,7 +32,7 @@