[vlc-devel] [PATCH 4/4] ytdl: use redirect for single media

RĂ©mi Denis-Courmont remi at remlab.net
Mon Sep 21 18:02:23 CEST 2020


Rather than expose a playlist with a single item, redirect to the
media URL directly if the original URL points to a single item rather
than a playlist.
---
 modules/access/ytdl.c | 25 ++++++++++++++++++++-----
 share/ytdl-extract.py | 19 +++++++++----------
 2 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/modules/access/ytdl.c b/modules/access/ytdl.c
index 6b4fa8b93d..5e41d09ae9 100644
--- a/modules/access/ytdl.c
+++ b/modules/access/ytdl.c
@@ -41,7 +41,6 @@
 struct ytdl_sys {
     pid_t pid;
     int fd;
-    char first_byte;
 };
 
 static size_t readall(int fd, void *buf, size_t len)
@@ -114,12 +113,10 @@ static int Control(stream_t *s, int query, va_list args)
 
 static ssize_t ReadFirstByte(stream_t *s, void *buf, size_t len)
 {
-    struct ytdl_sys *sys = s->p_sys;
-
     if (unlikely(len == 0))
         return 0;
 
-    *(char *)buf = sys->first_byte;
+    *(char *)buf = '#';
     s->pf_read = Read;
     return 1;
 }
@@ -175,13 +172,31 @@ static int Open(vlc_object_t *obj)
 
     free(path);
 
-    if (readall(sys->fd, &sys->first_byte, 1) <= 0) {
+    char first_byte;
+
+    if (readall(sys->fd, &first_byte, 1) <= 0) {
         /* Location not handled */
         msg_Dbg(s, "cannot extract infos");
         Close(obj);
         return VLC_EGENERIC;
     }
 
+    if (first_byte != '#') {
+        /* Redirect if there is a single URL, so that we can refresh it every
+         * time it is opened.
+         */
+        char buf[65536];
+
+        buf[0] = first_byte;
+
+        val = readall(sys->fd, buf + 1, sizeof (buf) - 1);
+        s->psz_url = strndup(buf, val + 1);
+        Close(obj);
+        var_Create(obj, "ytdl", VLC_VAR_BOOL); /* Prevent recursion */
+        return VLC_ACCESS_REDIRECT;
+    }
+
+    /* Playlist */
     s->pf_read = ReadFirstByte;
     s->pf_control = Control;
     return VLC_SUCCESS;
diff --git a/share/ytdl-extract.py b/share/ytdl-extract.py
index 4e7ea09aa6..ae71a0c04e 100755
--- a/share/ytdl-extract.py
+++ b/share/ytdl-extract.py
@@ -59,14 +59,14 @@ def formats_choose_best(fmts):
 
     return best_format
 
-def entry_extract(entry):
+def entry_extract(entry, meta=True):
     # Process a given entry of a playlist
     if 'formats' in entry:
         fmt = formats_choose_best(entry['formats'])
     else:
         fmt = entry
 
-    if 'title' in entry:
+    if meta and 'title' in entry:
         print('#EXTINF:,,' + entry['title'].splitlines()[0])
 
     if 'ie_key' in fmt:
@@ -92,18 +92,18 @@ def url_extract(url):
     # Process a given URL
     infos = dl.extract_info(url, download=False)
 
-    print('#EXTM3U')
-
-    if 'title' in infos:
-        print('#PLAYLIST:' + infos['title'].splitlines()[0])
-
     if 'entries' in infos:
         # URL is a playlist: iterate over entries
+        print('#EXTM3U')
+
+        if 'title' in infos:
+            print('#PLAYLIST:' + infos['title'].splitlines()[0])
+
         for entry in infos['entries']:
             entry_extract(entry)
     else:
         # URL is a single media
-        entry_extract(infos)
+        entry_extract(infos, meta=False)
 
 def url_process(ie_url):
     opts = {
@@ -119,8 +119,7 @@ def url_process(ie_url):
         entry[p[0]] = p[1]
 
     entry = dl.process_ie_result(entry, download=False)
-    print('#EXTM3U')
-    entry_extract(entry)
+    entry_extract(entry, meta=False)
 
 url = sys.argv[1]
 
-- 
2.28.0



More information about the vlc-devel mailing list