aadnk commited on
Commit
11c4fb6
1 Parent(s): 89bd811

Handle OS error 36 - File name too long

Browse files

On many Linux systems, the maximum file name lenght
is 255 bytes. If the title contains UTF-8 characters, each
character may require more than 1 byte in the file name,
causing a title of 100 characters to actually exceed the 255
byte limit.

We handle this error and then use a much stricter file name format.

Files changed (1) hide show
  1. download.py +16 -1
download.py CHANGED
@@ -1,7 +1,9 @@
1
  import os
2
 
3
  from tempfile import mkdtemp
 
4
  from yt_dlp import YoutubeDL
 
5
  from yt_dlp.postprocessor import PostProcessor
6
 
7
  class FilenameCollectorPP(PostProcessor):
@@ -14,6 +16,15 @@ class FilenameCollectorPP(PostProcessor):
14
  return [], information
15
 
16
  def downloadUrl(url: str, maxDuration: int = None):
 
 
 
 
 
 
 
 
 
17
  destinationDirectory = mkdtemp()
18
 
19
  ydl_opts = {
@@ -23,6 +34,11 @@ def downloadUrl(url: str, maxDuration: int = None):
23
  'home': destinationDirectory
24
  }
25
  }
 
 
 
 
 
26
  filename_collector = FilenameCollectorPP()
27
 
28
  with YoutubeDL(ydl_opts) as ydl:
@@ -44,7 +60,6 @@ def downloadUrl(url: str, maxDuration: int = None):
44
 
45
  return result
46
 
47
-
48
  class ExceededMaximumDuration(Exception):
49
  def __init__(self, videoDuration, maxDuration, message):
50
  self.videoDuration = videoDuration
 
1
  import os
2
 
3
  from tempfile import mkdtemp
4
+ from tkinter import Y
5
  from yt_dlp import YoutubeDL
6
+ import yt_dlp
7
  from yt_dlp.postprocessor import PostProcessor
8
 
9
  class FilenameCollectorPP(PostProcessor):
 
16
  return [], information
17
 
18
  def downloadUrl(url: str, maxDuration: int = None):
19
+ try:
20
+ return _performDownload(url, maxDuration=maxDuration)
21
+ except yt_dlp.utils.DownloadError as e:
22
+ # In case of an OS error, try again with a different output template
23
+ if e.msg and e.msg.find("[Errno 36] File name too long") >= 0:
24
+ return _performDownload(url, maxDuration=maxDuration, outputTemplate="%(title).10s %(id)s.%(ext)s")
25
+ pass
26
+
27
+ def _performDownload(url: str, maxDuration: int = None, outputTemplate: str = None):
28
  destinationDirectory = mkdtemp()
29
 
30
  ydl_opts = {
 
34
  'home': destinationDirectory
35
  }
36
  }
37
+
38
+ # Add output template if specified
39
+ if outputTemplate:
40
+ ydl_opts['outtmpl'] = outputTemplate
41
+
42
  filename_collector = FilenameCollectorPP()
43
 
44
  with YoutubeDL(ydl_opts) as ydl:
 
60
 
61
  return result
62
 
 
63
  class ExceededMaximumDuration(Exception):
64
  def __init__(self, videoDuration, maxDuration, message):
65
  self.videoDuration = videoDuration