summaryrefslogtreecommitdiffstats
path: root/player/lua/ytdl_hook.lua
blob: 3161da61944d363de3465545b661da63b9f2f243 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
local utils = require 'mp.utils'
local msg = require 'mp.msg'
local options = require 'mp.options'

local o = {
    exclude = "",
    try_ytdl_first = false,
    use_manifests = false,
    all_formats = false,
    force_all_formats = true,
    thumbnails = "none",
    ytdl_path = "",
}

local ytdl = {
    path = "",
    paths_to_search = {"yt-dlp", "yt-dlp_x86", "youtube-dl"},
    searched = false,
    blacklisted = {}
}

options.read_options(o, nil, function()
    ytdl.blacklisted = {} -- reparse o.exclude next time
    ytdl.searched = false
end)

local chapter_list = {}
local playlist_cookies = {}

function Set (t)
    local set = {}
    for _, v in pairs(t) do set[v] = true end
    return set
end

-- ?: surrogate (keep in mind that there is no lazy evaluation)
function iif(cond, if_true, if_false)
    if cond then
        return if_true
    end
    return if_false
end

-- youtube-dl JSON name to mpv tag name
local tag_list = {
    ["uploader"]        = "uploader",
    ["channel_url"]     = "channel_url",
    -- these titles tend to be a bit too long, so hide them on the terminal
    -- (default --display-tags does not include this name)
    ["description"]     = "ytdl_description",
    -- "title" is handled by force-media-title
    -- tags don't work with all_formats=yes
}

local safe_protos = Set {
    "http", "https", "ftp", "ftps",
    "rtmp", "rtmps", "rtmpe", "rtmpt", "rtmpts", "rtmpte",
    "data"
}

-- For some sites, youtube-dl returns the audio codec (?) only in the "ext" field.
local ext_map = {
    ["mp3"]         = "mp3",
    ["opus"]        = "opus",
}

local codec_map = {
    -- src pattern  = mpv codec
    ["vtt"]         = "webvtt",
    ["opus"]        = "opus",
    ["vp9"]         = "vp9",
    ["avc1%..*"]    = "h264",
    ["av01%..*"]    = "av1",
    ["mp4a%..*"]    = "aac",
}

-- Codec name as reported by youtube-dl mapped to mpv internal codec names.
-- Fun fact: mpv will not really use the codec, but will still try to initialize
-- the codec on track selection (just to scrap it), meaning it's only a hint,
-- but one that may make initialization fail. On the other hand, if the codec
-- is valid but completely different from the actual media, nothing bad happens.
local function map_codec_to_mpv(codec)
    if codec == nil then
        return nil
    end
    for k, v in pairs(codec_map) do
        local s, e = codec:find(k)
        if s == 1 and e == #codec then
            return v
        end
    end
    return nil
end

local function platform_is_windows()
    return mp.get_property_native("platform") == "windows"
end

local function exec(args)
    msg.debug("Running: " .. table.concat(args, " "))

    return mp.command_native({
        name = "subprocess",
        args = args,
        capture_stdout = true,
        capture_stderr = true,
    })
end

-- return true if it was explicitly set on the command line
local function option_was_set(name)
    return mp.get_property_bool("option-info/" ..name.. "/set-from-commandline",
                                false)
end

-- return true if the option was set locally
local function option_was_set_locally(name)
    return mp.get_property_bool("option-info/" ..name.. "/set-locally", false)
end

-- youtube-dl may set special http headers for some sites (user-agent, cookies)
local function set_http_headers(http_headers)
    if not http_headers then
        return
    end
    local headers = {}
    local useragent = http_headers["User-Agent"]
    if useragent and not option_was_set("user-agent") then
        mp.set_property("file-local-options/user-agent", useragent)
    end
    local additional_fields = {"Cookie", "Referer", "X-Forwarded-For"}
    for idx, item in pairs(additional_fields) do
        local field_value = http_headers[item]
        if field_value then
            headers[#headers + 1] = item .. ": " .. field_value
        end
    end
    if #headers > 0 and not option_was_set("http-header-fields") then
        mp.set_property_native("file-local-options/http-header-fields", headers)
    end
end

local special_cookie_field_names = Set {
    "expires", "max-age", "domain", "path"
}

-- parse single-line Set-Cookie syntax
local function parse_cookies(cookies_line)
    if not cookies_line then
        return {}
    end
    local cookies = {}
    local cookie = {}
    for stem in cookies_line:gmatch('[^;]+') do
        stem = stem:gsub("^%s*(.-)%s*$", "%1")
        local name, value = stem:match('^(.-)=(.+)$')
        if name and name ~= "" and value then
            local cmp_name = name:lower()
            if special_cookie_field_names[cmp_name] then
                cookie[cmp_name] = value
            else
                if cookie.name and cookie.value then
                    table.insert(cookies, cookie)
                end
                cookie = {
                    name = name,
                    value = value,
                }
            end
        end
    end
    if cookie.name and cookie.value then
        local cookie_key = cookie.domain .. ":" .. cookie.name
        cookies[cookie_key] = cookie
    end
    return cookies
end

-- serialize cookies for avformat
local function serialize_cookies_for_avformat(cookies)
    local result = ''
    for _, cookie in pairs(cookies) do
        local cookie_str = ('%s=%s; '):format(cookie.name, cookie.value)
        for k, v in pairs(cookie) do
            if k ~= "name" and k ~= "value" then
                cookie_str = cookie_str .. ('%s=%s; '):format(k, v)
            end
        end
        result = result .. cookie_str .. '\r\n'
    end
    return result
end

-- set file-local cookies, preserving existing ones
local function set_cookies(cookies)
    if not cookies or cookies == "" then
        return
    end

    local option_key = "file-local-options/stream-lavf-o"
    local stream_opts = mp.get_property_native(option_key, {})
    local existing_cookies = parse_cookies(stream_opts["cookies"])

    local new_cookies = parse_cookies(cookies)
    for cookie_key, cookie in pairs(new_cookies) do
        if not existing_cookies[cookie_key] then
            existing_cookies[cookie_key] = cookie
        end
    end

    stream_opts["cookies"] = serialize_cookies_for_avformat(existing_cookies)
    mp.set_property_native(option_key, stream_opts)
end

local function append_libav_opt(props, name, value)
    if not props then
        props = {}
    end

    if name and value and not props[name] then
        props[name] = value
    end

    return props
end

local function edl_escape(url)
    return "%" .. string.len(url) .. "%" .. url
end

local function url_is_safe(url)
    local proto = type(url) == "string" and url:match("^(%a[%w+.-]*):") or nil
    local safe = proto and safe_protos[proto]
    if not safe then
        msg.error(("Ignoring potentially unsafe url: '%s'"):format(url))
    end
    return safe
end

local function time_to_secs(time_string)
    local ret

    local a, b, c = time_string:match("(%d+):(%d%d?):(%d%d)")
    if a ~= nil then
        ret = (a*3600 + b*60 + c)
    else
        a, b = time_string:match("(%d%d?):(%d%d)")
        if a ~= nil then
            ret = (a*60 + b)
        end
    end

    return ret
end

local function extract_chapters(data, video_length)
    local ret = {}

    for line in data:gmatch("[^\r\n]+") do
        local time = time_to_secs(line)
        if time and (time < video_length) then
            table.insert(ret, {time = time, title = line})
        end
    end
    table.sort(ret, function(a, b) return a.time < b.time end)
    return ret
end

local function is_blacklisted(url)
    if o.exclude == "" then return false end
    if #ytdl.blacklisted == 0 then
        for match in o.exclude:gmatch('%|?([^|]+)') do
            ytdl.blacklisted[#ytdl.blacklisted + 1] = match
        end
    end
    if #ytdl.blacklisted > 0 then
        url = url:match('https?://(.+)')
        for _, exclude in ipairs(ytdl.blacklisted) do
            if url:match(exclude) then
                msg.verbose('URL matches excluded substring. Skipping.')
                return true
            end
        end
    end
    return false
end

local function parse_yt_playlist(url, json)
    -- return 0-based index to use with --playlist-start

    if not json.extractor or
       (json.extractor ~= "youtube:tab" and
        json.extractor ~= "youtube:playlist") then
        return nil
    end

    local query = url:match("%?.+")
    if not query then return nil end

    local args = {}
    for arg, param in query:gmatch("(%a+)=([^&?]+)") do
        if arg and param then
            args[arg] = param
        end
    end

    local maybe_idx = tonumber(args["index"])

    -- if index matches v param it's probably the requested item
    if maybe_idx and #json.entries >= maybe_idx and
        json.entries[maybe_idx].id == args["v"] then
        msg.debug("index matches requested video")
        return maybe_idx - 1
    end

    -- if there's no index or it doesn't match, look for video
    for i = 1, #json.entries do
        if json.entries[i].id == args["v"] then
            msg.debug("found requested video in index " .. (i - 1))
            return i - 1
        end
    end

    msg.debug("requested video not found in playlist")
    -- if item isn't on the playlist, give up
    return nil
end

local function make_absolute_url(base_url, url)
    if url:find("https?://") == 1 then return url end

    local proto, domain, rest =
        base_url:match("(https?://)([^/]+/)(.*)/?")
    local segs = {}
    rest:gsub("([^/]+)", function(c) table.insert(segs, c) end)
    url:gsub("([^/]+)", function(c) table.insert(segs, c) end)
    local resolved_url = {}
    for i, v in ipairs(segs) do
        if v == ".." then
            table.remove(resolved_url)
        elseif v ~= "." then
            table.insert(resolved_url, v)
        end
    end
    return proto .. domain ..
        table.concat(resolved_url, "/")
end

local function join_url(base_url, fragment)
    local res = ""
    if base_url and fragment.path then
        res = make_absolute_url(base_url, fragment.path)
    elseif fragment.url then
        res = fragment.url
    end
    return res
end

local function edl_track_joined(fragments, protocol, is_live, base)
    if type(fragments) ~= "table" or not fragments[1] then
        msg.debug("No fragments to join into EDL")
        return nil
    end

    local edl = "edl://"
    local offset = 1
    local parts = {}

    if protocol == "http_dash_segments" and not is_live then
        msg.debug("Using dash")
        local args = ""

        -- assume MP4 DASH initialization segment
        if not fragments[1].duration and #fragments > 1 then
            msg.debug("Using init segment")
            args = args .. ",init=" .. edl_escape(join_url(base, fragments[1]))
            offset = 2
        end

        table.insert(parts, "!mp4_dash" .. args)

        -- Check remaining fragments for duration;
        -- if not available in all, give up.
        for i = offset, #fragments do
            if not fragments[i].duration then
                msg.verbose("EDL doesn't support fragments " ..
                         "without duration with MP4 DASH")
                return nil
            end
        end
    end

    for i = offset, #fragments do
        local fragment = fragments[i]
        if not url_is_safe(join_url(base, fragment)) then
            return nil
        end
        table.insert(parts, edl_escape(join_url(base, fragment)))
        if fragment.duration then
            parts[#parts] =
                parts[#parts] .. ",length="..fragment.duration
        end
    end
    return edl .. table.concat(parts, ";") .. ";"
end

local function has_native_dash_demuxer()
    local demuxers = mp.get_property_native("demuxer-lavf-list", {})
    for _, v in ipairs(demuxers) do
        if v == "dash" then
            return true
        end
    end
    return false
end

local function valid_manifest(json)
    local reqfmt = json["requested_formats"] and json["requested_formats"][1] or {}
    if not reqfmt["manifest_url"] and not json["manifest_url"] then
        return false
    end
    local proto = reqfmt["protocol"] or json["protocol"] or ""
    return (proto == "http_dash_segments" and has_native_dash_demuxer()) or
        proto:find("^m3u8")
end

local function as_integer(v, def)
    def = def or 0
    local num = math.floor(tonumber(v) or def)
    if num > -math.huge and num < math.huge then
        return num
    end
    return def
end

local function tags_to_edl(json)
    local tags = {}
    for json_name, mp_name in pairs(tag_list) do
        local v = json[json_name]
        if v then
            tags[#tags + 1] = mp_name .. "=" .. edl_escape(tostring(v))
        end
    end
    if #tags == 0 then
        return nil
    end
    return "!global_tags," .. table.concat(tags, ",")
end

-- Convert a format list from youtube-dl to an EDL URL, or plain URL.
--  json: full json blob by youtube-dl
--  formats: format list by youtube-dl
--  use_all_formats: if=true, then formats is the full format list, and the
--                   function will attempt to return them as delay-loaded tracks
-- See res table initialization in the function for result type.
local function formats_to_edl(json, formats, use_all_formats)
    local res = {
        -- the media URL, which may be EDL
        url = nil,
        -- for use_all_formats=true: whether any muxed formats are present, and
        -- at the same time the separate EDL parts don't have both audio/video
        muxed_needed = false,
    }

    local default_formats = {}
    local requested_formats = json["requested_formats"] or json["requested_downloads"]
    if use_all_formats and requested_formats then
        for _, track in ipairs(requested_formats) do
            local id = track["format_id"]
            if id then
                default_formats[id] = true
            end
        end
    end

    local duration = as_integer(json["duration"])
    local single_url = nil
    local streams = {}

    local tbr_only = true
    for index, track in ipairs(formats) do
        tbr_only = tbr_only and track["tbr"] and
                   (not track["abr"]) and (not track["vbr"])
    end

    local has_requested_video = false
    local has_requested_audio = false
    -- Web players with quality selection always show the highest quality
    -- option at the top. Since tracks are usually listed with the first
    -- track at the top, that should also be the highest quality track.
    -- yt-dlp/youtube-dl sorts it's formats from worst to best.
    -- Iterate in reverse to get best track first.
    for index = #formats, 1, -1 do
        local track = formats[index]
        local edl_track = nil
        edl_track = edl_track_joined(track.fragments,
            track.protocol, json.is_live,
            track.fragment_base_url)
        if not edl_track and not url_is_safe(track.url) then
            msg.error("No safe URL or supported fragmented stream available")
            return nil
        end

        local is_default = default_formats[track["format_id"]]
        local tracks = {}
        -- "none" means it is not a video
        -- nil means it is unknown
        if (o.force_all_formats or track.vcodec) and track.vcodec ~= "none" then
            tracks[#tracks + 1] = {
                media_type = "video",
                codec = map_codec_to_mpv(track.vcodec),
            }
            if is_default then
                has_requested_video = true
            end
        end
        if (o.force_all_formats or track.acodec) and track.acodec ~= "none" then
            tracks[#tracks + 1] = {
                media_type = "audio",
                codec = map_codec_to_mpv(track.acodec) or
                        ext_map[track.ext],
            }
            if is_default then
                has_requested_audio = true
            end
        end

        local url = edl_track or track.url
        local hdr = {"!new_stream", "!no_clip", "!no_chapters"}
        local skip = #tracks == 0
        local params = ""

        if use_all_formats then
            for _, sub in ipairs(tracks) do
                -- A single track that is either audio or video. Delay load it.
                local props = ""
                if sub.media_type == "video" then
                    props = props .. ",w=" .. as_integer(track.width)
                                  .. ",h=" .. as_integer(track.height)
                                  .. ",fps=" .. as_integer(track.fps)
                elseif sub.media_type == "audio" then
                    props = props .. ",samplerate=" .. as_integer(track.asr)
                end
                hdr[#hdr + 1] = "!delay_open,media_type=" .. sub.media_type ..
                    ",codec=" .. (sub.codec or "null") .. props

                -- Add bitrate information etc. for better user selection.
                local byterate = 0
                local rates = {"tbr", "vbr", "abr"}
                if #tracks > 1 then
                    rates = {({video = "vbr", audio = "abr"})[sub.media_type]}
                end
                if tbr_only then
                    rates = {"tbr"}
                end
                for _, f in ipairs(rates) do
                    local br = as_integer(track[f])
                    if br > 0 then
                        byterate = math.floor(br * 1000 / 8)
                        break
                    end
                end
                local title = track.format or track.format_note or ""
                if #tracks > 1 then
                    if #title > 0 then
                        title = title .. " "
                    end
                    title = title .. "muxed-" .. index
                end
                local flags = {}
                if is_default then
                    flags[#flags + 1] = "default"
                end
                hdr[#hdr + 1] = "!track_meta,title=" ..
                    edl_escape(title) .. ",byterate=" .. byterate ..
                    iif(#flags > 0, ",flags=" .. table.concat(flags, "+"), "")
            end

            if duration > 0 then
                params = params .. ",length=" .. duration
            end
        end

        if not skip then
            hdr[#hdr + 1] = edl_escape(url) .. params

            streams[#streams + 1] = table.concat(hdr, ";")
            -- In case there is only 1 of these streams.
            -- Note: assumes it has no important EDL headers
            single_url = url
        end
    end

    local tags = tags_to_edl(json)

    -- Merge all tracks into a single virtual file, but avoid EDL if it's
    -- only a single track without metadata (i.e. redundant).
    if #streams == 1 and single_url and not tags then
        res.url = single_url
    elseif #streams > 0 then
        if tags then
            -- not a stream; just for the sake of concatenating the EDL string
            streams[#streams + 1] = tags
        end
        res.url = "edl://" .. table.concat(streams, ";")
    else
        return nil
    end

    if has_requested_audio ~= has_requested_video then
        local not_req_prop = has_requested_video and "aid" or "vid"
        if mp.get_property(not_req_prop) == "auto" then
            mp.set_property("file-local-options/" .. not_req_prop, "no")
        end
    end

    return res
end

local function add_single_video(json)
    local streamurl = ""
    local format_info = ""
    local max_bitrate = 0
    local requested_formats = json["requested_formats"] or json["requested_downloads"]
    local all_formats = json["formats"]
    local has_requested_formats = requested_formats and #requested_formats > 0
    local http_headers = has_requested_formats
                         and requested_formats[1].http_headers
                         or json.http_headers
    local cookies = has_requested_formats
                    and requested_formats[1].cookies
                    or json.cookies

    if o.use_manifests and valid_manifest(json) then
        -- prefer manifest_url if present
        format_info = "manifest"

        local mpd_url = requested_formats and