Skip to content
3 changes: 3 additions & 0 deletions fastflix/command_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from psutil import Popen

from fastflix.shared import clean_env

try:
from psutil import (
HIGH_PRIORITY_CLASS,
Expand Down Expand Up @@ -93,6 +95,7 @@ def start_exec(self, command, work_dir: str = None, shell: bool = False, errors=
stderr=stderr_handle,
stdin=PIPE, # FFmpeg can try to read stdin and wrecks havoc on linux
encoding="utf-8",
env=clean_env(),
)
except PermissionError:
logger.error(
Expand Down
4 changes: 4 additions & 0 deletions fastflix/flix.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from fastflix.language import t
from fastflix.models.config import Config
from fastflix.models.fastflix_app import FastFlixApp
from fastflix.shared import clean_env

here = os.path.abspath(os.path.dirname(__file__))
re_tff = re.compile(r"TFF:\s+(\d+)")
Expand Down Expand Up @@ -151,6 +152,7 @@ def execute(command: List, work_dir: Union[Path, str] = None, timeout: int = Non
cwd=work_dir,
timeout=timeout,
encoding="utf-8",
env=clean_env(),
)


Expand Down Expand Up @@ -361,6 +363,7 @@ def extract_attachment(ffmpeg: Path, source: Path, stream: int, work_dir: Path,
stderr=PIPE,
stdin=PIPE,
cwd=work_dir,
env=clean_env(),
)
try:
stdout, _ = proc.communicate(timeout=3)
Expand Down Expand Up @@ -732,6 +735,7 @@ def _detect_hdr10_plus_tool(app: FastFlixApp, config: Config, stream) -> bool:
stdout=PIPE,
stderr=PIPE,
stdin=PIPE, # FFmpeg can try to read stdin and wrecks havoc
env=clean_env(),
)

parser_version = get_hdr10_parser_version(config)
Expand Down
5 changes: 3 additions & 2 deletions fastflix/rigaya_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dataclasses import dataclass, field
from subprocess import run, PIPE

from fastflix.shared import clean_env

@dataclass
class Encoder:
Expand Down Expand Up @@ -72,11 +73,11 @@ def parse_qsv_devices(split_text: list[str]) -> QSVEncoder:
def run_check_features(executable, is_qsv=False):
outputs = []
if is_qsv:
result = run([executable, "--check-features"], stdout=PIPE, stderr=PIPE, encoding="utf-8")
result = run([executable, "--check-features"], stdout=PIPE, stderr=PIPE, encoding="utf-8", env=clean_env())
outputs.append(result.stdout.splitlines())
else:
for i in range(10):
result = run([executable, "--check-features", str(i)], stdout=PIPE, stderr=PIPE, encoding="utf-8")
result = run([executable, "--check-features", str(i)], stdout=PIPE, stderr=PIPE, encoding="utf-8", env=clean_env())
if result.stderr:
break
outputs.append(result.stdout.splitlines())
Expand Down
11 changes: 11 additions & 0 deletions fastflix/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
base_path = os.path.abspath(".")
pyinstaller = False

def clean_env():
"""Return a copy of os.environ with PyInstaller's LD_LIBRARY_PATH removed.

PyInstaller sets LD_LIBRARY_PATH to its bundled lib dir, which causes
system tools like ffmpeg to load incompatible shared libraries.
"""
env = os.environ.copy()
if pyinstaller:
env.pop("LD_LIBRARY_PATH", None)
return env

from PySide6 import QtGui, QtWidgets

from fastflix.language import t
Expand Down
7 changes: 6 additions & 1 deletion fastflix/widgets/background_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ffmpeg_normalize import FFmpegNormalize

from fastflix.flix import extract_attachments
from fastflix.shared import clean_env
from fastflix.language import t
from fastflix.models.fastflix_app import FastFlixApp
from fastflix.shared import clean_file_string
Expand Down Expand Up @@ -42,7 +43,7 @@ def __init__(self, main, command=""):

def run(self):
self.main.thread_logging_signal.emit(f"DEBUG:{t('Generating thumbnail')}: {_format_command(self.command)}")
result = run(self.command, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
result = run(self.command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, env=clean_env())
if result.returncode > 0:
if "No such filter: 'zscale'" in result.stdout.decode(encoding="utf-8", errors="ignore"):
self.main.thread_logging_signal.emit(
Expand Down Expand Up @@ -156,6 +157,7 @@ def run(self):
command,
stdout=PIPE,
stderr=STDOUT,
env=clean_env(),
)
stdout, _ = self._process.communicate()
returncode = self._process.returncode
Expand Down Expand Up @@ -224,6 +226,7 @@ def _get_subtitle_format(self):
stdout=PIPE,
stderr=STDOUT,
text=True,
env=clean_env(),
)

if result.returncode != 0:
Expand Down Expand Up @@ -530,6 +533,7 @@ def run(self):
stdout=PIPE,
stderr=open(self.app.fastflix.current_video.work_path / "hdr10extract_out.txt", "wb"),
# stdin=PIPE, # FFmpeg can try to read stdin and wrecks havoc
env=clean_env(),
)

process_two = Popen(
Expand All @@ -539,6 +543,7 @@ def run(self):
stdin=process.stdout,
encoding="utf-8",
cwd=str(self.app.fastflix.current_video.work_path),
env=clean_env(),
)

with open(self.app.fastflix.current_video.work_path / "hdr10extract_out.txt", "r", encoding="utf-8") as f:
Expand Down
5 changes: 3 additions & 2 deletions fastflix/widgets/main_post_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ def _record_history(self, video: Video, success: bool = True):
from subprocess import run as subprocess_run

from fastflix.flix import generate_thumbnail_command

from fastflix.shared import clean_env

thumb_command = generate_thumbnail_command(
config=self.app.fastflix.config,
source=video.source,
Expand All @@ -150,7 +151,7 @@ def _record_history(self, video: Video, success: bool = True):
start_time=video.video_settings.start_time or 0,
input_track=video.video_settings.selected_track,
)
result = subprocess_run(thumb_command, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
result = subprocess_run(thumb_command, stdin=PIPE, stdout=PIPE, stderr=STDOUT, env=clean_env())
if result.returncode != 0 or not thumb_output.exists():
logger.warning("Failed to generate thumbnail for history entry")
entry.thumbnail_filename = ""
Expand Down
3 changes: 2 additions & 1 deletion fastflix/widgets/windows/crop_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from fastflix.encoders.common import helpers
from fastflix.flix import generate_thumbnail_command
from fastflix.shared import clean_env
from fastflix.language import t
from fastflix.shared import time_to_number
from fastflix.ui_scale import scaler
Expand Down Expand Up @@ -690,7 +691,7 @@ def generate_image(self, with_crop=False):

logger.info(f"Generating crop preview: {thumb_command}")

thumb_run = run(thumb_command, shell=True, stderr=PIPE, stdout=PIPE)
thumb_run = run(thumb_command, shell=True, stderr=PIPE, stdout=PIPE, env=clean_env())
if thumb_run.returncode > 0:
logger.warning(f"Could not generate crop preview: {thumb_run.stdout} |----| {thumb_run.stderr}")
return
Expand Down
3 changes: 2 additions & 1 deletion fastflix/widgets/windows/large_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from fastflix.flix import (
generate_thumbnail_command,
)
from fastflix.shared import clean_env
from fastflix.encoders.common import helpers
from fastflix.resources import get_icon
from fastflix.language import t
Expand Down Expand Up @@ -93,7 +94,7 @@ def generate_image(self):

logger.info(f"Generating large thumbnail: {thumb_command}")

thumb_run = run(thumb_command, shell=True, stderr=PIPE, stdout=PIPE)
thumb_run = run(thumb_command, shell=True, stderr=PIPE, stdout=PIPE, env=clean_env())
if thumb_run.returncode > 0:
logger.warning(f"Could not generate large thumbnail: {thumb_run.stdout} |----| {thumb_run.stderr}")
return
Expand Down
Loading