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

ffmpeg executable path #1252

Merged
merged 5 commits into from
Apr 10, 2021
Merged
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
8 changes: 6 additions & 2 deletions spotdl/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ def console_entry_point():
'''
arguments = parse_arguments()

if ffmpeg.has_correct_version(arguments.ignore_ffmpeg_version) is False:
if ffmpeg.has_correct_version(
arguments.ignore_ffmpeg_version,
arguments.ffmpeg or "ffmpeg"
) is False:
sys.exit(1)

SpotifyClient.init(
Expand All @@ -105,7 +108,7 @@ def console_entry_point():
print(f"Will download to: {os.path.abspath(arguments.path)}")
os.chdir(arguments.path)

with DownloadManager() as downloader:
with DownloadManager(arguments.ffmpeg) as downloader:

for request in arguments.url:
if 'open.spotify.com' in request and 'track' in request:
Expand Down Expand Up @@ -158,6 +161,7 @@ def parse_arguments():
)
parser.add_argument("url", type=str, nargs="+", help="URL to a song/album/playlist")
parser.add_argument("-o", "--output", help="Output directory path", dest="path")
parser.add_argument("-f", "--ffmpeg", help="Path to ffmpeg", dest="ffmpeg")
parser.add_argument("--ignore-ffmpeg-version",
help="Ignore ffmpeg version", action="store_true")

Expand Down
8 changes: 6 additions & 2 deletions spotdl/download/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DownloadManager():
# ! Big pool sizes on slow connections will lead to more incomplete downloads
poolSize = 4

def __init__(self):
def __init__(self, ffmpeg_path: str = "ffmpeg"):

# start a server for objects shared across processes
self.displayManager = DisplayManager()
Expand All @@ -53,6 +53,9 @@ def __init__(self):
self.thread_executor = concurrent.futures.ThreadPoolExecutor(
max_workers=self.poolSize)

# ! ffmpeg path
self.ffmpeg_path = ffmpeg_path

def __enter__(self):
return self

Expand Down Expand Up @@ -216,7 +219,8 @@ async def download_song(self, songObj: SongObj) -> None:
ffmpeg_success = await ffmpeg.convert(
trackAudioStream=trackAudioStream,
downloadedFilePath=downloadedFilePath,
convertedFilePath=convertedFilePath
convertedFilePath=convertedFilePath,
ffmpegPath=self.ffmpeg_path
)

if dispayProgressTracker:
Expand Down
15 changes: 11 additions & 4 deletions spotdl/download/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import re


def has_correct_version(skip_version_check: bool = False) -> bool:
def has_correct_version(skip_version_check: bool = False, ffmpeg_path: str = "ffmpeg") -> bool:
process = subprocess.Popen(
['ffmpeg', '-version'],
[ffmpeg_path, '-version'],
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)

Expand All @@ -24,6 +24,10 @@ def has_correct_version(skip_version_check: bool = False) -> bool:
return False

version = result.group(0).replace("ffmpeg version ", "")

# remove all non numeric characters from string example: n4.3
version = re.sub(r"[a-zA-Z]", "", version)

if float(version) < 4.3:
print(f"Your FFmpeg installation is too old ({version}), please update to 4.3+\n",
file=sys.stderr)
Expand All @@ -32,7 +36,7 @@ def has_correct_version(skip_version_check: bool = False) -> bool:
return True


async def convert(trackAudioStream, downloadedFilePath, convertedFilePath) -> bool:
async def convert(trackAudioStream, downloadedFilePath, convertedFilePath, ffmpegPath) -> bool:
# convert downloaded file to MP3 with normalization

# ! -af loudnorm=I=-7:LRA applies EBR 128 loudness normalization algorithm with
Expand All @@ -55,8 +59,11 @@ async def convert(trackAudioStream, downloadedFilePath, convertedFilePath) -> bo
# ! sampled length of songs matches the actual length (i.e. a 5 min song won't display
# ! as 47 seconds long in your music player, yeah that was an issue earlier.)

if ffmpegPath is None:
ffmpegPath = "ffmpeg"

command = (
'ffmpeg -v quiet -y -i "%s" -acodec libmp3lame -abr true '
f'{ffmpegPath} -v quiet -y -i "%s" -acodec libmp3lame -abr true '
f"-b:a {trackAudioStream.bitrate} "
'-af "apad=pad_dur=2, dynaudnorm, loudnorm=I=-17" "%s"'
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_entry_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def new_initialize(client_id, client_secret):
def patch_dependencies(mocker, monkeypatch):
"""This is a helper fixture to patch out everything that shouldn't be called here"""
monkeypatch.setattr(SpotifyClient, "init", new_initialize)
monkeypatch.setattr(DownloadManager, "__init__", lambda _: None)
monkeypatch.setattr(DownloadManager, "__init__", lambda *_: None)
monkeypatch.setattr(ffmpeg, "has_correct_version", lambda *_: True)
mocker.patch.object(DownloadManager, "download_single_song", autospec=True)
mocker.patch.object(
Expand Down