So i am making a ytmp3 project. I’ve finnished it and decide to make it callable from Run(Win + r). I’ve added the folder that has the main.py, ytmp3.bat to system variable. I called and it works, but it has some permission issue. It seems like it don’t have the permission to write and delete from the folder.
How i execute it from Run is:
ytmp3 https://youtu.be/Wgw6tJ8yz9M?si=2xhuVB-ED_2WMmyR
The log it gives:
[Console] Downloading Audio.
ERROR: unable to open for writing: [Errno 13] Permission denied: ‘downloaded_video.mp4.part’
Traceback (most recent call last):
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 603, in sanitize_open
raise LockingUnsupportedError
yt_dlp.utils.LockingUnsupportedError: File locking is not supported
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloaderhttp.py”, line 263, in download
ctx.stream, ctx.tmpfilename = self.sanitize_open(
^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 241, in wrapper
for retry in RetryManager(self.params.get(‘file_access_retries’, 3), error_callback, fd=self):
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 5251, in __iter__
self.error_callback(self.error, self.attempt, self.retries)
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 234, in error_callback
return RetryManager.report_retry(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 5259, in report_retry
raise e
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 243, in wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 254, in sanitize_open
f, filename = sanitize_open(filename, open_mode)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 606, in sanitize_open
stream = open(filename, open_mode)
^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: ‘downloaded_video.mp4.part’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “D:System Variablesytmp3main.py”, line 148, in <module>
main()
File “D:System Variablesytmp3main.py”, line 135, in main
downloadAudio(link)
File “D:System Variablesytmp3main.py”, line 45, in downloadAudio
info = ydl.extract_info(url, download=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1613, in extract_info
return self.__extract_info(url, self.get_info_extractor(key), download, extra_info, process)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1624, in wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1780, in __extract_info
return self.process_ie_result(ie_result, download, extra_info)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1839, in process_ie_result
ie_result = self.process_video_result(ie_result, download=download)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 3011, in process_video_result
self.process_info(new_info)
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 177, in wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 3479, in process_info
success, real_download = self.dl(temp_filename, info_dict)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 3199, in dl
return fd.download(name, new_info, subtitle)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 464, in download
ret = self.real_download(filename, info_dict)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloaderhttp.py”, line 368, in real_download
return download()
^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloaderhttp.py”, line 269, in download
self.report_error(f’unable to open for writing: {err}’)
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1090, in report_error
self.trouble(f'{self._format_err(“ERROR:”, self.Styles.ERROR)} {message}’, *args, **kwargs)
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1029, in trouble
raise DownloadError(message, exc_info)
yt_dlp.utils.DownloadError: ERROR: unable to open for writing: [Errno 13] Permission denied: ‘downloaded_video.mp4.part’
Press any key to continue . . .
The code i used:
import yt_dlp
import os
import subprocess
from PIL import Image
from mutagen.id3 import ID3, TIT2, TPE1, APIC
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
import sys
def getInfo(url):
with yt_dlp.YoutubeDL({“quiet”: True}) as ydl:
info = ydl.extract_info(url, download=False) # Extract metadata without downloading
title = info.get(“title”) # Get the video title
uploader = info.get(“uploader”) # Get the uploader’s channel name
return [title, uploader]
def downloadVideo(url):
options = {
‘format’: ‘best’,
‘outtmpl’: ‘downloaded_video.%(ext)s’,
‘quiet’: True,
‘noprogress’: True,
‘no_warnings’: True
}
with yt_dlp.YoutubeDL(options) as ydl:
ydl.download([url])
def downloadAudio(url):
print(“[Console] Downloading Audio.”)
video_filename = “downloaded_video.%(ext)s”
audio_filename = “extracted_audio.mpeg”
video_options = {
‘format’: ‘best’,
‘outtmpl’: video_filename,
‘quiet’: True,
‘noprogress’: True,
‘no_warnings’: True
}
with yt_dlp.YoutubeDL(video_options) as ydl:
info = ydl.extract_info(url, download=True)
downloaded_video_path = ydl.prepare_filename(info)
if os.path.exists(downloaded_video_path):
audio_command = [
“ffmpeg”,
“-i”, downloaded_video_path,
“-q:a”, “0”,
“-map”, “a”,
audio_filename
]
subprocess.run(audio_command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
if os.path.exists(audio_filename):
subprocess.run(
[“ffmpeg”, “-i”, audio_filename, “-q:a”, “0”, “-map”, “a”, “audio.mp3”],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True
)
os.remove(downloaded_video_path)
os.remove(audio_filename)
def downloadThumbnail(url, output_thumbnail=”thumbnail.jpg”, output_padded=”thumbnail_padded.jpg”):
print(“[Console] Downloading thumbnail.”)
with yt_dlp.YoutubeDL({“quiet”: True}) as ydl:
info = ydl.extract_info(url, download=False)
thumbnail_url = info.get(“thumbnail”)
if thumbnail_url:
subprocess.run(
[“curl”, “-o”, output_thumbnail, thumbnail_url],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True
)
subprocess.run(
[“ffmpeg”, “-i”, output_thumbnail, “-q:v”, “1”, “thumbnail_converted.jpg”],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True
)
os.remove(output_thumbnail)
input_image = ‘thumbnail_converted.jpg’
output_image = ‘thumbnail.jpg’
with Image.open(input_image) as img:
width, height = img.size
new_size = max(width, height)
padding_x = (new_size – width) // 2
padding_y = (new_size – height) // 2
subprocess.run(
[‘ffmpeg’, ‘-i’, input_image, ‘-vf’, f’pad={new_size}:{new_size}:{padding_x}:{padding_y}:black’, ‘-q:v’, ‘1’, output_image],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True
)
os.remove(input_image)
def mergeAudio(Audio, thumbnail, songInfo):
print(“[Console] Merging song.”)
audio = EasyID3(Audio)
audio[‘title’] = songInfo[0]
audio[‘artist’] = songInfo[1]
audio.save()
audio = MP3(Audio, ID3=ID3)
with open(thumbnail, “rb”) as album_art:
audio.tags.add(
APIC(
encoding=3,
mime=”image/jpeg”,
type=3,
desc=”Cover”,
data=album_art.read()
)
)
audio.save()
os.rename(“audio.mp3″, f”{songInfo[0]} – {songInfo[1]}.mp3”)
os.remove(“thumbnail.jpg”)
print(f”[Console] Finished downloading {songInfo[0]} – {songInfo[1]}.mp3″)
def main():
if len(sys.argv) < 2:
print(“[Console] Error, no link provided.”)
print(“Usage: python main.py <url> [title] [artist]”)
return
if len(sys.argv) == 2: # Only the link is provided
link = sys.argv[1]
downloadAudio(link)
downloadThumbnail(link)
mergeAudio(“audio.mp3”, “thumbnail.jpg”, getInfo(link))
elif len(sys.argv) >= 4: # Link, title, and artist are provided
link = sys.argv[1]
info = [sys.argv[2], sys.argv[3]]
downloadAudio(link)
downloadThumbnail(link)
mergeAudio(“audio.mp3”, “thumbnail.jpg”, info)
if __name__ == “__main__”:
main()
submitted by /u/Prestigious_Ad_9555
[link] [comments]
r/learnpython So i am making a ytmp3 project. I’ve finnished it and decide to make it callable from Run(Win + r). I’ve added the folder that has the main.py, ytmp3.bat to system variable. I called and it works, but it has some permission issue. It seems like it don’t have the permission to write and delete from the folder. How i execute it from Run is: ytmp3 https://youtu.be/Wgw6tJ8yz9M?si=2xhuVB-ED_2WMmyR The log it gives: [Console] Downloading Audio. ERROR: unable to open for writing: [Errno 13] Permission denied: ‘downloaded_video.mp4.part’ Traceback (most recent call last): File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 603, in sanitize_open raise LockingUnsupportedError yt_dlp.utils.LockingUnsupportedError: File locking is not supported During handling of the above exception, another exception occurred: Traceback (most recent call last): File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloaderhttp.py”, line 263, in download ctx.stream, ctx.tmpfilename = self.sanitize_open( ^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 241, in wrapper for retry in RetryManager(self.params.get(‘file_access_retries’, 3), error_callback, fd=self): File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 5251, in __iter__ self.error_callback(self.error, self.attempt, self.retries) File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 234, in error_callback return RetryManager.report_retry( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 5259, in report_retry raise e File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 243, in wrapper return func(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 254, in sanitize_open f, filename = sanitize_open(filename, open_mode) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 606, in sanitize_open stream = open(filename, open_mode) ^^^^^^^^^^^^^^^^^^^^^^^^^ PermissionError: [Errno 13] Permission denied: ‘downloaded_video.mp4.part’ During handling of the above exception, another exception occurred: Traceback (most recent call last): File “D:System Variablesytmp3main.py”, line 148, in <module> main() File “D:System Variablesytmp3main.py”, line 135, in main downloadAudio(link) File “D:System Variablesytmp3main.py”, line 45, in downloadAudio info = ydl.extract_info(url, download=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1613, in extract_info return self.__extract_info(url, self.get_info_extractor(key), download, extra_info, process) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1624, in wrapper return func(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1780, in __extract_info return self.process_ie_result(ie_result, download, extra_info) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1839, in process_ie_result ie_result = self.process_video_result(ie_result, download=download) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 3011, in process_video_result self.process_info(new_info) File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 177, in wrapper return func(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 3479, in process_info success, real_download = self.dl(temp_filename, info_dict) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 3199, in dl return fd.download(name, new_info, subtitle) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 464, in download ret = self.real_download(filename, info_dict) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloaderhttp.py”, line 368, in real_download return download() ^^^^^^^^^^ File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloaderhttp.py”, line 269, in download self.report_error(f’unable to open for writing: {err}’) File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1090, in report_error self.trouble(f'{self._format_err(“ERROR:”, self.Styles.ERROR)} {message}’, *args, **kwargs) File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1029, in trouble raise DownloadError(message, exc_info) yt_dlp.utils.DownloadError: ERROR: unable to open for writing: [Errno 13] Permission denied: ‘downloaded_video.mp4.part’ Press any key to continue . . . The code i used: import yt_dlp import os import subprocess from PIL import Image from mutagen.id3 import ID3, TIT2, TPE1, APIC from mutagen.easyid3 import EasyID3 from mutagen.mp3 import MP3 import sys def getInfo(url): with yt_dlp.YoutubeDL({“quiet”: True}) as ydl: info = ydl.extract_info(url, download=False) # Extract metadata without downloading title = info.get(“title”) # Get the video title uploader = info.get(“uploader”) # Get the uploader’s channel name return [title, uploader] def downloadVideo(url): options = { ‘format’: ‘best’, ‘outtmpl’: ‘downloaded_video.%(ext)s’, ‘quiet’: True, ‘noprogress’: True, ‘no_warnings’: True } with yt_dlp.YoutubeDL(options) as ydl: ydl.download([url]) def downloadAudio(url): print(“[Console] Downloading Audio.”) video_filename = “downloaded_video.%(ext)s” audio_filename = “extracted_audio.mpeg” video_options = { ‘format’: ‘best’, ‘outtmpl’: video_filename, ‘quiet’: True, ‘noprogress’: True, ‘no_warnings’: True } with yt_dlp.YoutubeDL(video_options) as ydl: info = ydl.extract_info(url, download=True) downloaded_video_path = ydl.prepare_filename(info) if os.path.exists(downloaded_video_path): audio_command = [ “ffmpeg”, “-i”, downloaded_video_path, “-q:a”, “0”, “-map”, “a”, audio_filename ] subprocess.run(audio_command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) if os.path.exists(audio_filename): subprocess.run( [“ffmpeg”, “-i”, audio_filename, “-q:a”, “0”, “-map”, “a”, “audio.mp3″], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True ) os.remove(downloaded_video_path) os.remove(audio_filename) def downloadThumbnail(url, output_thumbnail=”thumbnail.jpg”, output_padded=”thumbnail_padded.jpg”): print(“[Console] Downloading thumbnail.”) with yt_dlp.YoutubeDL({“quiet”: True}) as ydl: info = ydl.extract_info(url, download=False) thumbnail_url = info.get(“thumbnail”) if thumbnail_url: subprocess.run( [“curl”, “-o”, output_thumbnail, thumbnail_url], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True ) subprocess.run( [“ffmpeg”, “-i”, output_thumbnail, “-q:v”, “1”, “thumbnail_converted.jpg”], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True ) os.remove(output_thumbnail) input_image = ‘thumbnail_converted.jpg’ output_image = ‘thumbnail.jpg’ with Image.open(input_image) as img: width, height = img.size new_size = max(width, height) padding_x = (new_size – width) // 2 padding_y = (new_size – height) // 2 subprocess.run( [‘ffmpeg’, ‘-i’, input_image, ‘-vf’, f’pad={new_size}:{new_size}:{padding_x}:{padding_y}:black’, ‘-q:v’, ‘1’, output_image], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True ) os.remove(input_image) def mergeAudio(Audio, thumbnail, songInfo): print(“[Console] Merging song.”) audio = EasyID3(Audio) audio[‘title’] = songInfo[0] audio[‘artist’] = songInfo[1] audio.save() audio = MP3(Audio, ID3=ID3) with open(thumbnail, “rb”) as album_art: audio.tags.add( APIC( encoding=3, mime=”image/jpeg”, type=3, desc=”Cover”, data=album_art.read() ) ) audio.save() os.rename(“audio.mp3″, f”{songInfo[0]} – {songInfo[1]}.mp3”) os.remove(“thumbnail.jpg”) print(f”[Console] Finished downloading {songInfo[0]} – {songInfo[1]}.mp3″) def main(): if len(sys.argv) < 2: print(“[Console] Error, no link provided.”) print(“Usage: python main.py <url> [title] [artist]”) return if len(sys.argv) == 2: # Only the link is provided link = sys.argv[1] downloadAudio(link) downloadThumbnail(link) mergeAudio(“audio.mp3”, “thumbnail.jpg”, getInfo(link)) elif len(sys.argv) >= 4: # Link, title, and artist are provided link = sys.argv[1] info = [sys.argv[2], sys.argv[3]] downloadAudio(link) downloadThumbnail(link) mergeAudio(“audio.mp3”, “thumbnail.jpg”, info) if __name__ == “__main__”: main() submitted by /u/Prestigious_Ad_9555 [link] [comments]
So i am making a ytmp3 project. I’ve finnished it and decide to make it callable from Run(Win + r). I’ve added the folder that has the main.py, ytmp3.bat to system variable. I called and it works, but it has some permission issue. It seems like it don’t have the permission to write and delete from the folder.
How i execute it from Run is:
ytmp3 https://youtu.be/Wgw6tJ8yz9M?si=2xhuVB-ED_2WMmyR
The log it gives:
[Console] Downloading Audio.
ERROR: unable to open for writing: [Errno 13] Permission denied: ‘downloaded_video.mp4.part’
Traceback (most recent call last):
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 603, in sanitize_open
raise LockingUnsupportedError
yt_dlp.utils.LockingUnsupportedError: File locking is not supported
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloaderhttp.py”, line 263, in download
ctx.stream, ctx.tmpfilename = self.sanitize_open(
^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 241, in wrapper
for retry in RetryManager(self.params.get(‘file_access_retries’, 3), error_callback, fd=self):
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 5251, in __iter__
self.error_callback(self.error, self.attempt, self.retries)
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 234, in error_callback
return RetryManager.report_retry(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 5259, in report_retry
raise e
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 243, in wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 254, in sanitize_open
f, filename = sanitize_open(filename, open_mode)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlputils_utils.py”, line 606, in sanitize_open
stream = open(filename, open_mode)
^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: ‘downloaded_video.mp4.part’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “D:System Variablesytmp3main.py”, line 148, in <module>
main()
File “D:System Variablesytmp3main.py”, line 135, in main
downloadAudio(link)
File “D:System Variablesytmp3main.py”, line 45, in downloadAudio
info = ydl.extract_info(url, download=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1613, in extract_info
return self.__extract_info(url, self.get_info_extractor(key), download, extra_info, process)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1624, in wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1780, in __extract_info
return self.process_ie_result(ie_result, download, extra_info)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1839, in process_ie_result
ie_result = self.process_video_result(ie_result, download=download)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 3011, in process_video_result
self.process_info(new_info)
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 177, in wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 3479, in process_info
success, real_download = self.dl(temp_filename, info_dict)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 3199, in dl
return fd.download(name, new_info, subtitle)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloadercommon.py”, line 464, in download
ret = self.real_download(filename, info_dict)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloaderhttp.py”, line 368, in real_download
return download()
^^^^^^^^^^
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpdownloaderhttp.py”, line 269, in download
self.report_error(f’unable to open for writing: {err}’)
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1090, in report_error
self.trouble(f'{self._format_err(“ERROR:”, self.Styles.ERROR)} {message}’, *args, **kwargs)
File “C:UsersACERAppDataLocalProgramsPythonPython312Libsite-packagesyt_dlpYoutubeDL.py”, line 1029, in trouble
raise DownloadError(message, exc_info)
yt_dlp.utils.DownloadError: ERROR: unable to open for writing: [Errno 13] Permission denied: ‘downloaded_video.mp4.part’
Press any key to continue . . .
The code i used:
import yt_dlp
import os
import subprocess
from PIL import Image
from mutagen.id3 import ID3, TIT2, TPE1, APIC
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
import sys
def getInfo(url):
with yt_dlp.YoutubeDL({“quiet”: True}) as ydl:
info = ydl.extract_info(url, download=False) # Extract metadata without downloading
title = info.get(“title”) # Get the video title
uploader = info.get(“uploader”) # Get the uploader’s channel name
return [title, uploader]
def downloadVideo(url):
options = {
‘format’: ‘best’,
‘outtmpl’: ‘downloaded_video.%(ext)s’,
‘quiet’: True,
‘noprogress’: True,
‘no_warnings’: True
}
with yt_dlp.YoutubeDL(options) as ydl:
ydl.download([url])
def downloadAudio(url):
print(“[Console] Downloading Audio.”)
video_filename = “downloaded_video.%(ext)s”
audio_filename = “extracted_audio.mpeg”
video_options = {
‘format’: ‘best’,
‘outtmpl’: video_filename,
‘quiet’: True,
‘noprogress’: True,
‘no_warnings’: True
}
with yt_dlp.YoutubeDL(video_options) as ydl:
info = ydl.extract_info(url, download=True)
downloaded_video_path = ydl.prepare_filename(info)
if os.path.exists(downloaded_video_path):
audio_command = [
“ffmpeg”,
“-i”, downloaded_video_path,
“-q:a”, “0”,
“-map”, “a”,
audio_filename
]
subprocess.run(audio_command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
if os.path.exists(audio_filename):
subprocess.run(
[“ffmpeg”, “-i”, audio_filename, “-q:a”, “0”, “-map”, “a”, “audio.mp3”],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True
)
os.remove(downloaded_video_path)
os.remove(audio_filename)
def downloadThumbnail(url, output_thumbnail=”thumbnail.jpg”, output_padded=”thumbnail_padded.jpg”):
print(“[Console] Downloading thumbnail.”)
with yt_dlp.YoutubeDL({“quiet”: True}) as ydl:
info = ydl.extract_info(url, download=False)
thumbnail_url = info.get(“thumbnail”)
if thumbnail_url:
subprocess.run(
[“curl”, “-o”, output_thumbnail, thumbnail_url],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True
)
subprocess.run(
[“ffmpeg”, “-i”, output_thumbnail, “-q:v”, “1”, “thumbnail_converted.jpg”],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True
)
os.remove(output_thumbnail)
input_image = ‘thumbnail_converted.jpg’
output_image = ‘thumbnail.jpg’
with Image.open(input_image) as img:
width, height = img.size
new_size = max(width, height)
padding_x = (new_size – width) // 2
padding_y = (new_size – height) // 2
subprocess.run(
[‘ffmpeg’, ‘-i’, input_image, ‘-vf’, f’pad={new_size}:{new_size}:{padding_x}:{padding_y}:black’, ‘-q:v’, ‘1’, output_image],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True
)
os.remove(input_image)
def mergeAudio(Audio, thumbnail, songInfo):
print(“[Console] Merging song.”)
audio = EasyID3(Audio)
audio[‘title’] = songInfo[0]
audio[‘artist’] = songInfo[1]
audio.save()
audio = MP3(Audio, ID3=ID3)
with open(thumbnail, “rb”) as album_art:
audio.tags.add(
APIC(
encoding=3,
mime=”image/jpeg”,
type=3,
desc=”Cover”,
data=album_art.read()
)
)
audio.save()
os.rename(“audio.mp3″, f”{songInfo[0]} – {songInfo[1]}.mp3”)
os.remove(“thumbnail.jpg”)
print(f”[Console] Finished downloading {songInfo[0]} – {songInfo[1]}.mp3″)
def main():
if len(sys.argv) < 2:
print(“[Console] Error, no link provided.”)
print(“Usage: python main.py <url> [title] [artist]”)
return
if len(sys.argv) == 2: # Only the link is provided
link = sys.argv[1]
downloadAudio(link)
downloadThumbnail(link)
mergeAudio(“audio.mp3”, “thumbnail.jpg”, getInfo(link))
elif len(sys.argv) >= 4: # Link, title, and artist are provided
link = sys.argv[1]
info = [sys.argv[2], sys.argv[3]]
downloadAudio(link)
downloadThumbnail(link)
mergeAudio(“audio.mp3”, “thumbnail.jpg”, info)
if __name__ == “__main__”:
main()
submitted by /u/Prestigious_Ad_9555
[link] [comments]