Skip to content

Commit

Permalink
🐛 StreamGear: Refactor stream copy handling (Fixes #396)
Browse files Browse the repository at this point in the history
💬 When the output codec is set to "copy" (stream copy mode), certain video
processing parameters like "-vf" (video filters) and "-aspect" (aspect ratio)
are not supported and can lead to errors and invalid output files.

♻️ This commit refactors the internal `PreProcess` method in StreamGear API to handle
the stream copy mode correctly:

- 🥅 Moved the existing code for setting "-vf" and "-aspect" inside conditional block that checks if
  the output stream codec is not "copy".
- 🔊 Added an else block to log warnings and discard "-vf" and "-aspect" in stream copy mode.
  • Loading branch information
abhiTronix committed May 21, 2024
1 parent 20dec0a commit e655f90
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions vidgear/gears/streamgear.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,21 @@ def __PreProcess(self, channels=0, rgb=False):
default_codec = "libx264rgb" if rgb else "libx264"
output_parameters["-vcodec"] = self.__params.pop("-vcodec", default_codec)
# enable optimizations and enforce compatibility
output_parameters["-vf"] = self.__params.pop("-vf", "format=yuv420p")
aspect_ratio = Fraction(
self.__inputwidth / self.__inputheight
).limit_denominator(10)
output_parameters["-aspect"] = ":".join(str(aspect_ratio).split("/"))
if output_parameters["-vcodec"] != "copy":
# NOTE: these parameters only supported when stream copy not defined
output_parameters["-vf"] = self.__params.pop("-vf", "format=yuv420p")
aspect_ratio = Fraction(
self.__inputwidth / self.__inputheight
).limit_denominator(10)
output_parameters["-aspect"] = ":".join(str(aspect_ratio).split("/"))
else:
# log warnings for these parameters
self.__params.pop("-vf", False) and logger.warning(
"Filtering and stream copy cannot be used together. Discarding `-vf` parameter!"
)
self.__params.pop("-aspect", False) and logger.warning(
"Overriding aspect ratio with stream copy may produce invalid files. Discarding `-aspect` parameter!"
)
# w.r.t selected codec
if output_parameters["-vcodec"] in [
"libx264",
Expand Down

0 comments on commit e655f90

Please sign in to comment.