Reputation: 2776
I wrote this function. I am trying to have it output the mp3 file but it isn't working. I keep getting this error. I have tried messing with putting .mp3 on "output_filepath" or "%(ext)s" with no avail.
def start_extraction(url, output_file):
output_filepath = 'music/%s.mp3' % output_file
temp_filepath = 'temp/%s-%s.%s' % (uuid.uuid4(), output_file, '%(ext)s')
ydl_opts = {
'format': 'bestaudio/best', # choice of quality
'extractaudio' : True, # only keep the audio
'audioformat' : 'mp3', # convert to mp3
'outtmpl': temp_filepath, # name the location
'noplaylist' : True, # only download single song, not playlist
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
result = ydl.download([url])
#
if result == 0:
# Move the temporary file to the proper location.
shutil.move(temp_filepath, output_filepath)
return result
So I get this error
[youtube] TZB8HRrJ0KM: Downloading webpage
[youtube] TZB8HRrJ0KM: Extracting video information
[youtube] TZB8HRrJ0KM: Downloading DASH manifest
Done downloading, now converting ...
05:54:31 IOError: [Errno 2] No such file or directory: u'temp/61499bc8-0c2e-4719-8556-826af80fd028-How_To_Shave_Your_Beard_Like_A_Man.%(ext)s'
Traceback (most recent call last):
File "/vagrant/yout/env/lib/python2.7/site-packages/rq/worker.py", line 557, in perform_job
rv = job.perform()
File "/vagrant/yout/env/lib/python2.7/site-packages/rq/job.py", line 492, in perform
self._result = self.func(*self.args, **self.kwargs)
File "/vagrant/yout/worker.py", line 100, in extract_audio
extraction_result = start_extraction(url, audio_filename)
File "/vagrant/yout/worker.py", line 130, in start_extraction
shutil.move(temp_filepath, output_filepath)
File "/usr/lib/python2.7/shutil.py", line 302, in move
copy2(src, real_dst)
File "/usr/lib/python2.7/shutil.py", line 130, in copy2
copyfile(src, dst)
File "/usr/lib/python2.7/shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: u'temp/61499bc8-0c2e-4719-8556-826af80fd028-How_To_Shave_Your_Beard_Like_A_Man.%(ext)s'
Traceback (most recent call last):
File "/vagrant/yout/env/lib/python2.7/site-packages/rq/worker.py", line 557, in perform_job
rv = job.perform()
File "/vagrant/yout/env/lib/python2.7/site-packages/rq/job.py", line 492, in perform
self._result = self.func(*self.args, **self.kwargs)
File "/vagrant/yout/worker.py", line 100, in extract_audio
extraction_result = start_extraction(url, audio_filename)
File "/vagrant/yout/worker.py", line 130, in start_extraction
shutil.move(temp_filepath, output_filepath)
File "/usr/lib/python2.7/shutil.py", line 302, in move
copy2(src, real_dst)
File "/usr/lib/python2.7/shutil.py", line 130, in copy2
copyfile(src, dst)
File "/usr/lib/python2.7/shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: u'temp/61499bc8-0c2e-4719-8556-826af80fd028-How_To_Shave_Your_Beard_Like_A_Man.%(ext)s'
Upvotes: 0
Views: 1557
Reputation: 2776
Ok so after a while of debuging. I found out that the file was getting saved as a m4a. Also needed to replace the variable since it mutated. So for anyone who is having using youtube-dl with itunes here is the solution I was able to come up with.
def start_extraction(url, output_file):
output_filepath = 'music/%s.mp3' % output_file
temp_filepath = 'temp/%s-%s.%s' % (uuid.uuid4(), output_file, '%(ext)s')
ydl_opts = {
'format': 'bestaudio/best', # choice of quality
'extractaudio' : True, # only keep the audio
'audioformat' : 'mp3', # convert to mp3
'outtmpl': temp_filepath, # name the location
'noplaylist' : True, # only download single song, not playlist
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
result = ydl.download([url])
#
if result == 0:
# Move the temporary file to the proper location.
temp_filepath = temp_filepath.replace('.%(ext)s', ".mp3")
shutil.move(temp_filepath, output_filepath)
return result
Upvotes: 2
Reputation: 3526
You got the wrong variable name, output_file
should be replaced by output_filepath
.
temp_filepath = 'temp/%s-%s.%s' % (uuid.uuid4(), output_file, '%(ext)s')
This line is causing the issue. String is not formatted properly.
temp_filepath = 'temp/%s-%s' % (uuid.uuid4(), output_filepath)
or
temp_filepath = 'temp/%s-%(title)s.%(ext)s' % uuid.uuid4()
or
temp_filepath = 'temp/%s-%s.%(ext)s' % (uuid.uuid4(), output_filepath)
Upvotes: 0