[WIP] started on codec parser

This commit is contained in:
Kieran Eglin 2024-05-21 12:36:42 -07:00
parent dec3938780
commit 4fdd07fc58
No known key found for this signature in database
GPG key ID: 193984967FCF432D
3 changed files with 70 additions and 10 deletions

View file

@ -0,0 +1,49 @@
defmodule Pinchflat.Downloading.CodecParser do
# TODO: test
def generate_vcodec_string(nil), do: "bestvideo[vcodec~='^avc']/bestvideo"
def generate_vcodec_string([]), do: generate_vcodec_string(nil)
def generate_vcodec_string(video_codecs) do
video_codecs
|> Enum.map(&video_codec_map()[&1])
|> Enum.reject(&is_nil/1)
|> Enum.map(&"bestvideo[vcodec~='^#{&1}']")
|> Enum.concat(["bestvideo", "best"])
|> Enum.join("/")
end
# TODO: test
def generate_acodec_string(nil), do: "bestaudio[acodec~='^mp4a']/bestaudio[acodec~='^mp3']/bestaudio"
def generate_acodec_string([]), do: generate_acodec_string(nil)
def generate_acodec_string(audio_codecs) do
audio_codecs
|> Enum.map(&audio_codec_map()[&1])
|> Enum.reject(&is_nil/1)
|> Enum.map(&"bestaudio[acodec~='^#{&1}']")
|> Enum.concat(["bestaudio", "best"])
|> Enum.join("/")
end
defp video_codec_map do
%{
"av01" => "av01",
"avc" => "avc",
"vp9" => "vp0?9"
}
end
defp audio_codec_map do
%{
"flac" => "flac",
"alac" => "alac",
"wav" => "wav",
"aiff" => "aiff",
"aac" => "aac",
"mp4a" => "mp4a",
"mp3" => "mp3",
"opus" => "opus",
"vorbis" => "vorbis"
}
end
end

View file

@ -6,6 +6,7 @@ defmodule Pinchflat.Downloading.DownloadOptionBuilder do
alias Pinchflat.Sources alias Pinchflat.Sources
alias Pinchflat.Sources.Source alias Pinchflat.Sources.Source
alias Pinchflat.Media.MediaItem alias Pinchflat.Media.MediaItem
alias Pinchflat.Downloading.CodecParser
alias Pinchflat.Downloading.OutputPathBuilder alias Pinchflat.Downloading.OutputPathBuilder
alias Pinchflat.Utils.FilesystemUtils, as: FSUtils alias Pinchflat.Utils.FilesystemUtils, as: FSUtils
@ -120,23 +121,32 @@ defmodule Pinchflat.Downloading.DownloadOptionBuilder do
end) end)
end end
# TODO: test
defp quality_options(media_profile) do defp quality_options(media_profile) do
vcodec_string = CodecParser.generate_vcodec_string(["vp9", "av01"])
acodec_string = CodecParser.generate_acodec_string(["opus", "aac", "mp4a"])
video_codec_option = fn res -> video_codec_option = fn res ->
[format_sort: "res:#{res},+codec:avc:m4a", remux_video: "mp4"] [
format_sort: "res:#{res}",
# Since Plex doesn't support reading metadata from MKV
remux_video: "mp4",
format: "((#{vcodec_string})+(#{acodec_string}))/best"
]
end end
audio_format_precedence = [ # audio_format_precedence = [
"bestaudio[ext=m4a]", # "bestaudio[ext=m4a]",
"bestaudio[ext=mp3]", # "bestaudio[ext=mp3]",
"bestaudio", # "bestaudio",
"best[ext=m4a]", # "best[ext=m4a]",
"best[ext=mp3]", # "best[ext=mp3]",
"best" # "best"
] # ]
case media_profile.preferred_resolution do case media_profile.preferred_resolution do
# Also be aware that :audio disabled all embedding options for subtitles # Also be aware that :audio disabled all embedding options for subtitles
:audio -> [:extract_audio, format: Enum.join(audio_format_precedence, "/")] :audio -> [:extract_audio, format: "#{acodec_string}/best"]
:"360p" -> video_codec_option.("360") :"360p" -> video_codec_option.("360")
:"480p" -> video_codec_option.("480") :"480p" -> video_codec_option.("480")
:"720p" -> video_codec_option.("720") :"720p" -> video_codec_option.("720")

View file

@ -15,6 +15,7 @@ defmodule PinchflatWeb.MediaItems.MediaItemHTML do
!!media_item.media_filepath and File.exists?(media_item.media_filepath) !!media_item.media_filepath and File.exists?(media_item.media_filepath)
end end
# TODO: update for new format types
def media_type(media_item) do def media_type(media_item) do
case Path.extname(media_item.media_filepath) do case Path.extname(media_item.media_filepath) do
ext when ext in [".mp4", ".webm", ".mkv"] -> :video ext when ext in [".mp4", ".webm", ".mkv"] -> :video