Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permission denied #209

Closed
anmaz opened this issue Aug 20, 2017 · 20 comments · Fixed by #262
Closed

Permission denied #209

anmaz opened this issue Aug 20, 2017 · 20 comments · Fixed by #262

Comments

@anmaz
Copy link

anmaz commented Aug 20, 2017

i want use playback to play a music

but it alway tell me that i don't have permission,by the way, i can use cmd.exe to play a music.

code like that:
In [9]: sou1=AudioSegment.from_mp3(r'd:\02.mp3')

In [10]: play(sou1)

IOError Traceback (most recent call last)
in ()
----> 1 play(sou1)

c:\python27\lib\site-packages\pydub\playback.pyc in play(audio_segment)
44 _play_with_pyaudio(audio_segment)
45 except ImportError:
---> 46 _play_with_ffplay(audio_segment)

c:\python27\lib\site-packages\pydub\playback.pyc in _play_with_ffplay(seg)
16 def _play_with_ffplay(seg):
17 with NamedTemporaryFile("w+b", suffix=".wav") as f:
---> 18 seg.export(f.name, "wav")
19 subprocess.call([PLAYER, "-nodisp", "-autoexit", "-hide_banner",
f.name])
20

c:\python27\lib\site-packages\pydub\audio_segment.pyc in export(self, out_f, for
mat, codec, bitrate, parameters, tags, id3v2_version, cover)
579 id3v2_allowed_versions = ['3', '4']
580
--> 581 out_f = _fd_or_path_or_tempfile(out_f, 'wb+')
582 out_f.seek(0)
583

c:\python27\lib\site-packages\pydub\utils.pyc in _fd_or_path_or_tempfile(fd, mod
e, tempfile)
57
58 if isinstance(fd, basestring):
---> 59 fd = open(fd, mode=mode)
60
61 return fd

IOError: [Errno 13] Permission denied: 'c:\users\admini~1\appdata\local\tem
p\tmpuzjjby.wav'

System configuration

  • Python version: 2.7
  • Pydub version: 0.20.0
  • ffmpeg or avlib?: ffmpeg
  • ffmpeg/avlib version: 3.3.3
@jiaaro
Copy link
Owner

jiaaro commented Aug 29, 2017

Pydub uses temp files (specifically NamedTemporaryFile) pretty heavily. It looks like you don't have permission to write in the temp directory your python interpreter is configured to use.

You can try setting the TMPDIR environment variable which is used by python to determine where to store temporary files (by default)

@pdsing
Copy link

pdsing commented Dec 7, 2017

I changed the TMPDIR environment variable to a subfolder of the current directory. It is still giving the same error. My python program is able to write and read files from this folder for sure. Somehow pydub seems to throw the error:

Traceback (most recent call last):
File "try_pydub.py", line 115, in
sys.exit(main())
File "try_pydub.py", line 110, in main
create_90_secs_audio(full_path_wav_file, full_path_audio_segments)
File "try_pydub.py", line 51, in create_90_secs_audio
play(sound)
File "C:\Python27\lib\site-packages\pydub\playback.py", line 46, in play
_play_with_ffplay(audio_segment)
File "C:\Python27\lib\site-packages\pydub\playback.py", line 18, in _play_with_ffplay
seg.export(f.name, "wav")
File "C:\Python27\lib\site-packages\pydub\audio_segment.py", line 581, in export
out_f = _fd_or_path_or_tempfile(out_f, 'wb+')
File "C:\Python27\lib\site-packages\pydub\utils.py", line 59, in _fd_or_path_or_tempfile
fd = open(fd, mode=mode)
IOError: [Errno 13] Permission denied: 'c:\users\pd\ml\ebook-v2\tmp\tmpn2yvsv.wav'

@kkealy
Copy link

kkealy commented Mar 23, 2018

So, you open this temp file using 'with', right?

def _play_with_ffplay(seg):
    with NamedTemporaryFile("w+b", suffix=".wav") as f:
        seg.export(f.name, "wav")
        subprocess.call([PLAYER, "-nodisp", "-autoexit", "-hide_banner", f.name])

But then you pass its name to AudioSegment.export(), which is, as you described, formatted as string:

out_f (string):

At this point, the file is still open. Then you open it again:

out_f = _fd_or_path_or_tempfile(out_f, 'wb+')

On linux distros, it works, but Windows isn't as forgiving. Opening a file twice, what for?
So, when you try to open it again, Windows tells you to gtfo because it's already opened.
The more you know.

A quick and dirty fix is to just save the name in a local variable, and pass that to the seg.export and subprocess.call calls, like this:

def _play_with_ffplay(seg):
    with NamedTemporaryFile("w+b", suffix=".wav") as f:
        fileName = f.name

    seg.export(fileName, "wav")
    subprocess.call([PLAYER, "-nodisp", "-autoexit", "-hide_banner", fileName])
    os.remove(fileName)

But at the end of the day, antlarr's solution is better. No need to write pointless shite to the disk for every single audio playback.

Goodest & bestest regards,
Me, yeah.

antlarr added a commit to antlarr/pydub that referenced this issue Mar 23, 2018
…m_file

Rename from_file to from_file_using_temporary_files just in case there's
any case in which the new from_file doesn't work (I couldn't find any,
but just in case, I guess it would be nice to keep it maybe as
deprecated).

Add a new from_file function that does all the reading on memory with
pipes, not using any temporary file, which is faster and doesn't wear
down disks for heavy usages.

The new from_file function reads the input file and passes it to ffmpeg
using a pipe and then reads ffmpeg output using another pipe directly
to memory.

Since wav files have the file length in the header and ffmpeg can't
write it since it's working on a stream, we modify the resulting raw data
from ffmpeg before reading it using the standard method.

Fixes jiaaro#237
Might also fix jiaaro#209
@jsl303
Copy link

jsl303 commented Dec 11, 2018

Any update on this? I also have the problem.
I'm running Windows 10, and I installed the master branch.
The error message is below.
Thanks,

Traceback (most recent call last):
File "", line 1, in
File "C:\Users\jl\Anaconda3\envs\audionews\lib\site-packages\pydub\playback.py", line 71, in play
_play_with_ffplay(audio_segment)
File "C:\Users\jl\Anaconda3\envs\audionews\lib\site-packages\pydub\playback.py", line 18, in _play_with_ffplay
seg.export(f.name, "wav")
File "C:\Users\jl\Anaconda3\envs\audionews\lib\site-packages\pydub\audio_segment.py", line 780, in export
out_f = _fd_or_path_or_tempfile(out_f, 'wb+')
File "C:\Users\jl\Anaconda3\envs\audionews\lib\site-packages\pydub\utils.py", line 57, in _fd_or_path_or_tempfile
fd = open(fd, mode=mode)
PermissionError: [Errno 13] Permission denied: 'C:\Users\jl\AppData\Local\Temp\tmp5aed_m3d.wav'

@liangzid
Copy link

Have Someone solved this problem? Please help me.

Traceback (most recent call last):
  File "D:/desktop/MIDI_learning/test.py", line 26, in <module>
    play(song_PianDuan_s[i])
  File "D:\games\Anaconda\lib\site-packages\pydub\playback.py", line 71, in play
    _play_with_ffplay(audio_segment)
  File "D:\games\Anaconda\lib\site-packages\pydub\playback.py", line 18, in _play_with_ffplay
    seg.export(f.name, "wav")
  File "D:\games\Anaconda\lib\site-packages\pydub\audio_segment.py", line 780, in export
    out_f = _fd_or_path_or_tempfile(out_f, 'wb+')
  File "D:\games\Anaconda\lib\site-packages\pydub\utils.py", line 57, in _fd_or_path_or_tempfile
    fd = open(fd, mode=mode)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\22730\\AppData\\Local\\Temp\\tmpj5yymayl.wav'

@dimitri320
Copy link

Same here, and looks like still no solution. I'm working on a Mac

@zegevlier
Copy link

I have the same problem. I'm on windows 10.

@pholpar
Copy link

pholpar commented Jun 16, 2019

I had a similar problem (the same kind of "PermissionError: [Errno 13] Permission denied") with playback on Windows 8.1, and installing simpleaudio (executing 'pip install simpleaudio') solved the issue.
A comment regarding playback from page https://github.com/jiaaro/pydub#installation: "simpleaudio strongly recommended, even if you are installing ffmpeg/libav"
See this issue: #343

@RealNicolasBourbaki
Copy link

On win 10, installing simpleaudio also can solve the problem.

@GillHawk
Copy link

GillHawk commented Mar 22, 2020

The fix for windows and other OSs that do not support opening same file twice is close the file stream before open it again. As pointed out by "kkealy", file is first opened by NamedTemporaryFile() then by seg.export() .

Added a line in playback.py:

def _play_with_ffplay(seg):
    with NamedTemporaryFile("w+b", suffix=".wav") as f:
        f.close() # close the file stream
        seg.export(f.name, "wav")
        subprocess.call([PLAYER, "-nodisp", "-autoexit", "-hide_banner", f.name])

@peterhudson
Copy link

I used @GillHawk solution... worked like a charm on Win10.

@zaphodikus
Copy link

How is this one-liner bug fix not published before closing this ticket?

@BrenoHA
Copy link

BrenoHA commented Aug 26, 2021

@jiaaro take a look at GillHawk's Solution
Quick solution and still not published

The fix for windows and other OSs that do not support opening same file twice is close the file stream before open it again. As pointed out by "kkealy", file is first opened by NamedTemporaryFile() then by seg.export() .

Added a line in playback.py:

def _play_with_ffplay(seg):
    with NamedTemporaryFile("w+b", suffix=".wav") as f:
        f.close() # close the file stream
        seg.export(f.name, "wav")
        subprocess.call([PLAYER, "-nodisp", "-autoexit", "-hide_banner", f.name])

@fmaillar
Copy link

fmaillar commented Mar 27, 2022

That's it. This line in func _play_with_ffplay of playback.py resolve the problem :)

    f.close() # close the file stream

lcavasso added a commit to lcavasso/pydub that referenced this issue Jul 11, 2023
per jiaaro#209 (comment)
Recent Windows update brought this issue back, this change resolves it.
@khadimFaye
Copy link

im working on windows 11 pro but same problem do anyone have solution??

@DaslavCl
Copy link

im working on windows 11 pro but same problem do anyone have solution??

just "pip install simpleaudio"
magic fix for windows11

@liam-keepmove
Copy link

The fix for windows and other OSs that do not support opening same file twice is close the file stream before open it again. As pointed out by "kkealy", file is first opened by NamedTemporaryFile() then by seg.export() .

Added a line in playback.py:

def _play_with_ffplay(seg):
    with NamedTemporaryFile("w+b", suffix=".wav") as f:
        f.close() # close the file stream
        seg.export(f.name, "wav")
        subprocess.call([PLAYER, "-nodisp", "-autoexit", "-hide_banner", f.name])

yes! this method is useful.

@pintert3
Copy link

pintert3 commented Sep 1, 2023

Is there a problem with fixing this issue? It looks like it's been on hold for a VERY LONG time, given that it's basically a one liner.

@zdyy001
Copy link

zdyy001 commented Sep 14, 2023

The fix for windows and other OSs that do not support opening same file twice is close the file stream before open it again. As pointed out by "kkealy", file is first opened by NamedTemporaryFile() then by seg.export() .

Added a line in playback.py:

def _play_with_ffplay(seg):
    with NamedTemporaryFile("w+b", suffix=".wav") as f:
        f.close() # close the file stream
        seg.export(f.name, "wav")
        subprocess.call([PLAYER, "-nodisp", "-autoexit", "-hide_banner", f.name])

It does work!

@niloriver
Copy link

as @DaslavCl already said "pip install simpleaudio" magically solves the problem on Windows 11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.