-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectformat.lua
1141 lines (978 loc) · 23.4 KB
/
selectformat.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Name: mpv-selectformat
-- Author: koonix <[email protected]>
-- Upstream: https://github.com/koonix/mpv-selectformat
-- Version: 1.0.3
-- License: MIT
local script_name = "selectformat"
-- ====================
-- = requires
-- ====================
mp.msg = require("mp.msg")
mp.utils = require("mp.utils")
mp.options = require("mp.options")
mp.assdraw = require("mp.assdraw")
-- ====================
-- = declarations
-- ====================
local function main() end
local function formats_fetch() end
local function formats_save(url, success, result, error) end
local function formats_fold(width, height, audio_only) end
local function menu_toggle() end
local function menu_show() end
local function menu_init_vars() end
local function menu_init_sel_pos() end
local function menu_hide() end
local function menu_draw() end
local function menu_get_prefix(pos) end
local function menu_get_indent_marker(pos) end
local function menu_keys_bind() end
local function menu_keys_unbind() end
local function menu_cursor_move(i) end
local function menu_unfold() end
local function menu_fold() end
local function get_unfolded_cursor_fmt_id() end
local function get_cursor_pos() end
local function get_selected_pos() end
local function get_parent_of_selected_pos() end
local function get_format_id_pos(id) end
local function menu_select() end
local function is_fetch_in_progress() end
local function no_formats_available() end
local function build_ytdl_format_str(fmt) end
local function build_format_label(fmt) end
local function get_menu_header() end
local function strfmt_label(...) end
local function format_sort_fn(a, b) end
local function get_param_precedence(param, value) end
local function is_format_useful(fmt) end
local function sanitize_format(fmt) end
local function get_ytdl_cmdline() end
local function get_ytdl_mpvconf_args() end
local function is_format_audio_only(fmt) end
local function is_loaded_file_audio_only() end
local function is_param_valid(p) end
local function is_param_empty(p) end
local function update_url() end
local function numshorten(n) end
local function sigcmp(a, operator, b) end
local function is_network_stream(path) end
local function reload_resume() end
local function reload(path, timepos) end
local function update_ytdl_path() end
local function find_executable_path(name) end
local function get_ytdl_hook_opt_paths() end
local function exec_async(args, fn) end
local function exec(args) end
local function is_os_windows() end
local function isempty(v) end
local function isnum(v) end
local function isstr(v) end
local function istable(v) end
-- ====================
-- = options
-- ====================
local opts = {
prioritize_proto = true,
prefix_header = " ", -- a non-breaking space followed by a space
prefix_norm = " ", -- a non-breaking space followed by a space
prefix_cursor = "● ",
prefix_norm_sel = "○ ",
prefix_indent = " ",
header_separator = "─",
menu_pos_x = 7,
menu_pos_y = 7,
ass_style = "{\\fnmonospace\\fs7}",
}
mp.options.read_options(opts, script_name)
-- ====================
-- = keys
-- ====================
local keys = {
{
{ "UP", "k" },
"up",
function()
menu_cursor_move(-1)
end,
{ repeatable = true },
},
{
{ "DOWN", "j" },
"down",
function()
menu_cursor_move(1)
end,
{ repeatable = true },
},
{
{ "PGUP", "ctrl+u" },
"pgup",
function()
menu_cursor_move(-5)
end,
{ repeatable = true },
},
{
{ "PGDWN", "ctrl+d" },
"pgdwn",
function()
menu_cursor_move(5)
end,
{ repeatable = true },
},
{
{ "HOME", "g" },
"top",
function()
menu_cursor_move("top")
end,
},
{
{ "END", "G" },
"bottom",
function()
menu_cursor_move("bottom")
end,
},
{
{ "RIGHT", "l" },
"unfold",
function()
menu_unfold()
end,
},
{
{ "LEFT", "h" },
"fold",
function()
menu_fold()
end,
},
{
{ "ESC", "q" },
"quit",
function()
menu_hide()
end,
},
{
{ "ENTER" },
"select",
function()
menu_select()
end,
},
}
-- ====================
-- = globals
-- ====================
local data = {}
local url = ""
local ytdl_path = ""
local ytdl_not_found = false
local is_menu_shown = false
-- ====================
-- = functions
-- ====================
function main()
mp.register_event("start-file", formats_fetch)
mp.register_event("end-file", menu_hide)
mp.add_key_binding(nil, "menu", menu_toggle)
end
-- fetch the formats using youtube-dl asyncronously and hand them to formats_save()
function formats_fetch()
if not update_url() then
return
end
if data[url] then
return
end
if not update_ytdl_path() then
return
end
data[url] = "fetching"
exec_async(get_ytdl_cmdline(), function(a, b, c)
formats_save(url, a, b, c)
end)
end
-- process the formats fetched by formats_fetch()
function formats_save(url, success, result, error)
data[url] = nil
if (not success) or result.status ~= 0 then
return
end
local json = mp.utils.parse_json(result.stdout)
if (not istable(json)) or (not istable(json.formats)) then
return
end
data[url] = { formats = {} }
data[url].initial_format_id = json.format_id
for _, fmt in ipairs(json.formats) do
if is_format_useful(fmt) then
fmt = sanitize_format(fmt)
---@diagnostic disable: inject-field
fmt.label = build_format_label(fmt)
fmt.ytdl_format = build_ytdl_format_str(fmt)
---@diagnostic enable: inject-field
table.insert(data[url].formats, fmt)
end
end
if no_formats_available() then
return
end
table.sort(data[url].formats, format_sort_fn)
data[url].formats_unfolded = data[url].formats
formats_fold()
end
function formats_fold(width, height, audio_only)
data[url].formats = {}
local inserted_res = {}
local unfold_res = (width or "null") .. "x" .. (height or "null")
for _, fmt in ipairs(data[url].formats_unfolded) do
local res = (fmt.width or "") .. "x" .. (fmt.height or "")
if res == "x" then
res = is_format_audio_only(fmt) and "audio-only" or fmt.format_id
end
fmt.is_unfolded = false
local fmt_audio_only = is_format_audio_only(fmt)
if
not inserted_res[res]
or res == unfold_res
or (audio_only and res == "audio-only")
then
inserted_res[res] = true
if res == unfold_res or (audio_only and res == "audio-only") then
fmt.is_unfolded = true
end
table.insert(data[url].formats, fmt)
end
end
end
-- show/hide the menu
function menu_toggle()
if not update_url() then
mp.osd_message("Formats are only fetched for internet videos.")
return
elseif not update_ytdl_path() then
mp.osd_message("Couldn't find a youtube-dl executable.")
return
end
if is_menu_shown then
menu_hide()
else
menu_show()
end
end
function menu_show()
if is_fetch_in_progress() then
mp.osd_message("Formats are being fetched...")
return
elseif no_formats_available() then
mp.osd_message("No formats available.")
return
end
is_menu_shown = true
menu_init_vars()
menu_draw()
menu_keys_bind()
end
function menu_init_vars()
if data[url].cursor_fmt_id == nil or data[url].selected_fmt_id == nil then
data[url].cursor_fmt_id = data[url].formats[1].format_id
data[url].selected_fmt_id = "UNSELECTED"
menu_init_sel_pos()
end
end
-- put the cursor on the initially loaded format.
-- see the comments of the get_ytdl_mpvconf_args() function for more info.
function menu_init_sel_pos()
local id = data[url].initial_format_id
if isempty(id) or not isstr(id) then
return
end
id = id:match("^(.*)%+") or id
for idx, fmt in ipairs(data[url].formats_unfolded) do
if fmt.format_id == id then
data[url].selected_fmt_id = fmt.format_id
end
end
end
function menu_hide()
if is_menu_shown then
is_menu_shown = false
mp.set_osd_ass(0, 0, "")
menu_keys_unbind()
end
end
function menu_draw()
local ass = mp.assdraw.ass_new()
local header = get_menu_header()
local header_separator = (opts.prefix_header .. header):gsub(
".",
opts.header_separator
)
ass:pos(opts.menu_pos_x, opts.menu_pos_y)
ass:append(opts.ass_style)
ass:append(
opts.prefix_header .. header .. "\\N" .. header_separator .. "\\N"
)
for idx, fmt in ipairs(data[url].formats) do
ass:append(menu_get_prefix(idx))
ass:append(menu_get_indent_marker(idx))
ass:append(fmt.label .. "\\N")
end
mp.set_osd_ass(0, 0, ass.text)
end
function menu_get_prefix(pos)
if pos == get_cursor_pos() then
return opts.prefix_cursor
elseif pos == get_selected_pos() then
return opts.prefix_norm_sel
elseif
not data[url].formats[pos].is_unfolded
and pos == get_parent_of_selected_pos()
then
return opts.prefix_norm_sel
else
return opts.prefix_norm
end
end
function menu_get_indent_marker(pos)
if data[url].formats[pos].is_unfolded then
return opts.prefix_indent
else
return ""
end
end
-- bind the menu movement/action keys
function menu_keys_bind()
for _, v in ipairs(keys) do
for i, key in ipairs(v[1]) do
mp.add_forced_key_binding(key, v[2] .. i, v[3], v[4])
end
end
end
-- unbind the menu movement/action keys
function menu_keys_unbind()
for _, v in ipairs(keys) do
for i in ipairs(v[1]) do
mp.remove_key_binding(v[2] .. i)
end
end
end
function menu_cursor_move(i)
if i == "top" then
data[url].cursor_fmt_id = data[url].formats[1].format_id
elseif i == "bottom" then
data[url].cursor_fmt_id =
data[url].formats[#data[url].formats].format_id
else
local pos = get_cursor_pos() + i
if pos < 1 then
pos = 1
elseif pos > #data[url].formats then
pos = #data[url].formats
end
data[url].cursor_fmt_id = data[url].formats[pos].format_id
end
menu_draw()
end
function menu_unfold()
local cursor_fmt = data[url].formats[get_cursor_pos()]
formats_fold(
cursor_fmt.width,
cursor_fmt.height,
is_format_audio_only(cursor_fmt)
)
menu_draw()
end
function menu_fold()
data[url].cursor_fmt_id = get_unfolded_cursor_fmt_id()
formats_fold()
menu_draw()
end
function get_unfolded_cursor_fmt_id()
local function getres(fmt)
if is_format_audio_only(fmt) then
return "audio-only"
else
return (fmt.width or "null") .. "x" .. (fmt.height or "null")
end
end
local cursor_fmt = data[url].formats[get_cursor_pos()]
if cursor_fmt.is_unfolded then
local cursor_res = ""
for i = #data[url].formats, 1, -1 do
local fmt = data[url].formats[i]
if cursor_fmt.format_id == fmt.format_id then
cursor_res = getres(fmt)
end
if cursor_res ~= "" and getres(fmt) ~= cursor_res then
return data[url].formats[i + 1].format_id
end
if i == 1 then
return fmt.format_id
end
end
end
return data[url].cursor_fmt_id
end
function get_cursor_pos()
for idx, fmt in ipairs(data[url].formats) do
if data[url].cursor_fmt_id == fmt.format_id then
return idx
end
end
return 0
end
function get_selected_pos()
for idx, fmt in ipairs(data[url].formats) do
if data[url].selected_fmt_id == fmt.format_id then
return idx
end
end
return 0
end
function get_parent_of_selected_pos()
local function getres(fmt)
if is_format_audio_only(fmt) then
return "audio-only"
else
return (fmt.width or "null") .. "x" .. (fmt.height or "null")
end
end
local sel_res = ""
for i = #data[url].formats_unfolded, 1, -1 do
local ufmt = data[url].formats_unfolded[i]
if data[url].selected_fmt_id == ufmt.format_id then
sel_res = getres(ufmt)
end
if sel_res ~= "" and getres(ufmt) ~= sel_res then
return get_format_id_pos(
data[url].formats_unfolded[i + 1].format_id
)
end
if i == 1 then
return 1
end
end
return 0
end
function get_format_id_pos(id)
for idx, fmt in ipairs(data[url].formats) do
if id == fmt.format_id then
return idx
end
end
return 0
end
function menu_select()
menu_hide()
data[url].selected_fmt_id = data[url].cursor_fmt_id
mp.set_property(
"ytdl-format",
data[url].formats[get_selected_pos()].ytdl_format
)
reload_resume()
end
function is_fetch_in_progress()
return data[url] == "fetching"
end
function no_formats_available()
return not istable(data[url])
or not istable(data[url].formats)
or #data[url].formats == 0
end
-- build the youtube-dl format option for the given format
function build_ytdl_format_str(fmt)
if is_format_audio_only(fmt) then
return string.format("%s/bestaudio", fmt.format_id)
else
local audiofmt = "bestaudio"
local maxpx =
math.max(tonumber(fmt.width) or 1, tonumber(fmt.height) or 1)
if maxpx < 1000 then
audiofmt = "bestaudio[abr<=70]"
end
return string.format(
"%s+%s/%s+bestaudio/%s/best",
fmt.format_id,
audiofmt,
fmt.format_id,
fmt.format_id
)
end
end
-- build the label that represents the format in the UI
function build_format_label(fmt)
local res, codec, br, formatstr
if is_format_audio_only(fmt) then
res = "audio-only"
codec = fmt.acodec
br = fmt.abr or fmt.tbr
else
res = (fmt.width or "?") .. "x" .. (fmt.height or "?")
codec = fmt.vcodec
br = fmt.vbr or fmt.tbr
end
if codec then
codec =
codec:gsub("av01", "av1"):gsub("avc1", "h264"):gsub("h265", "hevc")
end
return strfmt_label(
res,
fmt.fps and numshorten(fmt.fps) or "",
codec or "",
br and numshorten(br * 10 ^ 3) or "",
fmt.asr and numshorten(fmt.asr) or "",
fmt.protocol or ""
)
end
function get_menu_header()
return strfmt_label("Resolution", "FPS", "Codec", "BR", "ASR", "Proto")
end
function strfmt_label(...)
return string.format("%-10s %-3s %-5s %-4s %-4s %s", ...)
end
-- function for sorting the formats table
function format_sort_fn(a, b)
local params
if opts.prioritize_proto then
params = {
"fps",
"dynamic_range",
"vcodec",
"acodec",
"protocol",
"tbr",
"vbr",
"abr",
"asr",
}
else
params = {
"fps",
"dynamic_range",
"vcodec",
"acodec",
"tbr",
"vbr",
"abr",
"asr",
"protocol",
}
end
a.res = (a.width or 1) * (a.height or 1)
b.res = (b.width or 1) * (b.height or 1)
if a.res > b.res then
return true
elseif a.res < b.res then
return false
end
for _, v in ipairs({ 1, 2 }) do
for _, p in ipairs(params) do
local do_sigcmp
if v == 1 and isnum(a[p]) and isnum(b[p]) then
do_sigcmp = true
else
do_sigcmp = false
end
local x = isnum(a[p]) and a[p] or get_param_precedence(p, a[p])
local y = isnum(b[p]) and b[p] or get_param_precedence(p, b[p])
if do_sigcmp then
if sigcmp(x, ">", y) then
return true
elseif sigcmp(x, "<", y) then
return false
end
else
if x > y then
return true
elseif x < y then
return false
end
end
end
end
return a.format_id > b.format_id
end
-- rate the given parameter value based on it's precedence
function get_param_precedence(param, value)
-- orders of precedence.
-- each item in any of the categories is a list of lua patterns.
-- pattern lists are specified from low to high precedence.
local order = {
dynamic_range = {
{ "sdr" },
{ "^$" },
{ "hlg" },
{ "h?d?r?10$" },
{ "h?d?r?10%+" },
{ "h?d?r?12" },
{ "dv" },
},
vcodec = {
{ "theora" },
{ "mp4v", "h263" },
{ "vp0?8" },
{ "[hx]264", "avc" },
{ "[hx]265", "he?vc" },
{ "vp0?9$" },
{ "vp0?9%.2" },
{ "av0?1" },
},
acodec = {
{ "dts" },
{ "^ac%-?3" },
{ "e%-?a?c%-?3" },
{ "mp3" },
{ "mp?4a?" },
{ "avc" },
{ "vorbis", "ogg" },
{ "opus" },
},
protocol = {
{ "f4" },
{ "ws", "websocket$" },
{ "mms", "rtsp" },
{ "^$" },
{ "rtmpe?" },
{ "websocket_frag" },
{ ".*dash" },
{ "m3u8.*" },
{ "http$", "ftp$" },
{ "https", "ftps" },
},
}
if isempty(order[param]) then
return tonumber(value) or 0
elseif isempty(value) then
value = ""
end
local n = 1
for _, patternlist in ipairs(order[param]) do
for _, pattern in ipairs(patternlist) do
if value:lower():find(pattern) then
return n
end
end
n = n + 1
end
return 0
end
-- test whether the given format contains the bare minimum of information
function is_format_useful(fmt)
if (not istable(fmt)) or fmt.ext == "mhtml" or fmt.protocol == "mhtml" then
return false
end
local params = {
"format_id",
"vcodec",
"acodec",
"width",
"height",
"vbr",
"abr",
"tbr",
}
for _, p in ipairs(params) do
if is_param_valid(fmt[p]) then
return true
end
end
return false
end
-- convert the parameters of the given format to their own appropriate type
function sanitize_format(fmt)
local numeric_params = {
"width",
"height",
"fps",
"tbr",
"vbr",
"abr",
"asr",
}
local string_params = {
"format_id",
"dynamic_range",
"vcodec",
"acodec",
"protocol",
}
for _, p in ipairs(numeric_params) do
if is_param_empty(fmt[p]) then
fmt[p] = nil
elseif isstr(fmt[p]) then
fmt[p] = tonumber(fmt[p])
elseif not isnum(fmt[p]) then
fmt[p] = nil
end
end
for _, p in ipairs(string_params) do
if is_param_empty(fmt[p]) then
fmt[p] = nil
elseif isnum(fmt[p]) then
fmt[p] = tostring(fmt[p])
elseif not isstr(fmt[p]) then
fmt[p] = nil
end
end
fmt.vcodec = fmt.vcodec and fmt.vcodec:gsub("%..*", "") or nil
fmt.acodec = fmt.acodec and fmt.acodec:gsub("%..*", "") or nil
return fmt
end
-- build and return the command that needs to run in order to fetch the formats
function get_ytdl_cmdline()
local args = { ytdl_path, "--no-playlist", "-j" }
---@diagnostic disable: param-type-mismatch
for _, format_arg in ipairs(get_ytdl_mpvconf_args()) do
table.insert(args, format_arg)
end
---@diagnostic enable: param-type-mismatch
table.insert(args, "--")
table.insert(args, (url:gsub("^ytdl://", "")))
return args
end
-- get youtube-dl's options that are specified in mpv's
-- command line options or config file. if we call the youtube-dl command
-- with these options included, the initially loaded format will be apparent
-- in the "format_id" parameter of the infojson.
function get_ytdl_mpvconf_args()
local args = {}
local fmtopt = mp.get_property("ytdl-format")
local rawopts = mp.get_property_native("ytdl-raw-options")
if isempty(fmtopt) then
fmtopt = is_loaded_file_audio_only() and "bestaudio/best"
or "bestvideo+bestaudio/best"
end
if fmtopt ~= "ytdl" then
table.insert(args, "--format")
table.insert(args, fmtopt)
end
if istable(rawopts) then
for k, v in pairs(rawopts) do
table.insert(args, "--" .. k)
if not isempty(v) then
table.insert(args, v)
end
end
end
return args
end
-- test whether the given format contains only an audio stream
function is_format_audio_only(fmt)
return (is_param_valid(fmt.acodec) and (not is_param_valid(fmt.vcodec)))
or (
is_param_valid(fmt.audio_ext)
and (not is_param_valid(fmt.video_ext))
)
end
function is_loaded_file_audio_only()
return mp.get_property("video") == "no"
end
function is_param_valid(p)
return isnum(p) or (isstr(p) and (not is_param_empty(p)))
end
-- test whether the given format parameter is empty
function is_param_empty(p)
return isempty(p) or p == "none" or p == "null"
end
-- update the global url variable with the URL of the currently playing video
function update_url()
local path = mp.get_property("path")
if isstr(path) and is_network_stream(path) then
url = path
return true
else
return false
end
end
-- shorten and format the given number (eg. 4560 -> 4K)
function numshorten(n)
n = math.floor(n + 0.5) -- round the number
if n >= 10 ^ 9 then
return string.format("%dG", n / 10 ^ 9)
elseif n >= 10 ^ 6 then
return string.format("%dM", n / 10 ^ 6)
elseif n >= 10 ^ 3 then
return string.format("%dK", n / 10 ^ 3)
else
return string.format("%d", n)
end
end
-- compare the given numbers, but only succeed if the larger
-- number is significantly (15%) larger than the smaller one.
function sigcmp(a, operator, b)
local fraction = 0.15
if operator == ">" and a > b + (a * fraction) then
return true
elseif operator == "<" and a + (b * fraction) < b then
return true
else
return false
end
end
-- test whether the given path or URL is a network stream.
-- works by checking the given URL's protocol.
function is_network_stream(path)
local proto = path:match("^(%a+)://")
if not proto then
return false
end
for _, p in ipairs({
"http",
"https",
"ytdl",
"rtmp",
"rtmps",
"rtmpe",
"rtmpt",
"rtmpts",
"rtmpte",
"rtsp",
"rtsps",
"mms",
"mmst",
"mmsh",
"mmshttp",
"rtp",
"srt",
"srtp",
"gopher",
"gophers",
"data",
"ftp",
"ftps",
"sftp",
}) do
if proto == p then
return true
end
end
return false
end
-- this function is a modified version of mpv-reload's reload_resume()
-- https://github.com/4e6/mpv-reload, commit 1a6a938
function reload_resume()
local timepos = mp.get_property("time-pos")
local duration = mp.get_property_native("duration")
local plcount = mp.get_property_number("playlist-count")
local plpos = mp.get_property_number("playlist-pos")
local playlist = {}
for i = 0, plcount - 1 do