Search code examples
pythonffmpegsubprocessshutil

subprocess.call can't find file/shutil.which failed in pycharm


I am trying to transform a mp3 to a wav file in pycharm using subprocess

import subprocess
subprocess.call(['ffmpeg', '-i','test.mp3','test.wav'])

It returns error of not finding file, so I change the 'ffmpeg' to its path on my pc and it work.

The problem is that I am making an app and others might install ffpmeg on other's location (since it is download with zip and can be unzip at any place), but I don't know how to get its full path.

I tried using os module

import os
print(os.path('ffmpeg.exe'))

but it seems like it is not able to get the path of exe

Traceback (most recent call last):
  File "C:\Users\Percy\PycharmProjects\APP\test3.py", line 8, in <module>
    print(os.path('ffmpeg.exe'))
TypeError: 'module' object is not callable

I also tried shutil module

import shutil
print(shutil.which('ffmpeg'))
print(shutil.which('ffmpeg.exe'))

but it returns 2 None (prob wrong cause I am 100% sure I have installed ffmpeg)

None
None

I want to ask if there is any way to get the full path of ffmpeg in pycharm or any method that I can make ffmpeg install in designated path with the app when it is downloaded by users


Solution

  • If you can make "everyone" to install using my ffmpeg-downloader then all of you can install FFmpeg by:

    pip install ffmpeg-downloader
    ffdl install
    

    Then in Python your package could use

    import ffmpeg_downloader as ffdl
    
    sp.run([ffdl.ffmpeg_path, '-i', 'input.mp4', 'output.mkv'])
    

    Alternately, you can use static-ffmpeg to (dynamically) install FFmpeg to Lib/site-package. (See the linked GitHub page for howto.)