0

My (first) web app uses pydub, which depends on ffmpeg. On my local windows environment, I installed ffmpeg and added the path to the ffmpeg executables to the windows "path" environment variables.

It all works locally, but bow that I have deployed my app to PythonAnywhere, the following line in my code is causing an error:

 sound.export(export_path, format="mp3", bitrate="128k")

I believe the error is because this code relies on ffmpeg.

I have read on their forums that ffmpeg is installed for all users on PythonAnywhere. Is there something I need to do to get it to work? Do I need to add the path of the ffmpeg files to the environment variables? I have a .env file with other env variables -- would I need to add something to this?

ragmats
  • 91
  • 8
  • You can try to incorporate [`static-ffmpeg`](https://github.com/zackees/static_ffmpeg) package to your venv. This package downloads the ffmpeg binaries in venv's `site-packages` folder. So, I suspect it would work on PythonAnywhere. – kesh Nov 15 '22 at 16:32
  • I installed the package and added the usage to my views.py, but I am still getting the same error. Maybe I have done something wrong? – ragmats Nov 15 '22 at 16:54
  • AFAIK, the package does not automatically add its ffmpeg binaries to the system path when you use it in a Python program. There is an undocumented `static_ffmpeg.add_paths()` function, which appears to download and add the binaries to the path. (The owner appears to have added the package recently.) I strongly suggest review the code in the package (not a large repo) to get better understanding of it yourself. – kesh Nov 15 '22 at 17:01
  • I had added "import static_ffmpeg" and then "static_ffmpeg.add_paths()" to views.py, but this did not seem to change anything. – ragmats Nov 15 '22 at 17:03
  • When I type "ffmpeg" in the PythonAnywhere terminal, I get the version and configuration info, so it appears to be installed. There must be something else I need to do in order to get my code to correctly use it. As mentioned, it worked locally on Windows, where I had ffmpeg installed and added to the path environment variable. I have no idea if or how to do this on PythonAnywhere though. – ragmats Nov 15 '22 at 17:16
  • I don't know how PythonAnywhere works, but you can try to print `os.environ['PATH']` in your Python program and compare it to `$PATH` in the terminal to inspect if ffmpeg dir is included in both. (I can't tell you what to do if not... but least you know what to pursue then) – kesh Nov 15 '22 at 17:21
  • When I print os.environ['PATH'] from my app, I get "/home/gridsquid/.local/bin:/usr/local/bin:/usr/bin:/bin". When I type "$PATH" into the terminal, I get: "bash: /home/gridsquid/.virtualenvs/myvirtualenv/bin:/home/gridsquid/.local/bin:/usr/local/julia-1.6.1/bin:/usr/lib/postgresql/12/bin/:/home/gridsquid/.local/bin:/usr/local/julia-1.6.1/bin:/usr/lib/postgresql/12/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin: No such file or directory". I'm not sure what to do with this information (sorry, noob here). – ragmats Nov 15 '22 at 17:46
  • Also, I got this reply from PythonAnywhere: "It is installed for all users and is on the path." So, why would my code not be working here when it was working locally? – ragmats Nov 15 '22 at 18:14
  • oh ok, so the service supports ffmpeg by default. my bad, I wasn't assuming that. Does `os.system('ffmpeg')` work? – kesh Nov 15 '22 at 18:31

2 Answers2

1

Got home and tried PythonAnywhere myself, and I don't find any issue with it and its FFmpeg.

Without installing anything (no FFmpeg, no Python packages), I run successfully in REPL:

>>> os.system('ffmpeg')
[snipped ffmpeg banner]
>>> import pydub
>>> pydub.utils.get_encoder_name()
'ffmpeg'
>>> pydub.utils.get_prober_name()
'ffprobe'
>>> pydub.utils.get_supported_codecs()
[snipped a large list of FFmpeg codecs]

So, your issue is not ffmpeg/pydub. Post the exact error. Can it be that it doesn't like you to save a file in the directory you specified?

kesh
  • 4,515
  • 2
  • 12
  • 20
1

The problem was not ffmpeg - which I have confirmed is installed for all users on PythonAnywhere. Instead, my issue was the export path I was using with pydub. I fixed my issue by changing:

export_path = "media/my_path"

to

export_path = "home/my_app/my_app/media/my_path"

which is then used in

sound.export(export_path, format="mp3", bitrate="128k")
ragmats
  • 91
  • 8