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

Enhancement: Custom FFmpeg Path Support for FFMPEG Backend #142

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion audioread/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def available_backends(flush_cache=False):
return BACKENDS


def audio_open(path, backends=None):
def audio_open(path, backends=None,ffmpeg_path=None):
"""Open an audio file using a library that is available on this
system.

Expand All @@ -124,6 +124,8 @@ def audio_open(path, backends=None):

for BackendClass in backends:
try:
if BackendClass is ffdec.FFmpegAudioFile:
return BackendClass(path,ffmpeg_path=ffmpeg_path)
return BackendClass(path)
except DecodeError:
pass
Expand Down
37 changes: 35 additions & 2 deletions audioread/ffdec.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import re
import subprocess
import sys
import os
import threading
import time
from io import DEFAULT_BUFFER_SIZE

from .exceptions import DecodeError
from .base import AudioFile

COMMANDS = ('ffmpeg', 'avconv')
COMMANDS = ['ffmpeg', 'avconv']

if sys.platform == "win32":
PROC_FLAGS = 0x08000000
Expand Down Expand Up @@ -76,7 +77,32 @@ def run(self):
# Stream closed (EOF).
break

def find_ffmpeg_path(input_path):
'''
input_path: str
check wheather the given path is a valid ffmpeg path or contains ffmpeg in all operating systems
return: str if path is valid and None if it is invalid
'''
if os.path.isdir(input_path):
'''
if the given path is a directory, then check for the existence of ffmpeg or ffmpeg.exe
'''
if os.path.exists(input_path + os.sep + 'ffmpeg'):
return input_path + os.sep + 'ffmpeg'
elif os.path.exists(input_path + os.sep + 'ffmpeg.exe'):
return input_path + os.sep + 'ffmpeg.exe'
else:
return None
else:
if os.path.exists(input_path):
return input_path
elif os.path.exists(input_path + '.exe'):
return input_path + '.exe'
else:
return None



def popen_multiple(commands, command_args, *args, **kwargs):
"""Like `subprocess.Popen`, but can try multiple commands in case
some are not available.
Expand Down Expand Up @@ -121,7 +147,7 @@ def available():

class FFmpegAudioFile(AudioFile):
"""An audio file decoded by the ffmpeg command-line utility."""
def __init__(self, filename, block_size=DEFAULT_BUFFER_SIZE):
def __init__(self, filename, block_size=DEFAULT_BUFFER_SIZE,ffmpeg_path=None):
# On Windows, we need to disable the subprocess's crash dialog
# in case it dies. Passing SEM_NOGPFAULTERRORBOX to SetErrorMode
# disables this behavior.
Expand All @@ -137,6 +163,13 @@ def __init__(self, filename, block_size=DEFAULT_BUFFER_SIZE):
ctypes.windll.kernel32.SetErrorMode(
previous_error_mode | SEM_NOGPFAULTERRORBOX
)


if ffmpeg_path is not None:
global COMMANDS
ffmpeg_path = find_ffmpeg_path(ffmpeg_path)
if ffmpeg_path is not None:
COMMANDS = [ffmpeg_path] + COMMANDS

try:
self.proc = popen_multiple(
Expand Down