From 8dd6714a02eaa7fa347c55aa2a87534cdeeb9fae Mon Sep 17 00:00:00 2001 From: Erik Hendrix Date: Sat, 14 Aug 2021 11:04:12 -0600 Subject: [PATCH] Updates for --gpu and --no-gpu support --- README.rst | 35 +++++++---- tesla_dashcam/tesla_dashcam.py | 103 +++++++++++++++++++++++---------- 2 files changed, 96 insertions(+), 42 deletions(-) diff --git a/README.rst b/README.rst index b9de661..5eda679 100755 --- a/README.rst +++ b/README.rst @@ -286,16 +286,12 @@ Usage Advanced encoding settings: Advanced options for encoding - --no-gpu Disable use of GPU acceleration. - All MACs with Haswell CPU or later support this (Macs after 2013). - See following link as well: - https://en.wikipedia.org/wiki/List_of_Macintosh_models_grouped_by_CPU_type#Haswell + --no-gpu Disable use of GPU acceleration. Default on Apple Silicon Mac and on Non-MACs. + --gpu Use GPU acceleration. Default on Intel Macs. Note: ffmpeg currently seems to have issues on Apple Silicon with GPU acceleration, --no-gpu might need to be set to produce video. - (default: False) - --gpu Use GPU acceleration, only enable if supported by hardware. - --gpu_type has to be provided as well when enabling this parameter (default: False) + --gpu_type has to be provided as well on Non-Macs when enabling this parameter (default: False) --gpu_type {nvidia,intel,rpi} - Type of graphics card (GPU) in the system. This determines the encoder that will be used.This parameter is mandatory if --gpu is provided. (default: None) + Type of graphics card (GPU) in the system. This determines the encoder that will be used.This parameter is mandatory if --gpu is provided on Non-Macs. (default: None) --no-faststart Do not enable flag faststart on the resulting video files. Use this when using a network share and errors occur during encoding. (default: False) --quality {LOWEST,LOWER,LOW,MEDIUM,HIGH} Define the quality setting for the video, higher quality means bigger file size but might not be noticeable. (default: LOWER) @@ -871,13 +867,27 @@ The following parameters are more advanced settings to determine how ffmpeg shou *--no-gpu* - For MacOS only! Disables GPU acceleration. If not provided GPU acceleration will be used. + Disables GPU acceleration. + Intel Macs: GPU acceleration is enabled by default, use this parameter to disable it. + Apple Silicon Macs: GPU acceleration is disabled by default due to current issues with ffmpeg. + Non-Macs: GPU acceleration is disabled by default. + + If this parameter is used in combination with --gpu then the first one will have preference. + + Note: we can only detect Apple Silicon if Python deployed is the Universal2 binary OR the tesla_dashcam executable is for Apple Silicon. + If running on Apple Silicon but using the x64 executable or x64 Python then tesla_dashcam will not be able to detect it is running on Apple Silicon. *--gpu* - All platforms except Macs. Enables GPU acceleration. + Enables GPU acceleration. + Intel Macs: this is already enabled by default + Apple Silicon Macs: to enable GPU acceleration. Note that current ffmpeg produces a corrupt video when doing this but newer versions of ffmpeg might work. + Non-Macs: to enable GPU acceleration, parameter parameter --gpu_type has to be provided as well then to identify the hardware. + + If this parameter is used in combination with --no-gpu then the first one will have preference. - Note: When providing this parameter to enable GPU you also have to provide parameter --gpu_type. + Note: we can only detect Apple Silicon if Python deployed is the Universal2 binary OR the tesla_dashcam executable is for Apple Silicon. + If running on Apple Silicon but using the x64 executable or x64 Python then tesla_dashcam will not be able to detect it is running on Apple Silicon. *--gpu_type* @@ -887,7 +897,7 @@ The following parameters are more advanced settings to determine how ffmpeg shou nvidia: if NVIDIA GPU is installed - RPi: on Raspberry Pi systems + rpi: on Raspberry Pi systems *--no-faststart* @@ -1469,6 +1479,7 @@ Release Notes - New: Metadata tag title in video file is now set to reason for event (if exist) and timestamp or start/end timestamp - New: Metadata tag creation_time in video files created is now set to start timestamp of that particular video. - New: When scanning folders a message will be printed after every 10 folders scanned to show progress. + - New: --gpu and --no-gpu are now valid arguments irrespective of platform. - Changed: Improvement for Docker file size and stability. Contributed by magicalyak - Changed: Choice values for parameters (i.e. FULLSCREEN, intel, black) are now case-insensitive. - Changed: Updated supporting libraries to latest available. diff --git a/tesla_dashcam/tesla_dashcam.py b/tesla_dashcam/tesla_dashcam.py index d486da9..4c3adce 100644 --- a/tesla_dashcam/tesla_dashcam.py +++ b/tesla_dashcam/tesla_dashcam.py @@ -6,6 +6,7 @@ import logging import os import sys +from platform import processor as platform_processor import json from datetime import datetime, timedelta, timezone from glob import glob, iglob @@ -36,7 +37,7 @@ # different ones to be created based on where it should go to (stdout, # log file, ...). -VERSION = {"major": 0, "minor": 1, "patch": 17, "beta": 4} +VERSION = {"major": 0, "minor": 1, "patch": 17, "beta": 5} VERSION_STR = f"v{VERSION['major']}.{VERSION['minor']}.{VERSION['patch']}" if VERSION["beta"] > -1: @@ -116,6 +117,16 @@ display_ts = False +PLATFORM = sys.platform +# Allow setting for testing. +# PLATFORM = "darwin" +# PLATFORM = "win32" +# PLATFORM = "linux" + +PROCESSOR = platform_processor() +# Allow setting for testing. +# PROCESSOR = "arm" + class Camera_Clip(object): """ Camera Clip Class @@ -3021,11 +3032,11 @@ def notify_linux(title, subtitle, message): def notify(title, subtitle, message): """ Call function to send notification based on OS """ - if sys.platform == "darwin": + if PLATFORM == "darwin": notify_macos(title, subtitle, message) - elif sys.platform == "win32": + elif PLATFORM == "win32": notify_windows(title, subtitle, message) - elif sys.platform == "linux": + elif PLATFORM == "linux": notify_linux(title, subtitle, message) @@ -3037,9 +3048,9 @@ def main() -> int: ) internal_ffmpeg = getattr(sys, "frozen", None) is not None - ffmpeg_default = resource_path(FFMPEG.get(sys.platform, "ffmpeg")) + ffmpeg_default = resource_path(FFMPEG.get(PLATFORM, "ffmpeg")) - movie_folder = os.path.join(str(Path.home()), MOVIE_HOMEDIR.get(sys.platform), "") + movie_folder = os.path.join(str(Path.home()), MOVIE_HOMEDIR.get(PLATFORM), "") global display_ts @@ -3047,7 +3058,7 @@ def main() -> int: # provided. if not os.path.isfile(ffmpeg_default): internal_ffmpeg = False - ffmpeg_default = FFMPEG.get(sys.platform, "ffmpeg") + ffmpeg_default = FFMPEG.get(PLATFORM, "ffmpeg") epilog = ( "This program leverages ffmpeg which is included. See https://ffmpeg.org/ for more information on ffmpeg" @@ -3300,7 +3311,7 @@ def main() -> int: "--font", required=False, type=str, - default=DEFAULT_FONT.get(sys.platform, None), + default=DEFAULT_FONT.get(PLATFORM, None), help="Fully qualified filename (.ttf) to the font to be chosen for timestamp.", ) text_overlay_group.add_argument( @@ -3492,27 +3503,55 @@ def main() -> int: title="Advanced encoding settings", description="Advanced options for encoding" ) - mac_gpu_help = ( - "R|Disable use of GPU acceleration.\n" - " All MACs with Haswell CPU or later support this (Macs after 2013).\n" - " See following link as well: \n" - " https://en.wikipedia.org/wiki/List_of_Macintosh_models_grouped_by_CPU_type#Haswell\n" - " Note: ffmpeg currently seems to have issues on Apple Silicon with GPU acceleration, --no-gpu might need to be set to produce video.\n" - ) + if PLATFORM == "darwin": + if PROCESSOR != "arm": + nogpuhelp = ( + "R|Disable use of GPU acceleration, default is to use GPU acceleration.\n" + " Note: if this is being run on Apple Silicon then please check README for --no-gpu!!\n" + ) + gpuhelp = ( + "R|Use GPU acceleration (this is the default).\n" + " Note: if this is being run on Apple Silicon then please check README for --gpu!!\n" + ) + else: + nogpuhelp = "R|Disable use of GPU acceleration, this is the default as currently ffmpeg has issues on Apple Silicon with GPU acceleration.\n" + gpuhelp = ( + "R|Use GPU acceleration.\n" + " Note: ffmpeg currently seems to have issues on Apple Silicon with GPU acceleration resulting in corrupt video.\n" + ) - nonmac_gpu_help = ( - "R|Use GPU acceleration, only enable if supported by hardware.\n" - " --gpu_type has to be provided as well when enabling this parameter" - ) + advancedencoding_group.add_argument( + "--no-gpu", + dest="gpu", + action="store_false", + default=argparse.SUPPRESS, + help=nogpuhelp, + ) - PRINT_ALL = True - if sys.platform == "darwin" or PRINT_ALL: advancedencoding_group.add_argument( - "--no-gpu", dest="gpu", action="store_true", help=mac_gpu_help + "--gpu", + dest="gpu", + action="store_true", + default=argparse.SUPPRESS, + help=gpuhelp, ) - if sys.platform != "darwin" or PRINT_ALL: + + elif PLATFORM != "darwin": advancedencoding_group.add_argument( - "--gpu", dest="gpu", action="store_true", help=nonmac_gpu_help + "--no-gpu", + dest="gpu", + action="store_false", + default=argparse.SUPPRESS, + help="R|Disable use of GPU acceleration, this is the default.\n", + ) + + advancedencoding_group.add_argument( + "--gpu", + dest="gpu", + action="store_true", + default=argparse.SUPPRESS, + help="R|Use GPU acceleration, only enable if supported by hardware.\n" + " --gpu_type has to be provided as well when enabling this parameter", ) advancedencoding_group.add_argument( @@ -3733,7 +3772,7 @@ def main() -> int: ffmpeg = ffmpeg_default if getattr(args, "ffmpeg", None) is None else args.ffmpeg if which(ffmpeg) is None: print( - f"{get_current_timestamp()}ffmpeg is a requirement, unable to find {ffmpeg} executable. Please ensure it exist and is located" + f"{get_current_timestamp()}ffmpeg is a requirement, unable to find {ffmpeg} executable. Please ensure it exist and is located " f"within PATH environment or provide full path using parameter --ffmpeg." ) @@ -3938,7 +3977,7 @@ def main() -> int: if not args.no_timestamp and text_overlay_format is not None: if layout_settings.font.font is None: print( - f"{get_current_timestamp()}Unable to get a font file for platform {sys.platform}. Please provide valid font file using " + f"{get_current_timestamp()}Unable to get a font file for platform {PLATFORM}. Please provide valid font file using " f"--font or disable timestamp using --no-timestamp." ) return 0 @@ -3946,7 +3985,7 @@ def main() -> int: # noinspection PyPep8 temp_font_file = ( f"c:\{layout_settings.font.font}" - if sys.platform == "win32" + if PLATFORM == "win32" else layout_settings.font.font ) if not os.path.isfile(temp_font_file): @@ -3954,7 +3993,7 @@ def main() -> int: f"{get_current_timestamp()}Font file {temp_font_file} does not exist. Provide a valid font file using --font or" f" disable timestamp using --no-timestamp" ) - if sys.platform == "linux": + if PLATFORM == "linux": print( f"{get_current_timestamp()}You can also install the fonts using for example: " f"apt-get install ttf-freefont" @@ -4002,7 +4041,11 @@ def main() -> int: ffmpeg_params = ["-preset", args.compression, "-crf", MOVIE_QUALITY[args.quality]] - use_gpu = not args.gpu if sys.platform == "darwin" else args.gpu + use_gpu = ( + getattr(args, "gpu", True) + if PLATFORM == "darwin" and PROCESSOR != "arm" + else getattr(args, "gpu", False) + ) video_encoding = [] if not "enc" in args: @@ -4015,7 +4058,7 @@ def main() -> int: # GPU acceleration enabled if use_gpu: print(f"{get_current_timestamp()}GPU acceleration is enabled") - if sys.platform == "darwin": + if PLATFORM == "darwin": video_encoding = video_encoding + ["-allow_sw", "1"] encoding = encoding + "_mac"