I am currently working on a small project which requies youtube-dl to work kivy environment. I have python 3.8.3, Kivy 1.11.1, youtube-dl 2020.5.8. Other than this issue kivy is working fine with python3.8.
I get this error AttributeError: 'str' object has no attribute 'write' while try to fetch information about a youtube video using youtube-dl
It does works with a few link and does not work with many others.
This error has already is already present on youtube-dl's github page [[here]][1], but I could not find any solution.
These are the two following cases with a reproducible example.
Case 1
import youtube_dl
from kivy.app import App
from kivy.uix.button import Button
class MusicApp(App):
def build(self):
return Button(text='Example')
ydl_opts = {
'outtmpl': '%(title)s.%(ext)s',
'audio-format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])
Output
[INFO ] [Logger ] Record log in /home/adarsh/.kivy/logs/kivy_20-06-23_1.txt
[INFO ] [Kivy ] v1.11.1
[INFO ] [Kivy ] Installed at "/usr/local/lib/python3.8/dist-packages/kivy/__init__.py"
[INFO ] [Python ] v3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0]
[INFO ] [Python ] Interpreter at "/usr/bin/python3"
[INFO ] [Factory ] 184 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2(['text_pango'] ignored)
[youtube] BaW_jenozKc: Downloading webpage
[download] Resuming download at byte 215799
[download] Destination: youtube-dl test video ''_ä↭.f137.mp4
[download] 100% of 2.11MiB in 00:07.90KiB/s ETA 00:00
[download] Destination: youtube-dl test video ''_ä↭.f140.m4a
[download] 100% of 154.06KiB in 00:00.07KiB/s ETA 00:00
[ffmpeg] Merging formats into "youtube-dl test video ''_ä↭.mp4"
Deleting original file youtube-dl test video ''_ä↭.f137.mp4 (pass -k to keep)
Deleting original file youtube-dl test video ''_ä↭.f140.m4a (pass -k to keep)
[ffmpeg] Destination: youtube-dl test video ''_ä↭.mp3
Deleting original file youtube-dl test video ''_ä↭.mp4 (pass -k to keep)
Case 2
import youtube_dl
from kivy.app import App
from kivy.uix.button import Button
class MusicApp(App):
def build(self):
return Button(text='Example')
ydl_opts = {
'outtmpl': '%(title)s.%(ext)s',
'audio-format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['https://www.youtube.com/watch?v=nTj4wVJAbNg'])
Output
[INFO ] [Logger ] Record log in /home/adarsh/.kivy/logs/kivy_20-06-23_2.txt
[INFO ] [Kivy ] v1.11.1
[INFO ] [Kivy ] Installed at "/usr/local/lib/python3.8/dist-packages/kivy/__init__.py"
[INFO ] [Python ] v3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0]
[INFO ] [Python ] Interpreter at "/usr/bin/python3"
[INFO ] [Factory ] 184 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2(['text_pango'] ignored)
[youtube] nTj4wVJAbNg: Downloading webpage
Traceback (most recent call last):
File "temp/tmp.py", line 21, in <module>
ydl.download(['https://www.youtube.com/watch?v=nTj4wVJAbNg'])
File "/home/adarsh/.local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 2018, in download
res = self.extract_info(
File "/home/adarsh/.local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 808, in extract_info
return self.process_ie_result(ie_result, download, extra_info)
File "/home/adarsh/.local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 863, in process_ie_result
return self.process_video_result(ie_result, download=download)
File "/home/adarsh/.local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 1644, in process_video_result
self.process_info(new_info)
File "/home/adarsh/.local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 1902, in process_info
self.report_warning(
File "/home/adarsh/.local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 613, in report_warning
self.to_stderr(warning_message)
File "/home/adarsh/.local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 527, in to_stderr
self._write_string(output, self._err_file)
File "/home/adarsh/.local/lib/python3.8/site-packages/youtube_dl/YoutubeDL.py", line 506, in _write_string
write_string(s, out=out, encoding=self.params.get('encoding'))
File "/home/adarsh/.local/lib/python3.8/site-packages/youtube_dl/utils.py", line 3174, in write_string
out.buffer.write(byt)
AttributeError: 'str' object has no attribute 'write'
Please suggest even if there are some workarounds.
EDIT : I passed a logger to youtube-dl taken from their github page
class MyLogger(object):
def debug(self, msg):
print("DEBUG " + msg)
pass
def warning(self, msg):
print("WARNING " + msg)
pass
def error(self, msg):
print("ERROR " + msg)
I needed to write my own logger at later point to get the progress of download. So I am going with this solution. [1]: https://github.com/ytdl-org/youtube-dl/issues/22109