summaryrefslogtreecommitdiff
path: root/mpv
diff options
context:
space:
mode:
Diffstat (limited to 'mpv')
-rw-r--r--mpv/input.conf2
-rw-r--r--mpv/mpv.conf5
-rw-r--r--mpv/script-opts/quality-menu.conf95
-rw-r--r--mpv/script-opts/youtube-download.conf48
-rw-r--r--mpv/scripts/quality-menu.lua806
-rw-r--r--mpv/scripts/youtube-download.lua758
6 files changed, 0 insertions, 1714 deletions
diff --git a/mpv/input.conf b/mpv/input.conf
deleted file mode 100644
index d28ab28..0000000
--- a/mpv/input.conf
+++ /dev/null
@@ -1,2 +0,0 @@
-Ctrl+f script-binding quality_menu/video_formats_toggle
-Ctrl+a script-binding quality_menu/audio_formats_toggle
diff --git a/mpv/mpv.conf b/mpv/mpv.conf
deleted file mode 100644
index 649af35..0000000
--- a/mpv/mpv.conf
+++ /dev/null
@@ -1,5 +0,0 @@
-save-position-on-quit
-ao=pipewire,pulse,jack
-vo=gpu
-hwdec=yes
-audio-channels=2
diff --git a/mpv/script-opts/quality-menu.conf b/mpv/script-opts/quality-menu.conf
deleted file mode 100644
index 80ab845..0000000
--- a/mpv/script-opts/quality-menu.conf
+++ /dev/null
@@ -1,95 +0,0 @@
-# KEY BINDINGS
-
-# move the menu cursor up
-up_binding=UP WHEEL_UP
-# move the menu cursor down
-down_binding=DOWN WHEEL_DOWN
-# select menu entry
-select_binding=ENTER MBTN_LEFT
-# close menu
-close_menu_binding=ESC MBTN_RIGHT Ctrl+f Alt+f
-
-# youtube-dl version(could be youtube-dl or yt-dlp, or something else)
-ytdl_ver=yt-dlp
-
-# formatting / cursors
-selected_and_active=▶ -
-selected_and_inactive=● -
-unselected_and_active=▷ -
-unselected_and_inactive=○ -
-
-# font size scales by window, if false requires larger font and padding sizes
-scale_playlist_by_window=yes
-
-# playlist ass style overrides inside curly brackets, \keyvalue is one field, extra \ for escape in lua
-# example {\\fnUbuntu\\fs10\\b0\\bord1} equals: font=Ubuntu, size=10, bold=no, border=1
-# read http://docs.aegisub.org/3.2/ASS_Tags/ for reference of tags
-# undeclared tags will use default osd settings
-# these styles will be used for the whole playlist. More specific styling will need to be hacked in
-#
-# (a monospaced font is recommended but not required)
-style_ass_tags={\\fnmonospace\\fs10\\bord1}
-
-# paddings for top left corner
-text_padding_x=5
-text_padding_y=5
-
-# how many seconds until the quality menu times out
-# setting this to 0 deactivates the timeout
-menu_timeout=6
-
-# use youtube-dl to fetch a list of available formats (overrides quality_strings)
-fetch_formats=yes
-
-# list of ytdl-format strings to choose from
-quality_strings=[ {"4320p" : "bestvideo[height<=?4320p]+bestaudio/best"}, {"2160p" : "bestvideo[height<=?2160]+bestaudio/best"}, {"1440p" : "bestvideo[height<=?1440]+bestaudio/best"}, {"1080p" : "bestvideo[height<=?1080]+bestaudio/best"}, {"720p" : "bestvideo[height<=?720]+bestaudio/best"}, {"480p" : "bestvideo[height<=?480]+bestaudio/best"}, {"360p" : "bestvideo[height<=?360]+bestaudio/best"}, {"240p" : "bestvideo[height<=?240]+bestaudio/best"}, {"144p" : "bestvideo[height<=?144]+bestaudio/best"} ]
-
-# reset youtube-dl format to the original format string when changing files (e.g. going to the next playlist entry)
-# if file was opened previously, reset to previously selected format
-reset_format=yes
-
-# automatically fetch available formats when opening an url
-fetch_on_start=yes
-
-# show the video format menu after opening an url
-start_with_menu=no
-
-# include unknown formats in the list
-# Unfortunately choosing which formats are video or audio is not always perfect.
-# Set to true to make sure you don't miss any formats, but then the list
-# might also include formats that aren't actually video or audio.
-# Formats that are known to not be video or audio are still filtered out.
-include_unknown=no
-
-# hide columns that are identical for all formats
-hide_identical_columns=yes
-
-# which columns are shown in which order
-# comma separated list, prefix column with "-" to align left
-#
-# columns that might be useful are:
-# resolution, width, height, fps, dynamic_range, tbr, vbr, abr, asr,
-# filesize, filesize_approx, vcodec, acodec, ext, video_ext, audio_ext,
-# language, format, format_note, quality
-#
-# columns that are derived from the above, but with special treatment:
-# frame_rate, bitrate_total, bitrate_video, bitrate_audio,
-# codec_video, codec_audio, audio_sample_rate
-#
-# If those still aren't enough or you're just curious, run:
-# yt-dlp -j <url>
-# This outputs unformatted JSON.
-# Format it and look under "formats" to see what's available.
-#
-# Not all videos have all columns available.
-# Be careful, misspelled columns simply won't be displayed, there is no error.
-columns_video=-resolution,frame_rate,dynamic_range,language,bitrate_total,size,-codec_video,-codec_audio
-columns_audio=audio_sample_rate,bitrate_total,size,language,-codec_audio
-
-# columns used for sorting, see "columns_video" for available columns
-# comma separated list, prefix column with "-" to reverse sorting order
-# Leaving this empty keeps the order from yt-dlp/youtube-dl.
-# Be careful, misspelled columns won't result in an error,
-# but they might influence the result.
-sort_video=height,fps,tbr,size,format_id
-sort_audio=asr,tbr,size,format_id
diff --git a/mpv/script-opts/youtube-download.conf b/mpv/script-opts/youtube-download.conf
deleted file mode 100644
index 33630bf..0000000
--- a/mpv/script-opts/youtube-download.conf
+++ /dev/null
@@ -1,48 +0,0 @@
-# KEY BINDINGS
-download_video_binding=ctrl+d
-download_audio_binding=ctrl+a
-#download_subtitle_binding=ctrl+s
-#download_video_embed_subtitle_binding=ctrl+i
-#select_range_binding=ctrl+r
-
-# Specify audio format: "best", "aac","flac", "mp3", "m4a", "opus", "vorbis", or "wav"
-audio_format=best
-
-# Specify ffmpeg/avconv audio quality
-# insert a value between 0 (better) and 9 (worse) for VBR or a specific bitrate like 128K
-audio_quality=0
-
-# Same as youtube-dl --format FORMAT
-# see https://github.com/ytdl-org/youtube-dl/blob/master/README.md#format-selection
-# set to "current" to download the same quality that is currently playing
-video_format=best
-
-# Encode the video to another format if necessary (currently supported: mp4|flv|ogg|webm|mkv|avi)
-# recode_video="mp4"
-
-# Restrict filenames to only ASCII characters, and avoid "&" and spaces in filenames
-restrict_filenames=yes
-
-# Download the whole playlist (no) or only one video (yes)
-# Same as youtube-dl --no-playlist
-no_playlist=yes
-
-# Use an archive file, see youtube-dl --download-archive
-# You have these options:
-# * Set to empty string "" to not use an archive file
-# * Set an absolute path to use one archive for all downloads e.g. download_archive="/home/user/archive.txt"
-# * Set a relative path/only a filename to use one archive per directory e.g. download_archive="archive.txt"
-# * Use $PLAYLIST to create one archive per playlist e.g. download_archive="/home/user/archives/$PLAYLIST.txt"
-download_archive=~/Videos/archive/archive.txt
-
-# Filename or full path
-# Same youtube-dl -o
-# see https://github.com/ytdl-org/youtube-dl/blob/master/README.md#output-template
-filename=~/Videos/%(title)s.%(ext)s
-
-# Use a cookies file for youtube-dl
-# Same as youtube-dl --cookies
-#cookies=C:\Users\username\cookies.txt
-
-# Write download errors to a log file
-log_file=~/.config/mpv/download.log
diff --git a/mpv/scripts/quality-menu.lua b/mpv/scripts/quality-menu.lua
deleted file mode 100644
index c5da0d3..0000000
--- a/mpv/scripts/quality-menu.lua
+++ /dev/null
@@ -1,806 +0,0 @@
--- quality-menu.lua
---
--- Change the stream video and audio quality on the fly.
---
--- Usage:
--- add bindings to input.conf:
--- Ctrl+f script-message-to quality_menu video_formats_toggle
--- Alt+f script-message-to quality_menu audio_formats_toggle
---
--- Displays a menu that lets you switch to different ytdl-format settings while
--- you're in the middle of a video (just like you were using the web player).
-
-local mp = require 'mp'
-local utils = require 'mp.utils'
-local msg = require 'mp.msg'
-local assdraw = require 'mp.assdraw'
-local opt = require('mp.options')
-
-local opts = {
- --key bindings
- up_binding = "UP WHEEL_UP",
- down_binding = "DOWN WHEEL_DOWN",
- select_binding = "ENTER MBTN_LEFT",
- close_menu_binding = "ESC MBTN_RIGHT Ctrl+f Alt+f",
-
- --youtube-dl version(could be youtube-dl or yt-dlp, or something else)
- ytdl_ver = "yt-dlp",
-
- --formatting / cursors
- selected_and_active = "▶ - ",
- selected_and_inactive = "● - ",
- unselected_and_active = "▷ - ",
- unselected_and_inactive = "○ - ",
-
- --font size scales by window, if false requires larger font and padding sizes
- scale_playlist_by_window=true,
-
- --playlist ass style overrides inside curly brackets, \keyvalue is one field, extra \ for escape in lua
- --example {\\fnUbuntu\\fs10\\b0\\bord1} equals: font=Ubuntu, size=10, bold=no, border=1
- --read http://docs.aegisub.org/3.2/ASS_Tags/ for reference of tags
- --undeclared tags will use default osd settings
- --these styles will be used for the whole playlist. More specific styling will need to be hacked in
- --
- --(a monospaced font is recommended but not required)
- style_ass_tags = "{\\fnmonospace\\fs10\\bord1}",
-
- --paddings for top left corner
- text_padding_x = 5,
- text_padding_y = 5,
-
- --how many seconds until the quality menu times out
- --setting this to 0 deactivates the timeout
- menu_timeout = 6,
-
- --use youtube-dl to fetch a list of available formats (overrides quality_strings)
- fetch_formats = true,
-
- --default menu entries
- quality_strings=[[
- [
- {"4320p" : "bestvideo[height<=?4320p]+bestaudio/best"},
- {"2160p" : "bestvideo[height<=?2160]+bestaudio/best"},
- {"1440p" : "bestvideo[height<=?1440]+bestaudio/best"},
- {"1080p" : "bestvideo[height<=?1080]+bestaudio/best"},
- {"720p" : "bestvideo[height<=?720]+bestaudio/best"},
- {"480p" : "bestvideo[height<=?480]+bestaudio/best"},
- {"360p" : "bestvideo[height<=?360]+bestaudio/best"},
- {"240p" : "bestvideo[height<=?240]+bestaudio/best"},
- {"144p" : "bestvideo[height<=?144]+bestaudio/best"}
- ]
- ]],
-
- --reset ytdl-format to the original format string when changing files (e.g. going to the next playlist entry)
- --if file was opened previously, reset to previously selected format
- reset_format = true,
-
- --automatically fetch available formats when opening an url
- fetch_on_start = true,
-
- --show the video format menu after opening an url
- start_with_menu = false,
-
- --include unknown formats in the list
- --Unfortunately choosing which formats are video or audio is not always perfect.
- --Set to true to make sure you don't miss any formats, but then the list
- --might also include formats that aren't actually video or audio.
- --Formats that are known to not be video or audio are still filtered out.
- include_unknown = false,
-
- --hide columns that are identical for all formats
- hide_identical_columns = true,
-
- --which columns are shown in which order
- --comma separated list, prefix column with "-" to align left
- --
- --columns that might be useful are:
- --resolution, width, height, fps, dynamic_range, tbr, vbr, abr, asr,
- --filesize, filesize_approx, vcodec, acodec, ext, video_ext, audio_ext,
- --language, format, format_note, quality
- --
- --columns that are derived from the above, but with special treatment:
- --frame_rate, bitrate_total, bitrate_video, bitrate_audio,
- --codec_video, codec_audio, audio_sample_rate
- --
- --If those still aren't enough or you're just curious, run:
- --yt-dlp -j <url>
- --This outputs unformatted JSON.
- --Format it and look under "formats" to see what's available.
- --
- --Not all videos have all columns available.
- --Be careful, misspelled columns simply won't be displayed, there is no error.
- columns_video = '-resolution,frame_rate,dynamic_range,language,bitrate_total,size,-codec_video,-codec_audio',
- columns_audio = 'audio_sample_rate,bitrate_total,size,language,-codec_audio',
-
- --columns used for sorting, see "columns_video" for available columns
- --comma separated list, prefix column with "-" to reverse sorting order
- --Leaving this empty keeps the order from yt-dlp/youtube-dl.
- --Be careful, misspelled columns won't result in an error,
- --but they might influence the result.
- sort_video = 'height,fps,tbr,size,format_id',
- sort_audio = 'asr,tbr,size,format_id',
-}
-opt.read_options(opts, "quality-menu")
-opts.quality_strings = utils.parse_json(opts.quality_strings)
-
--- special thanks to reload.lua (https://github.com/4e6/mpv-reload/)
-local function reload_resume()
- local playlist_pos = mp.get_property_number("playlist-pos")
- local reload_duration = mp.get_property_native("duration")
- local time_pos = mp.get_property("time-pos")
-
- mp.set_property_number("playlist-pos", playlist_pos)
-
- -- Tries to determine live stream vs. pre-recorded VOD. VOD has non-zero
- -- duration property. When reloading VOD, to keep the current time position
- -- we should provide offset from the start. Stream doesn't have fixed start.
- -- Decent choice would be to reload stream from it's current 'live' position.
- -- That's the reason we don't pass the offset when reloading streams.
- if reload_duration and reload_duration > 0 then
- local function seeker()
- mp.commandv("seek", time_pos, "absolute")
- mp.unregister_event(seeker)
- end
- mp.register_event("file-loaded", seeker)
- end
-end
-
-local ytdl = {
- path = opts.ytdl_ver,
- searched = false,
- blacklisted = {}
-}
-
-local function process_json(json)
- local function is_video(format)
- -- "none" means it is not a video
- -- nil means it is unknown
- return (opts.include_unknown or format.vcodec) and format.vcodec ~= "none"
- end
-
- local function is_audio(format)
- return (opts.include_unknown or format.acodec) and format.acodec ~= "none"
- end
-
- local vfmt = nil
- local afmt = nil
- local requested_formats = json["requested_formats"] or json["requested_downloads"]
- for _, format in ipairs(requested_formats) do
- if is_video(format) then
- vfmt = format["format_id"]
- elseif is_audio(format) then
- afmt = format["format_id"]
- end
- end
-
- local video_formats = {}
- local audio_formats = {}
- local all_formats = {}
- for i = #json.formats, 1, -1 do
- local format = json.formats[i]
- if is_video(format) then
- video_formats[#video_formats+1] = format
- all_formats[#all_formats+1] = format
- elseif is_audio(format) then
- audio_formats[#audio_formats+1] = format
- all_formats[#all_formats+1] = format
- end
- end
-
- local function populate_special_fields(format)
- format.size = format.filesize or format.filesize_approx
- format.frame_rate = format.fps
- format.bitrate_total = format.tbr
- format.bitrate_video = format.vbr
- format.bitrate_audio = format.abr
- format.codec_video = format.vcodec
- format.codec_audio = format.acodec
- format.audio_sample_rate = format.asr
- end
-
- for _,format in ipairs(all_formats) do
- populate_special_fields(format)
- end
-
- local function strip_minus(list)
- local stripped_list = {}
- local had_minus = {}
- for i, val in ipairs(list) do
- if string.sub(val, 1, 1) == "-" then
- val = string.sub(val, 2)
- had_minus[val] = true
- end
- stripped_list[i] = val
- end
- return stripped_list, had_minus
- end
-
- local function string_split (inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
-
- local sort_video, reverse_video = strip_minus(string_split(opts.sort_video, ','))
- local sort_audio, reverse_audio = strip_minus(string_split(opts.sort_audio, ','))
-
- local function comp(properties, reverse)
- return function (a, b)
- for _,prop in ipairs(properties) do
- local a_val = a[prop]
- local b_val = b[prop]
- if a_val and b_val and type(a_val) ~= 'table' and a_val ~= b_val then
- if reverse[prop] then
- return a_val < b_val
- else
- return a_val > b_val
- end
- end
- end
- return false
- end
- end
-
- if #sort_video > 0 then
- table.sort(video_formats, comp(sort_video, reverse_video))
- end
- if #sort_audio > 0 then
- table.sort(audio_formats, comp(sort_audio, reverse_audio))
- end
-
- local function scale_filesize(size)
- if size == nil then
- return ""
- end
- size = tonumber(size)
-
- local counter = 0
- while size > 1024 do
- size = size / 1024
- counter = counter+1
- end
-
- if counter >= 3 then return string.format("%.1fGiB", size)
- elseif counter >= 2 then return string.format("%.1fMiB", size)
- elseif counter >= 1 then return string.format("%.1fKiB", size)
- else return string.format("%.1fB ", size)
- end
- end
-
- local function scale_bitrate(br)
- if br == nil then
- return ""
- end
- br = tonumber(br)
-
- local counter = 0
- while br > 1000 do
- br = br / 1000
- counter = counter+1
- end
-
- if counter >= 2 then return string.format("%.1fGbps", br)
- elseif counter >= 1 then return string.format("%.1fMbps", br)
- else return string.format("%.1fKbps", br)
- end
- end
-
- local function format_special_fields(format)
- local size_prefix = not format.filesize and format.filesize_approx and "~" or ""
- format.size = (size_prefix) .. scale_filesize(format.size)
- format.frame_rate = format.fps and format.fps.."fps" or ""
- format.bitrate_total = scale_bitrate(format.tbr)
- format.bitrate_video = scale_bitrate(format.vbr)
- format.bitrate_audio = scale_bitrate(format.abr)
- format.codec_video = format.vcodec == nil and "unknown" or format.vcodec == "none" and "" or format.vcodec
- format.codec_audio = format.acodec == nil and "unknown" or format.acodec == "none" and "" or format.acodec
- format.audio_sample_rate = format.asr and tostring(format.asr) .. "Hz" or ""
- end
-
- for _,format in ipairs(all_formats) do
- format_special_fields(format)
- end
-
- local function format_table(formats, columns)
- local function calc_shown_columns()
- local display_col = {}
- local column_widths = {}
- local column_values = {}
- local columns, column_align_left = strip_minus(columns)
-
- for _,format in pairs(formats) do
- for col, prop in ipairs(columns) do
- local label = tostring(format[prop] or "")
- format[prop] = label
-
- if not column_widths[col] or column_widths[col] < label:len() then
- column_widths[col] = label:len()
- end
-
- column_values[col] = column_values[col] or label
- display_col[col] = display_col[col] or (column_values[col] ~= label)
- end
- end
-
- local show_columns={}
- for i, width in ipairs(column_widths) do
- if width > 0 and not opts.hide_identical_columns or display_col[i] then
- local prop = columns[i]
- show_columns[#show_columns+1] = {
- prop=prop,
- width=width,
- align_left=column_align_left[prop]
- }
- end
- end
- return show_columns
- end
-
- local show_columns = calc_shown_columns()
-
- local spacing = 2
- local res = {}
- for _,f in ipairs(formats) do
- local row = ''
- for i,column in ipairs(show_columns) do
- -- lua errors out with width > 99 ("invalid conversion specification")
- local width = math.min(column.width * (column.align_left and -1 or 1), 99)
- row = row .. (i > 1 and string.format('%' .. spacing .. 's', '') or '')
- .. string.format('%' .. width .. 's', f[column.prop] or "")
- end
- res[#res+1] = {label=row, format=f.format_id}
- end
- return res
- end
-
- local columns_video = string_split(opts.columns_video, ',')
- local columns_audio = string_split(opts.columns_audio, ',')
- local vres = format_table(video_formats, columns_video)
- local ares = format_table(audio_formats, columns_audio)
- return vres, ares , vfmt, afmt
-end
-
-local function get_url()
- local path = mp.get_property("path")
- path = string.gsub(path, "ytdl://", "") -- Strip possible ytdl:// prefix.
-
- local function is_url(s)
- -- adapted the regex from https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url
- return nil ~= string.match(path, "^[%w]-://[-a-zA-Z0-9@:%._\\+~#=]+%.[a-zA-Z0-9()][a-zA-Z0-9()]?[a-zA-Z0-9()]?[a-zA-Z0-9()]?[a-zA-Z0-9()]?[a-zA-Z0-9()]?[-a-zA-Z0-9()@:%_\\+.~#?&/=]*")
- end
-
- return is_url(path) and path or nil
-end
-
-local uosc = false
-local url_data={}
-local function process_json_string(url, json)
- local json, err = utils.parse_json(json)
-
- if (json == nil) then
- mp.osd_message("fetching formats failed...", 2)
- msg.error("failed to parse JSON data: " .. err)
- return
- end
-
- if json.formats == nil then
- return
- end
-
- local vres, ares , vfmt, afmt = process_json(json)
- url_data[url] = {voptions=vres, aoptions=ares, vfmt=vfmt, afmt=afmt}
- if uosc and get_url() == url then
- mp.commandv('script-message-to', 'uosc', 'set', 'vformats', #vres)
- mp.commandv('script-message-to', 'uosc', 'set', 'aformats', #ares)
- end
- return vres, ares , vfmt, afmt
-end
-
-local function download_formats(url)
-
- mp.osd_message("fetching available formats with youtube-dl...", 60)
-
- if not (ytdl.searched) then
- local ytdl_mcd = mp.find_config_file(opts.ytdl_ver)
- if not (ytdl_mcd == nil) then
- msg.verbose("found youtube-dl at: " .. ytdl_mcd)
- ytdl.path = ytdl_mcd
- end
- ytdl.searched = true
- end
-
- local function exec(args)
- local res, err = mp.command_native({name = "subprocess", args = args, capture_stdout = true, capture_stderr = true})
- return res.status, res.stdout, res.stderr
- end
-
- local ytdl_format = mp.get_property("ytdl-format")
- local command = nil
- if (ytdl_format == nil or ytdl_format == "") then
- command = {ytdl.path, "--no-warnings", "--no-playlist", "-J", url}
- else
- command = {ytdl.path, "--no-warnings", "--no-playlist", "-J", "-f", ytdl_format, url}
- end
-
- msg.verbose("calling youtube-dl with command: " .. table.concat(command, " "))
-
- local es, stdout, stderr = exec(command)
-
- if (es < 0) or (stdout == nil) or (stdout == "") then
- mp.osd_message("fetching formats failed...", 2)
- msg.error("failed to get format list: " .. es)
- msg.error("stderr: " .. stderr)
- return
- end
-
- msg.verbose("youtube-dl succeeded!")
- mp.osd_message("", 0)
-
- local vres, ares , vfmt, afmt = process_json_string(url, stdout)
- return vres, ares , vfmt, afmt
-end
-
-local function send_formats_to(type, url, script_name, options, format_id)
- mp.commandv('script-message-to', script_name, type .. '_formats', url, utils.format_json(options or {}), format_id or '')
-end
-
-local queue_callback_video = {}
-local queue_callback_audio = {}
-local function get_formats()
-
- local url = get_url()
- if url == nil then
- return
- end
-
- if url_data[url] then
- local data = url_data[url]
- return data.voptions, data.aoptions, data.vfmt, data.afmt, url
- end
-
- if opts.fetch_formats == false then
- local vres = {}
- for i,v in ipairs(opts.quality_strings) do
- for k,v2 in pairs(v) do
- vres[i] = {label = k, format=v2}
- end
- end
- url_data[url] = {voptions=vres, aoptions={}, vfmt=nil, afmt=nil}
- return vres, {}, nil, nil, url
- end
-
- local vres, ares , vfmt, afmt = download_formats(url)
-
- for _, script_name in ipairs(queue_callback_video[url] or {}) do
- send_formats_to('video', url, script_name, vres, vfmt)
- end
- for _, script_name in ipairs(queue_callback_audio[url] or {}) do
- send_formats_to('audio', url, script_name, ares, afmt)
- end
-
- queue_callback_video[url] = nil
- queue_callback_audio[url] = nil
- return vres, ares , vfmt, afmt, url
-end
-
-local function format_string(vfmt, afmt)
- if vfmt and afmt then
- return vfmt.."+"..afmt
- elseif vfmt then
- return vfmt
- elseif afmt then
- return afmt
- else
- return ""
- end
-end
-
-local function set_format(url, vfmt, afmt)
- if (url_data[url].vfmt ~= vfmt or url_data[url].afmt ~= afmt) then
- url_data[url].afmt = afmt
- url_data[url].vfmt = vfmt
- if url == mp.get_property("path") then
- mp.set_property("ytdl-format", format_string(vfmt, afmt))
- reload_resume()
- end
- end
-end
-
-local destroyer = nil
-local function show_menu(isvideo)
-
- if destroyer then
- destroyer()
- end
-
- local voptions, aoptions, vfmt, afmt, url = get_formats()
-
- local options
- local fmt
- if isvideo then
- options = voptions
- fmt = vfmt
- else
- options = aoptions
- fmt = afmt
- end
-
- if options == nil then
- if uosc then
- if isvideo then
- mp.commandv('script-binding', 'uosc/video')
- else
- mp.commandv('script-binding', 'uosc/audio')
- end
- end
-
- return
- end
-
- msg.verbose("current ytdl-format: "..format_string(vfmt, afmt))
-
- local active = 0
- local selected = 1
- --set the cursor to the current format
- if fmt then
- for i,v in ipairs(options) do
- if v.format == fmt then
- active = i
- selected = active
- break
- end
- end
- else
- active = #options + 1
- selected = active
- end
-
- if uosc then
- local menu = {
- title = isvideo and 'Video Formats' or 'Audio Formats',
- items = {},
- type = (isvideo and 'video' or 'audio') .. '_formats',
- }
- for i, option in ipairs(options) do
- menu.items[i] = {
- title = option.label,
- active = i == active,
- value = {
- 'script-message-to',
- 'quality_menu',
- (isvideo and 'video' or 'audio') .. '-format-set',
- url,
- option.format}
- }
- end
- menu.items[#menu.items + 1] = {
- title = 'None',
- value = {
- 'script-message-to',
- 'quality_menu',
- (isvideo and 'video' or 'audio') .. '-format-set',
- url}
- }
- local json = utils.format_json(menu)
- mp.commandv('script-message-to', 'uosc', 'open-menu', json)
- return
- end
-
- local function choose_prefix(i)
- if i == selected and i == active then return opts.selected_and_active
- elseif i == selected then return opts.selected_and_inactive end
-
- if i ~= selected and i == active then return opts.unselected_and_active
- elseif i ~= selected then return opts.unselected_and_inactive end
- return "> " --shouldn't get here.
- end
-
- local function draw_menu()
- local ass = assdraw.ass_new()
-
- ass:pos(opts.text_padding_x, opts.text_padding_y)
- ass:append(opts.style_ass_tags)
-
- if #options > 0 then
- for i,v in ipairs(options) do
- ass:append(choose_prefix(i)..v.label.."\\N")
- end
- ass:append(choose_prefix(#options+1).."None")
- else
- ass:append("no formats found")
- end
-
- local w, h = mp.get_osd_size()
- if opts.scale_playlist_by_window then w,h = 0, 0 end
- mp.set_osd_ass(w, h, ass.text)
- end
-
- local num_options = #options + 1
- local timeout = nil
-
- local function selected_move(amt)
- selected = selected + amt
- if selected < 1 then selected = num_options
- elseif selected > num_options then selected = 1 end
- if timeout then
- timeout:kill()
- timeout:resume()
- end
- draw_menu()
- end
-
- local function bind_keys(keys, name, func, opts)
- if not keys then
- mp.add_forced_key_binding(keys, name, func, opts)
- return
- end
- local i = 1
- for key in keys:gmatch("[^%s]+") do
- local prefix = i == 1 and '' or i
- mp.add_forced_key_binding(key, name..prefix, func, opts)
- i = i + 1
- end
- end
-
- local function unbind_keys(keys, name)
- if not keys then
- mp.remove_key_binding(name)
- return
- end
- local i = 1
- for key in keys:gmatch("[^%s]+") do
- local prefix = i == 1 and '' or i
- mp.remove_key_binding(name..prefix)
- i = i + 1
- end
- end
-
- local function destroy()
- if timeout then
- timeout:kill()
- end
- mp.set_osd_ass(0,0,"")
- unbind_keys(opts.up_binding, "move_up")
- unbind_keys(opts.down_binding, "move_down")
- unbind_keys(opts.select_binding, "select")
- unbind_keys(opts.close_menu_binding, "close")
- destroyer = nil
- end
-
- if opts.menu_timeout > 0 then
- timeout = mp.add_periodic_timer(opts.menu_timeout, destroy)
- end
- destroyer = destroy
-
- bind_keys(opts.up_binding, "move_up", function() selected_move(-1) end, {repeatable=true})
- bind_keys(opts.down_binding, "move_down", function() selected_move(1) end, {repeatable=true})
- if #options > 0 then
- bind_keys(opts.select_binding, "select", function()
- destroy()
- if selected == active then return end
-
- fmt = options[selected] and options[selected].format or nil
- if isvideo then
- vfmt = fmt
- else
- afmt = fmt
- end
- set_format(url, vfmt, afmt)
- end)
- end
- bind_keys(opts.close_menu_binding, "close", destroy) --close menu using ESC
- mp.osd_message("", 0)
- draw_menu()
-end
-
-local ui_callback = {}
-
-local function video_formats_toggle()
- if #ui_callback > 0 then
- for _, name in ipairs(ui_callback) do
- mp.commandv('script-message-to', name, 'video-formats-menu')
- end
- else
- show_menu(true)
- end
-end
-
-local function audio_formats_toggle()
- if #ui_callback > 0 then
- for _, name in ipairs(ui_callback) do
- mp.commandv('script-message-to', name, 'audio-formats-menu')
- end
- else
- show_menu(false)
- end
-end
-
--- keybind to launch menu
-mp.add_key_binding(nil, "video_formats_toggle", video_formats_toggle)
-mp.add_key_binding(nil, "audio_formats_toggle", audio_formats_toggle)
-mp.add_key_binding(nil, "reload", reload_resume)
-
-local original_format = mp.get_property("ytdl-format")
-local path = nil
-local function file_start()
- local new_path = get_url()
- if not new_path then return end
-
- local data = url_data[new_path]
-
- if uosc then
- if data then
- mp.commandv('script-message-to', 'uosc', 'set', 'vformats', #data.voptions)
- mp.commandv('script-message-to', 'uosc', 'set', 'aformats', #data.aoptions)
- else
- mp.commandv('script-message-to', 'uosc', 'set', 'vformats', 0)
- mp.commandv('script-message-to', 'uosc', 'set', 'aformats', 0)
- end
- end
-
- if opts.reset_format and path and new_path ~= path then
- if data then
- msg.verbose("setting previously set format")
- mp.set_property("ytdl-format", format_string(data.vfmt, data.afmt))
- else
- msg.verbose("setting original format")
- mp.set_property("ytdl-format", original_format)
- end
- end
- if opts.start_with_menu and new_path ~= path then
- video_formats_toggle()
- elseif opts.fetch_on_start and not data then
- download_formats(new_path)
- end
- path = new_path
-end
-mp.register_event("start-file", file_start)
-
-mp.register_script_message('video-formats-get', function(url, script_name)
- local data = url_data[url]
- if data then
- send_formats_to('video', url, script_name, data.voptions, data.vfmt)
- else
- local queue = queue_callback_video[url] or {}
- queue[#queue + 1] = script_name
- queue_callback_video[url] = queue
- get_formats()
- end
-end)
-
-mp.register_script_message('audio-formats-get', function(url, script_name)
- local data = url_data[url]
- if data then
- send_formats_to('audio', url, script_name, data.aoptions, data.afmt)
- else
- local queue = queue_callback_audio[url] or {}
- queue[#queue + 1] = script_name
- queue_callback_audio[url] = queue
- get_formats()
- end
-end)
-
-mp.register_script_message('video-format-set', function(url, format_id)
- set_format(url, format_id, url_data[url].afmt)
-end)
-
-mp.register_script_message('audio-format-set', function(url, format_id)
- set_format(url, url_data[url].vfmt, format_id)
-end)
-
-mp.register_script_message('register-ui', function(script_name)
- ui_callback[#ui_callback + 1] = script_name
-end)
-
--- check if uosc is running
-mp.register_script_message('uosc-version', function(version)
- version = tonumber((version:gsub('%.', '')))
----@diagnostic disable-next-line: cast-local-type
- uosc = version and version >= 400
-end)
-mp.commandv('script-message-to', 'uosc', 'get-version', mp.get_script_name()) \ No newline at end of file
diff --git a/mpv/scripts/youtube-download.lua b/mpv/scripts/youtube-download.lua
deleted file mode 100644
index 3050b39..0000000
--- a/mpv/scripts/youtube-download.lua
+++ /dev/null
@@ -1,758 +0,0 @@
--- youtube-download.lua
---
--- Download video/audio from youtube via youtube-dl and ffmpeg/avconv
--- This is forked/based on https://github.com/jgreco/mpv-youtube-quality
---
--- Video download bound to ctrl-d by default.
--- Audio download bound to ctrl-a by default.
-
--- Requires youtube-dl in PATH for video download
--- Requires ffmpeg or avconv in PATH for audio download
-
-local mp = require 'mp'
-local utils = require 'mp.utils'
-local msg = require 'mp.msg'
-
-local opts = {
- -- Key bindings
- -- Set to empty string "" to disable
- download_video_binding = "ctrl+d",
- download_audio_binding = "ctrl+a",
- download_subtitle_binding = "ctrl+s",
- download_video_embed_subtitle_binding = "ctrl+i",
- select_range_binding = "ctrl+r",
-
- -- Specify audio format: "best", "aac","flac", "mp3", "m4a", "opus", "vorbis", or "wav"
- audio_format = "mp3",
-
- -- Specify ffmpeg/avconv audio quality
- -- insert a value between 0 (better) and 9 (worse) for VBR or a specific bitrate like 128K
- audio_quality = "0",
-
- -- Same as youtube-dl --format FORMAT
- -- see https://github.com/ytdl-org/youtube-dl/blob/master/README.md#format-selection
- -- set to "current" to download the same quality that is currently playing
- video_format = "",
-
- -- Encode the video to another format if necessary: "mp4", "flv", "ogg", "webm", "mkv", "avi"
- recode_video = "",
-
- -- Restrict filenames to only ASCII characters, and avoid "&" and spaces in filenames
- restrict_filenames = true,
-
- -- Download the whole playlist (false) or only one video (true)
- -- Same as youtube-dl --no-playlist
- no_playlist = true,
-
- -- Use an archive file, see youtube-dl --download-archive
- -- You have these options:
- -- * Set to empty string "" to not use an archive file
- -- * Set an absolute path to use one archive for all downloads e.g. download_archive="/home/user/archive.txt"
- -- * Set a relative path/only a filename to use one archive per directory e.g. download_archive="archive.txt"
- -- * Use $PLAYLIST to create one archive per playlist e.g. download_archive="/home/user/archives/$PLAYLIST.txt"
- download_archive = "",
-
- -- Use a cookies file for youtube-dl
- -- Same as youtube-dl --cookies
- -- On Windows you need to use a double blackslash or a single fordwardslash
- -- For example "C:\\Users\\Username\\cookies.txt"
- -- Or "C:/Users/Username/cookies.txt"
- cookies = "",
-
- -- Filename or full path
- -- Same as youtube-dl -o FILETEMPLATE
- -- see https://github.com/ytdl-org/youtube-dl/blob/master/README.md#output-template
- -- A relative path or a file name is relative to the path mpv was launched from
- -- On Windows you need to use a double blackslash or a single fordwardslash
- -- For example "C:\\Users\\Username\\Downloads\\%(title)s.%(ext)s"
- -- Or "C:/Users/Username/Downloads/%(title)s.%(ext)s"
- filename = "%(title)s.%(ext)s",
-
- -- Subtitle language
- -- Same as youtube-dl --sub-lang en
- sub_lang = "en",
-
- -- Subtitle format
- -- Same as youtube-dl --sub-format best
- sub_format = "best",
-
- -- Log file for download errors
- log_file = "",
-
-}
-
---Read configuration file
-(require 'mp.options').read_options(opts, "youtube-download")
-
---Read command line arguments
-local ytdl_raw_options = mp.get_property("ytdl-raw-options")
-if ytdl_raw_options ~= nil and ytdl_raw_options:find("cookies=") ~= nil then
- local cookie_file = ytdl_raw_options:match("cookies=([^,]+)")
- if cookie_file ~= nil then
- opts.cookies = cookie_file
- end
-end
-
-local function exec(args, capture_stdout, capture_stderr)
- local ret = mp.command_native({
- name = "subprocess",
- playback_only = false,
- capture_stdout = capture_stdout,
- capture_stderr = capture_stderr,
- args = args,
- })
- return ret.status, ret.stdout, ret.stderr, ret
-end
-
-local function trim(str)
- return str:gsub("^%s+", ""):gsub("%s+$", "")
-end
-
-local function not_empty(str)
- if str == nil or str == "" then
- return false
- end
- return trim(str) ~= ""
-end
-
-local function path_separator()
- return package.config:sub(1,1)
-end
-
-local function path_join(...)
- return table.concat({...}, path_separator())
-end
-
-local function get_current_format()
- -- get the current youtube-dl format or the default value
- local ytdl_format = mp.get_property("options/ytdl-format")
- if not_empty(ytdl_format) then
- return ytdl_format
- end
- ytdl_format = mp.get_property("ytdl-format")
- if not_empty(ytdl_format) then
- return ytdl_format
- end
- return "bestvideo+bestaudio/best"
-end
-
-local DOWNLOAD = {
- VIDEO=1,
- AUDIO=2,
- SUBTITLE=3,
- VIDEO_EMBED_SUBTITLE=4
-}
-local select_range_mode = 0
-local start_time_seconds = nil
-local start_time_formated = nil
-local end_time_seconds = nil
-local end_time_formated = nil
-
-local is_downloading = false
-
-local function disable_select_range()
- -- Disable range mode
- select_range_mode = 0
- -- Remove the arrow key key bindings
- mp.remove_key_binding("select-range-set-up")
- mp.remove_key_binding("select-range-set-down")
- mp.remove_key_binding("select-range-set-left")
- mp.remove_key_binding("select-range-set-right")
-end
-
-
-local function download(download_type)
- local start_time = os.date("%c")
- if is_downloading then
- return
- end
- is_downloading = true
-
- local ass0 = mp.get_property("osd-ass-cc/0")
- local ass1 = mp.get_property("osd-ass-cc/1")
- local url = mp.get_property("path")
-
- url = string.gsub(url, "ytdl://", "") -- Strip possible ytdl:// prefix.
-
- if string.find(url, "//youtu.be/") == nil
- and string.find(url, "//ww.youtu.be/") == nil
- and string.find(url, "//youtube.com/") == nil
- and string.find(url, "//www.youtube.com/") == nil
- then
- mp.osd_message("Not a youtube URL: " .. tostring(url), 10)
- is_downloading = false
- return
- end
-
- local list_match = url:match("list=(%w+)")
- local download_archive = opts.download_archive
- if list_match ~= nil and opts.download_archive ~= nil and opts.download_archive:find("$PLAYLIST", 1, true) then
- download_archive = opts.download_archive:gsub("$PLAYLIST", list_match)
- end
-
- if download_type == DOWNLOAD.AUDIO then
- mp.osd_message("Audio download started", 2)
- elseif download_type == DOWNLOAD.SUBTITLE then
- mp.osd_message("Subtitle download started", 2)
- elseif download_type == DOWNLOAD.VIDEO_EMBED_SUBTITLE then
- mp.osd_message("Video w/ subtitle download started", 2)
- else
- mp.osd_message("Video download started", 2)
- end
-
- -- Compose command line arguments
- local command = {}
-
- local range_mode_file_name = nil
- local range_mode_subtitle_file_name = nil
- local start_time_offset = 0
-
- if select_range_mode == 0 or (select_range_mode > 0 and (download_type == DOWNLOAD.AUDIO or download_type == DOWNLOAD.SUBTITLE)) then
- table.insert(command, "youtube-dl")
- table.insert(command, "--no-overwrites")
- if opts.restrict_filenames then
- table.insert(command, "--restrict-filenames")
- end
- if not_empty(opts.filename) then
- table.insert(command, "-o")
- table.insert(command, opts.filename)
- end
- if opts.no_playlist then
- table.insert(command, "--no-playlist")
- end
- if not_empty(download_archive) then
- table.insert(command, "--download-archive")
- table.insert(command, download_archive)
- end
-
- if download_type == DOWNLOAD.SUBTITLE then
- table.insert(command, "--sub-lang")
- table.insert(command, opts.sub_lang)
- table.insert(command, "--write-sub")
- table.insert(command, "--skip-download")
- if not_empty(opts.sub_format) then
- table.insert(command, "--sub-format")
- table.insert(command, opts.sub_format)
- end
- if select_range_mode > 0 then
- mp.osd_message("Range mode is not available for subtitle-only download", 10)
- is_downloading = false
- return
- end
- elseif download_type == DOWNLOAD.AUDIO then
- table.insert(command, "--extract-audio")
- if not_empty(opts.audio_format) then
- table.insert(command, "--audio-format")
- table.insert(command, opts.audio_format)
- end
- if not_empty(opts.audio_quality) then
- table.insert(command, "--audio-quality")
- table.insert(command, opts.audio_quality)
- end
- if select_range_mode > 0 then
- local start_time_str = tostring(start_time_seconds)
- local end_time_str = tostring(end_time_seconds)
- table.insert(command, "--external-downloader")
- table.insert(command, "ffmpeg")
- table.insert(command, "--external-downloader-args")
- table.insert(command, "-loglevel warning -nostats -hide_banner -ss ".. start_time_str .. " -to " .. end_time_str .. " -avoid_negative_ts make_zero")
- end
- else --DOWNLOAD.VIDEO or DOWNLOAD.VIDEO_EMBED_SUBTITLE
- if download_type == DOWNLOAD.VIDEO_EMBED_SUBTITLE then
- table.insert(command, "--all-subs")
- table.insert(command, "--write-sub")
- table.insert(command, "--embed-subs")
- if not_empty(opts.sub_format) then
- table.insert(command, "--sub-format")
- table.insert(command, opts.sub_format)
- end
- end
- if not_empty(opts.video_format) then
- table.insert(command, "--format")
- if opts.video_format == "current" then
- table.insert(command, get_current_format())
- else
- table.insert(command, opts.video_format)
- end
- end
- if not_empty(opts.recode_video) then
- table.insert(command, "--recode-video")
- table.insert(command, opts.recode_video)
- end
- end
- if not_empty(opts.cookies) then
- table.insert(command, "--cookies")
- table.insert(command, opts.cookies)
- end
- table.insert(command, url)
-
- elseif select_range_mode > 0 and
- (download_type == DOWNLOAD.VIDEO or download_type == DOWNLOAD.VIDEO_EMBED_SUBTITLE) then
-
- -- Show download indicator
- mp.set_osd_ass(0, 0, "{\\an9}{\\fs12}⌛🔗")
-
- start_time_seconds = math.floor(start_time_seconds)
- end_time_seconds = math.ceil(end_time_seconds)
-
- local start_time_str = tostring(start_time_seconds)
- local end_time_str = tostring(end_time_seconds)
-
- -- Add time to the file name of the video
- local filename_format
- -- Insert start time/end time
- if not_empty(opts.filename) then
- if opts.filename:find("%%%(start_time%)") ~= nil then
- -- Found "start_time" -> replace it
- filename_format = tostring(opts.filename:
- gsub("%%%(start_time%)[^diouxXeEfFgGcrs]*[diouxXeEfFgGcrs]", start_time_str):
- gsub("%%%(end_time%)[^diouxXeEfFgGcrs]*[diouxXeEfFgGcrs]", end_time_str))
- else
- local ext_pattern = "%(ext)s"
- if opts.filename:sub(-#ext_pattern) == ext_pattern then
- -- Insert before ext
- filename_format = opts.filename:sub(1, #(opts.filename) - #ext_pattern) ..
- start_time_str .. "-" ..
- end_time_str .. ".%(ext)s"
- else
- -- append at end
- filename_format = opts.filename .. start_time_str .. "-" .. end_time_str
- end
- end
- else
- -- default youtube-dl filename pattern
- filename_format = "%(title)s-%(id)s." .. start_time_str .. "-" .. end_time_str .. ".%(ext)s"
- end
-
- -- Find a suitable format
- local format = "bestvideo[ext*=mp4]+bestaudio/best[ext*=mp4]/best"
- local requested_format = opts.video_format
- if requested_format == "current" then
- requested_format = get_current_format()
- end
- if requested_format == nil or requested_format == "" then
- format = format
- elseif requested_format == "best" then
- -- "best" works, because its a single file stream
- format = "best"
- elseif requested_format:find("mp4") ~= nil then
- -- probably a mp4 format, so use it
- format = requested_format
- else
- -- custom format, no "mp4" found -> use default
- msg.warn("Select range mode requires a .mp4 format or \"best\", found " ..
- requested_format .. "\n(" .. opts.video_format .. ")" ..
- "\nUsing default format instead: " .. format)
- end
-
- -- Get the download url of the video file
- -- e.g.: youtube-dl -g -f bestvideo[ext*=mp4]+bestaudio/best[ext*=mp4]/best -s --get-filename https://www.youtube.com/watch?v=abcdefg
- command = {"youtube-dl"}
- if opts.restrict_filenames then
- table.insert(command, "--restrict-filenames")
- end
- if not_empty(opts.cookies) then
- table.insert(command, "--cookies")
- table.insert(command, opts.cookies)
- end
- table.insert(command, "-g")
- table.insert(command, "--no-playlist")
- table.insert(command, "-f")
- table.insert(command, format)
- table.insert(command, "-o")
- table.insert(command, filename_format)
- table.insert(command, "-s")
- table.insert(command, "--get-filename")
- table.insert(command, url)
-
- msg.debug("info exec: " .. table.concat(command, " "))
- local info_status, info_stdout, info_stderr = exec(command, true, true)
- if info_status ~= 0 then
- mp.set_osd_ass(0, 0, "")
- mp.osd_message("Could not retieve download stream url: status=" .. tostring(info_status) .. "\n" ..
- ass0 .. "{\\fs8} " .. info_stdout:gsub("\r", "") .."\n" .. info_stderr:gsub("\r", "") .. ass1, 20)
- msg.debug("info_stdout:\n" .. info_stdout)
- msg.debug("info_stderr:\n" .. info_stderr)
- mp.set_osd_ass(0, 0, "")
- is_downloading = false
- return
- end
-
- -- Split result into lines
- local info_lines = {}
- local last_index = 0
- local info_lines_N = 0
- while true do
- local start_i, end_i = info_stdout:find("\n", last_index, true)
- if start_i then
- local line = tostring(trim(info_stdout:sub(last_index, start_i)))
- if line ~= "" then
- table.insert(info_lines, line)
- info_lines_N = info_lines_N + 1
- end
- else
- break
- end
- last_index = end_i + 1
- end
-
- if info_lines_N < 2 then
- mp.set_osd_ass(0, 0, "")
- mp.osd_message("Could not extract download stream urls and filename from output\n" ..
- ass0 .. "{\\fs8} " .. info_stdout:gsub("\r", "") .."\n" .. info_stderr:gsub("\r", "") .. ass1, 20)
- msg.debug("info_stdout:\n" .. info_stdout)
- msg.debug("info_stderr:\n" .. info_stderr)
- mp.set_osd_ass(0, 0, "")
- is_downloading = false
- return
- end
- range_mode_file_name = info_lines[info_lines_N]
- table.remove(info_lines)
-
- if download_type == DOWNLOAD.VIDEO_EMBED_SUBTITLE then
- -- youtube-dl --write-sub --skip-download https://www.youtube.com/watch?v=abcdefg -o "temp.%(ext)s"
- command = {"youtube-dl", "--write-sub", "--skip-download", "--sub-lang", opts.sub_lang}
- if not_empty(opts.sub_format) then
- table.insert(command, "--sub-format")
- table.insert(command, opts.sub_format)
- end
- local randomName = "tmp_" .. tostring(math.random())
- table.insert(command, "-o")
- table.insert(command, randomName .. ".%(ext)s")
- table.insert(command, url)
-
- -- Start subtitle download
- msg.debug("exec: " .. table.concat(command, " "))
- local subtitle_status, subtitle_stdout, subtitle_stderr = exec(command, true, true)
- if subtitle_status == 0 and subtitle_stdout:find(randomName) then
- local i, j = subtitle_stdout:find(randomName .. "[^\n]+")
- range_mode_subtitle_file_name = trim(subtitle_stdout:sub(i, j))
- if range_mode_subtitle_file_name ~= "" then
- if range_mode_file_name:sub(-4) ~= ".mkv" then
- -- Only mkv supports all kinds of subtitle formats
- range_mode_file_name = range_mode_file_name:sub(1,-4) .. "mkv"
- end
- end
- else
- mp.osd_message("Could not find a suitable subtitle")
- msg.debug("subtitle_stdout:\n" .. subtitle_stdout)
- msg.debug("subtitle_stderr:\n" .. subtitle_stderr)
- end
-
- end
-
- -- Download earlier (cut off afterwards)
- start_time_offset = math.min(15, start_time_seconds)
- start_time_seconds = start_time_seconds - start_time_offset
-
- start_time_str = tostring(start_time_seconds)
- end_time_str = tostring(end_time_seconds)
-
- command = {"ffmpeg", "-loglevel", "warning", "-nostats", "-hide_banner", "-y"}
- for _, value in ipairs(info_lines) do
- table.insert(command, "-ss")
- table.insert(command, start_time_str)
- table.insert(command, "-to")
- table.insert(command, end_time_str)
- table.insert(command, "-i")
- table.insert(command, value)
- end
- if not_empty(range_mode_subtitle_file_name) then
- table.insert(command, "-ss")
- table.insert(command, start_time_str)
- table.insert(command, "-i")
- table.insert(command, range_mode_subtitle_file_name)
- table.insert(command, "-to") -- To must be after input for subtitle
- table.insert(command, end_time_str)
- end
- table.insert(command, "-c")
- table.insert(command, "copy")
- table.insert(command, range_mode_file_name)
-
- disable_select_range()
- end
-
- -- Show download indicator
- mp.set_osd_ass(0, 0, "{\\an9}{\\fs12}⌛💾")
-
- -- Start download
- msg.debug("exec: " .. table.concat(command, " "))
- local status, stdout, stderr = exec(command, true, true)
-
- if status == 0 and range_mode_file_name ~= nil then
- mp.set_osd_ass(0, 0, "{\\an9}{\\fs12}⌛🔨")
-
- -- Cut first few seconds to fix errors
- local start_time_offset_str = tostring(start_time_offset)
- if #start_time_offset_str == 1 then
- start_time_offset_str = "0" .. start_time_offset_str
- end
- local max_length = end_time_seconds - start_time_seconds + start_time_offset + 12
- local tmp_file_name = range_mode_file_name .. ".tmp." .. range_mode_file_name:sub(-3)
- command = {"ffmpeg", "-loglevel", "warning", "-nostats", "-hide_banner", "-y",
- "-i", range_mode_file_name, "-ss", "00:00:" .. start_time_offset_str,
- "-c", "copy", "-avoid_negative_ts", "make_zero", "-t", tostring(max_length), tmp_file_name}
- msg.debug("mux exec: " .. table.concat(command, " "))
- local muxstatus, muxstdout, muxstderr = exec(command, true, true)
- if muxstatus ~= 0 and not_empty(muxstderr) then
- msg.warn("Remux log:" .. tostring(muxstdout))
- msg.warn("Remux errorlog:" .. tostring(muxstderr))
- end
- if muxstatus == 0 then
- os.remove(range_mode_file_name)
- os.rename(tmp_file_name, range_mode_file_name)
- if not_empty(range_mode_subtitle_file_name) then
- os.remove(range_mode_subtitle_file_name)
- end
- end
-
- end
-
-
- is_downloading = false
-
- -- Hide download indicator
- mp.set_osd_ass(0, 0, "")
-
- local wrote_error_log = false
- if stderr ~= nil and not_empty(opts.log_file) and not_empty(stderr) then
- -- Write stderr to log file
- local title = mp.get_property("media-title")
- local file = io.open (opts.log_file , "a+")
- file:write("\n[")
- file:write(start_time)
- file:write("] ")
- file:write(url)
- file:write("\n[\"")
- file:write(title)
- file:write("\"]\n")
- file:write(stderr)
- file:close()
- wrote_error_log = true
- end
-
- if (status ~= 0) then
- mp.osd_message("download failed:\n" .. tostring(stderr), 10)
- msg.error("URL: " .. tostring(url))
- msg.error("Return status code: " .. tostring(status))
- msg.debug(tostring(stderr))
- msg.debug(tostring(stdout))
- return
- end
-
- if string.find(stdout, "has already been recorded in archive") ~=nil then
- mp.osd_message("Has already been recorded in archive", 5)
- return
- end
-
- -- Retrieve the file name
- local filename = nil
- if range_mode_file_name == nil and stdout then
- local i, j, last_i, start_index = 0
- while i ~= nil do
- last_i, start_index = i, j
- i, j = stdout:find ("Destination: ",j, true)
- end
-
- if last_i ~= nil then
- local end_index = stdout:find ("\n", start_index, true)
- if end_index ~= nil and start_index ~= nil then
- filename = trim(stdout:sub(start_index, end_index))
- end
- end
- elseif not_empty(range_mode_file_name) then
- filename = range_mode_file_name
- end
-
- local osd_text = "Download succeeded\n"
- local osd_time = 5
- -- Find filename or directory
- if filename then
- local filepath
- local basepath
- if filename:find("/") == nil and filename:find("\\") == nil then
- basepath = utils.getcwd()
- filepath = path_join(utils.getcwd(), filename)
- else
- basepath = ""
- filepath = filename
- end
-
- if filepath:len() < 100 then
- osd_text = osd_text .. ass0 .. "{\\fs12} " .. filepath .. " {\\fs20}" .. ass1
- elseif basepath == "" then
- osd_text = osd_text .. ass0 .. "{\\fs8} " .. filepath .. " {\\fs20}" .. ass1
- else
- osd_text = osd_text .. ass0 .. "{\\fs11} " .. basepath .. "\n" .. filename .. " {\\fs20}" .. ass1
- end
- if wrote_error_log then
- -- Write filename and end time to log file
- local file = io.open (opts.log_file , "a+")
- file:write("[" .. filepath .. "]\n")
- file:write(os.date("[end %c]\n"))
- file:close()
- end
- else
- if wrote_error_log then
- -- Write directory and end time to log file
- local file = io.open (opts.log_file , "a+")
- file:write("[" .. utils.getcwd() .. "]\n")
- file:write(os.date("[end %c]\n"))
- file:close()
- end
- osd_text = osd_text .. utils.getcwd()
- end
-
- -- Show warnings
- if not_empty(stderr) then
- msg.warn("Errorlog:" .. tostring(stderr))
- if stderr:find("incompatible for merge") == nil then
- local i = stderr:find("Input #")
- if i ~= nil then
- stderr = stderr:sub(i)
- end
- osd_text = osd_text .. "\n" .. ass0 .. "{\\fs8} " .. stderr:gsub("\r", "") .. ass1
- osd_time = osd_time + 5
- end
- end
-
- mp.osd_message(osd_text, osd_time)
-end
-
-local function select_range_show()
- local status
- if select_range_mode > 0 then
- if select_range_mode == 2 then
- status = "Download range: Fine tune\n← → start time\n↓ ↑ end time\n" ..
- tostring(opts.select_range_binding) .. " next mode"
- elseif select_range_mode == 1 then
- status = "Download range: Select interval\n← start here\n→ end here\n↓from beginning\n↑til end\n" ..
- tostring(opts.select_range_binding) .. " next mode"
- end
- mp.osd_message("Start: " .. start_time_formated .. "\nEnd: " .. end_time_formated .. "\n" .. status, 30)
- else
- status = "Download range: Disabled (download full length)"
- mp.osd_message(status, 3)
- end
-end
-
-local function select_range_set_left()
- if select_range_mode == 2 then
- start_time_seconds = math.max(0, start_time_seconds - 1)
- if start_time_seconds < 86400 then
- start_time_formated = os.date("!%H:%M:%S", start_time_seconds)
- else
- start_time_formated = tostring(start_time_seconds) .. "s"
- end
- elseif select_range_mode == 1 then
- start_time_seconds = mp.get_property_number("time-pos")
- start_time_formated = mp.command_native({"expand-text","${time-pos}"})
- end
- select_range_show()
-end
-
-local function select_range_set_start()
- if select_range_mode == 2 then
- end_time_seconds = math.max(1, end_time_seconds - 1)
- if end_time_seconds < 86400 then
- end_time_formated = os.date("!%H:%M:%S", end_time_seconds)
- else
- end_time_formated = tostring(end_time_seconds) .. "s"
- end
- elseif select_range_mode == 1 then
- start_time_seconds = 0
- start_time_formated = "00:00:00"
- end
- select_range_show()
-end
-
-local function select_range_set_end()
- if select_range_mode == 2 then
- end_time_seconds = math.min(mp.get_property_number("duration"), end_time_seconds + 1)
- if end_time_seconds < 86400 then
- end_time_formated = os.date("!%H:%M:%S", end_time_seconds)
- else
- end_time_formated = tostring(end_time_seconds) .. "s"
- end
- elseif select_range_mode == 1 then
- end_time_seconds = mp.get_property_number("duration")
- end_time_formated = mp.command_native({"expand-text","${duration}"})
- end
- select_range_show()
-end
-
-local function select_range_set_right()
- if select_range_mode == 2 then
- start_time_seconds = math.min(mp.get_property_number("duration") - 1, start_time_seconds + 1)
- if start_time_seconds < 86400 then
- start_time_formated = os.date("!%H:%M:%S", start_time_seconds)
- else
- start_time_formated = tostring(start_time_seconds) .. "s"
- end
- elseif select_range_mode == 1 then
- end_time_seconds = mp.get_property_number("time-pos")
- end_time_formated = mp.command_native({"expand-text","${time-pos}"})
- end
- select_range_show()
-end
-
-
-local function select_range()
- -- Cycle through modes
- if select_range_mode == 2 then
- -- Disable range mode
- disable_select_range()
- elseif select_range_mode == 1 then
- -- Switch to "fine tune" mode
- select_range_mode = 2
- else
- select_range_mode = 1
- -- Add keybinds for arrow keys
- mp.add_key_binding("up", "select-range-set-up", select_range_set_end)
- mp.add_key_binding("down", "select-range-set-down", select_range_set_start)
- mp.add_key_binding("left", "select-range-set-left", select_range_set_left)
- mp.add_key_binding("right", "select-range-set-right", select_range_set_right)
-
- -- Defaults
- if start_time_seconds == nil then
- start_time_seconds = mp.get_property_number("time-pos")
- start_time_formated = mp.command_native({"expand-text","${time-pos}"})
- end_time_seconds = mp.get_property_number("duration")
- end_time_formated = mp.command_native({"expand-text","${duration}"})
- end
- end
- select_range_show()
-end
-
-local function download_video()
- return download(DOWNLOAD.VIDEO)
-end
-
-local function download_audio()
- return download(DOWNLOAD.AUDIO)
-end
-
-local function download_subtitle()
- return download(DOWNLOAD.SUBTITLE)
-end
-
-local function download_embed_subtitle()
- return download(DOWNLOAD.VIDEO_EMBED_SUBTITLE)
-end
-
--- keybind
-if not_empty(opts.download_video_binding) then
- mp.add_key_binding(opts.download_video_binding, "download-video", download_video)
-end
-if not_empty(opts.download_audio_binding) then
- mp.add_key_binding(opts.download_audio_binding, "download-audio", download_audio)
-end
-if not_empty(opts.download_subtitle_binding) then
- mp.add_key_binding(opts.download_subtitle_binding, "download-subtitle", download_subtitle)
-end
-if not_empty(opts.download_video_embed_subtitle_binding) then
- mp.add_key_binding(opts.download_video_embed_subtitle_binding, "download-embed-subtitle", download_embed_subtitle)
-end
-if not_empty(opts.select_range_binding) then
- mp.add_key_binding(opts.select_range_binding, "select-range-start", select_range)
-end