Skip to content

Commit 8ca1c05

Browse files
committed
swap NUL to os.devnull
1 parent c055776 commit 8ca1c05

3 files changed

Lines changed: 22 additions & 23 deletions

File tree

ffmpeg_normalize/_cmd_utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
import re
66
import shlex
77
import subprocess
8-
from platform import system
98
from shutil import which
10-
from typing import Iterator, Any
9+
from typing import Any, Iterator
1110

1211
from ffmpeg_progress_yield import FfmpegProgress
1312

1413
from ._errors import FFmpegNormalizeError
1514

1615
_logger = logging.getLogger(__name__)
1716

18-
NUL = "NUL" if system() in ("Windows", "cli") else "/dev/null"
1917
DUR_REGEX = re.compile(
2018
r"Duration: (?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})\.(?P<ms>\d{2})"
2119
)

ffmpeg_normalize/_media_file.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from tqdm import tqdm
1212

13-
from ._cmd_utils import DUR_REGEX, NUL, CommandRunner
13+
from ._cmd_utils import DUR_REGEX, CommandRunner
1414
from ._errors import FFmpegNormalizeError
1515
from ._streams import (
1616
AudioStream,
@@ -69,7 +69,7 @@ def __init__(
6969
self.output_file = output_file
7070
current_ext = os.path.splitext(output_file)[1][1:]
7171
# we need to check if it's empty, e.g. /dev/null or NUL
72-
if current_ext == "" or self.output_file == NUL:
72+
if current_ext == "" or self.output_file == os.devnull:
7373
self.output_ext = self.ffmpeg_normalize.extension
7474
else:
7575
self.output_ext = current_ext
@@ -114,7 +114,7 @@ def parse_streams(self) -> None:
114114
"0",
115115
"-f",
116116
"null",
117-
NUL,
117+
os.devnull,
118118
]
119119

120120
output = CommandRunner().run_command(cmd).get_output()
@@ -435,7 +435,7 @@ def _second_pass(self) -> Iterator[float]:
435435
return
436436

437437
# special case: if output is a null device, write directly to it
438-
if self.output_file == NUL:
438+
if self.output_file == os.devnull:
439439
cmd.append(self.output_file)
440440
else:
441441
temp_dir = mkdtemp()
@@ -452,14 +452,14 @@ def _second_pass(self) -> Iterator[float]:
452452
)
453453
raise e
454454
else:
455-
if self.output_file != NUL:
455+
if self.output_file != os.devnull:
456456
_logger.debug(
457457
f"Moving temporary file from {temp_file} to {self.output_file}"
458458
)
459459
move(temp_file, self.output_file)
460460
rmtree(temp_dir, ignore_errors=True)
461461
except Exception as e:
462-
if self.output_file != NUL:
462+
if self.output_file != os.devnull:
463463
rmtree(temp_dir, ignore_errors=True)
464464
raise e
465465

ffmpeg_normalize/_streams.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import logging
55
import os
66
import re
7-
from typing import TYPE_CHECKING, Iterator, List, Literal, Optional, TypedDict, cast
7+
from typing import TYPE_CHECKING, Iterator, Literal, TypedDict, cast
88

9-
from ._cmd_utils import NUL, CommandRunner, dict_to_filter_opts
9+
from ._cmd_utils import CommandRunner, dict_to_filter_opts
1010
from ._errors import FFmpegNormalizeError
1111

1212
if TYPE_CHECKING:
@@ -17,6 +17,7 @@
1717

1818
_loudnorm_pattern = re.compile(r"\[Parsed_loudnorm_(\d+)")
1919

20+
2021
class EbuLoudnessStatistics(TypedDict):
2122
input_i: float
2223
input_tp: float
@@ -239,7 +240,7 @@ def parse_astats(self) -> Iterator[float]:
239240
"-sn",
240241
"-f",
241242
"null",
242-
NUL,
243+
os.devnull,
243244
]
244245

245246
cmd_runner = CommandRunner()
@@ -310,7 +311,7 @@ def parse_loudnorm_stats(self) -> Iterator[float]:
310311
"-sn",
311312
"-f",
312313
"null",
313-
NUL,
314+
os.devnull,
314315
]
315316

316317
cmd_runner = CommandRunner()
@@ -322,11 +323,13 @@ def parse_loudnorm_stats(self) -> Iterator[float]:
322323
)
323324

324325
# only one stream
325-
self.loudness_statistics["ebu_pass1"] = next(iter(AudioStream.prune_and_parse_loudnorm_output(output).values()))
326+
self.loudness_statistics["ebu_pass1"] = next(
327+
iter(AudioStream.prune_and_parse_loudnorm_output(output).values())
328+
)
326329

327330
@staticmethod
328331
def prune_and_parse_loudnorm_output(
329-
output: str
332+
output: str,
330333
) -> dict[int, EbuLoudnessStatistics]:
331334
"""
332335
Prune ffmpeg progress lines from output and parse the loudnorm filter output.
@@ -344,7 +347,7 @@ def prune_and_parse_loudnorm_output(
344347

345348
@staticmethod
346349
def _parse_loudnorm_output(
347-
output_lines: list[str]
350+
output_lines: list[str],
348351
) -> dict[int, EbuLoudnessStatistics]:
349352
"""
350353
Parse the output of a loudnorm filter to get the EBU loudness statistics.
@@ -403,7 +406,9 @@ def _parse_loudnorm_output(
403406
# convert to floats
404407
loudnorm_stats[key] = float(loudnorm_stats[key])
405408

406-
result[stream_index] = cast(EbuLoudnessStatistics, loudnorm_stats)
409+
result[stream_index] = cast(
410+
EbuLoudnessStatistics, loudnorm_stats
411+
)
407412
stream_index = -1
408413
except Exception as e:
409414
raise FFmpegNormalizeError(
@@ -504,15 +509,11 @@ def get_second_pass_opts_ebu(self) -> str:
504509
"offset": self._constrain(
505510
stats["target_offset"], -99, 99, name="target_offset"
506511
),
507-
"measured_i": self._constrain(
508-
stats["input_i"], -99, 0, name="input_i"
509-
),
512+
"measured_i": self._constrain(stats["input_i"], -99, 0, name="input_i"),
510513
"measured_lra": self._constrain(
511514
stats["input_lra"], 0, 99, name="input_lra"
512515
),
513-
"measured_tp": self._constrain(
514-
stats["input_tp"], -99, 99, name="input_tp"
515-
),
516+
"measured_tp": self._constrain(stats["input_tp"], -99, 99, name="input_tp"),
516517
"measured_thresh": self._constrain(
517518
stats["input_thresh"], -99, 0, name="input_thresh"
518519
),

0 commit comments

Comments
 (0)