Skip to content

Commit 8dd6714

Browse files
committed
Updates for --gpu and --no-gpu support
1 parent 0b79896 commit 8dd6714

File tree

2 files changed

+96
-42
lines changed

2 files changed

+96
-42
lines changed

README.rst

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,12 @@ Usage
286286
Advanced encoding settings:
287287
Advanced options for encoding
288288
289-
--no-gpu Disable use of GPU acceleration.
290-
All MACs with Haswell CPU or later support this (Macs after 2013).
291-
See following link as well:
292-
https://en.wikipedia.org/wiki/List_of_Macintosh_models_grouped_by_CPU_type#Haswell
289+
--no-gpu Disable use of GPU acceleration. Default on Apple Silicon Mac and on Non-MACs.
290+
--gpu Use GPU acceleration. Default on Intel Macs.
293291
Note: ffmpeg currently seems to have issues on Apple Silicon with GPU acceleration, --no-gpu might need to be set to produce video.
294-
(default: False)
295-
--gpu Use GPU acceleration, only enable if supported by hardware.
296-
--gpu_type has to be provided as well when enabling this parameter (default: False)
292+
--gpu_type has to be provided as well on Non-Macs when enabling this parameter (default: False)
297293
--gpu_type {nvidia,intel,rpi}
298-
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)
294+
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)
299295
--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)
300296
--quality {LOWEST,LOWER,LOW,MEDIUM,HIGH}
301297
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
871867
872868
*--no-gpu*
873869
874-
For MacOS only! Disables GPU acceleration. If not provided GPU acceleration will be used.
870+
Disables GPU acceleration.
871+
Intel Macs: GPU acceleration is enabled by default, use this parameter to disable it.
872+
Apple Silicon Macs: GPU acceleration is disabled by default due to current issues with ffmpeg.
873+
Non-Macs: GPU acceleration is disabled by default.
874+
875+
If this parameter is used in combination with --gpu then the first one will have preference.
876+
877+
Note: we can only detect Apple Silicon if Python deployed is the Universal2 binary OR the tesla_dashcam executable is for Apple Silicon.
878+
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.
875879
876880
*--gpu*
877881
878-
All platforms except Macs. Enables GPU acceleration.
882+
Enables GPU acceleration.
883+
Intel Macs: this is already enabled by default
884+
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.
885+
Non-Macs: to enable GPU acceleration, parameter parameter --gpu_type has to be provided as well then to identify the hardware.
886+
887+
If this parameter is used in combination with --no-gpu then the first one will have preference.
879888
880-
Note: When providing this parameter to enable GPU you also have to provide parameter --gpu_type.
889+
Note: we can only detect Apple Silicon if Python deployed is the Universal2 binary OR the tesla_dashcam executable is for Apple Silicon.
890+
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.
881891
882892
*--gpu_type*
883893
@@ -887,7 +897,7 @@ The following parameters are more advanced settings to determine how ffmpeg shou
887897
888898
nvidia: if NVIDIA GPU is installed
889899
890-
RPi: on Raspberry Pi systems
900+
rpi: on Raspberry Pi systems
891901
892902
*--no-faststart*
893903
@@ -1469,6 +1479,7 @@ Release Notes
14691479
- New: Metadata tag title in video file is now set to reason for event (if exist) and timestamp or start/end timestamp
14701480
- New: Metadata tag creation_time in video files created is now set to start timestamp of that particular video.
14711481
- New: When scanning folders a message will be printed after every 10 folders scanned to show progress.
1482+
- New: --gpu and --no-gpu are now valid arguments irrespective of platform.
14721483
- Changed: Improvement for Docker file size and stability. Contributed by magicalyak
14731484
- Changed: Choice values for parameters (i.e. FULLSCREEN, intel, black) are now case-insensitive.
14741485
- Changed: Updated supporting libraries to latest available.

tesla_dashcam/tesla_dashcam.py

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import logging
77
import os
88
import sys
9+
from platform import processor as platform_processor
910
import json
1011
from datetime import datetime, timedelta, timezone
1112
from glob import glob, iglob
@@ -36,7 +37,7 @@
3637
# different ones to be created based on where it should go to (stdout,
3738
# log file, ...).
3839

39-
VERSION = {"major": 0, "minor": 1, "patch": 17, "beta": 4}
40+
VERSION = {"major": 0, "minor": 1, "patch": 17, "beta": 5}
4041
VERSION_STR = f"v{VERSION['major']}.{VERSION['minor']}.{VERSION['patch']}"
4142

4243
if VERSION["beta"] > -1:
@@ -116,6 +117,16 @@
116117

117118
display_ts = False
118119

120+
PLATFORM = sys.platform
121+
# Allow setting for testing.
122+
# PLATFORM = "darwin"
123+
# PLATFORM = "win32"
124+
# PLATFORM = "linux"
125+
126+
PROCESSOR = platform_processor()
127+
# Allow setting for testing.
128+
# PROCESSOR = "arm"
129+
119130

120131
class Camera_Clip(object):
121132
""" Camera Clip Class
@@ -3021,11 +3032,11 @@ def notify_linux(title, subtitle, message):
30213032

30223033
def notify(title, subtitle, message):
30233034
""" Call function to send notification based on OS """
3024-
if sys.platform == "darwin":
3035+
if PLATFORM == "darwin":
30253036
notify_macos(title, subtitle, message)
3026-
elif sys.platform == "win32":
3037+
elif PLATFORM == "win32":
30273038
notify_windows(title, subtitle, message)
3028-
elif sys.platform == "linux":
3039+
elif PLATFORM == "linux":
30293040
notify_linux(title, subtitle, message)
30303041

30313042

@@ -3037,17 +3048,17 @@ def main() -> int:
30373048
)
30383049

30393050
internal_ffmpeg = getattr(sys, "frozen", None) is not None
3040-
ffmpeg_default = resource_path(FFMPEG.get(sys.platform, "ffmpeg"))
3051+
ffmpeg_default = resource_path(FFMPEG.get(PLATFORM, "ffmpeg"))
30413052

3042-
movie_folder = os.path.join(str(Path.home()), MOVIE_HOMEDIR.get(sys.platform), "")
3053+
movie_folder = os.path.join(str(Path.home()), MOVIE_HOMEDIR.get(PLATFORM), "")
30433054

30443055
global display_ts
30453056

30463057
# Check if ffmpeg exist, if not then hope it is in default path or
30473058
# provided.
30483059
if not os.path.isfile(ffmpeg_default):
30493060
internal_ffmpeg = False
3050-
ffmpeg_default = FFMPEG.get(sys.platform, "ffmpeg")
3061+
ffmpeg_default = FFMPEG.get(PLATFORM, "ffmpeg")
30513062

30523063
epilog = (
30533064
"This program leverages ffmpeg which is included. See https://ffmpeg.org/ for more information on ffmpeg"
@@ -3300,7 +3311,7 @@ def main() -> int:
33003311
"--font",
33013312
required=False,
33023313
type=str,
3303-
default=DEFAULT_FONT.get(sys.platform, None),
3314+
default=DEFAULT_FONT.get(PLATFORM, None),
33043315
help="Fully qualified filename (.ttf) to the font to be chosen for timestamp.",
33053316
)
33063317
text_overlay_group.add_argument(
@@ -3492,27 +3503,55 @@ def main() -> int:
34923503
title="Advanced encoding settings", description="Advanced options for encoding"
34933504
)
34943505

3495-
mac_gpu_help = (
3496-
"R|Disable use of GPU acceleration.\n"
3497-
" All MACs with Haswell CPU or later support this (Macs after 2013).\n"
3498-
" See following link as well: \n"
3499-
" https://en.wikipedia.org/wiki/List_of_Macintosh_models_grouped_by_CPU_type#Haswell\n"
3500-
" Note: ffmpeg currently seems to have issues on Apple Silicon with GPU acceleration, --no-gpu might need to be set to produce video.\n"
3501-
)
3506+
if PLATFORM == "darwin":
3507+
if PROCESSOR != "arm":
3508+
nogpuhelp = (
3509+
"R|Disable use of GPU acceleration, default is to use GPU acceleration.\n"
3510+
" Note: if this is being run on Apple Silicon then please check README for --no-gpu!!\n"
3511+
)
3512+
gpuhelp = (
3513+
"R|Use GPU acceleration (this is the default).\n"
3514+
" Note: if this is being run on Apple Silicon then please check README for --gpu!!\n"
3515+
)
3516+
else:
3517+
nogpuhelp = "R|Disable use of GPU acceleration, this is the default as currently ffmpeg has issues on Apple Silicon with GPU acceleration.\n"
3518+
gpuhelp = (
3519+
"R|Use GPU acceleration.\n"
3520+
" Note: ffmpeg currently seems to have issues on Apple Silicon with GPU acceleration resulting in corrupt video.\n"
3521+
)
35023522

3503-
nonmac_gpu_help = (
3504-
"R|Use GPU acceleration, only enable if supported by hardware.\n"
3505-
" --gpu_type has to be provided as well when enabling this parameter"
3506-
)
3523+
advancedencoding_group.add_argument(
3524+
"--no-gpu",
3525+
dest="gpu",
3526+
action="store_false",
3527+
default=argparse.SUPPRESS,
3528+
help=nogpuhelp,
3529+
)
35073530

3508-
PRINT_ALL = True
3509-
if sys.platform == "darwin" or PRINT_ALL:
35103531
advancedencoding_group.add_argument(
3511-
"--no-gpu", dest="gpu", action="store_true", help=mac_gpu_help
3532+
"--gpu",
3533+
dest="gpu",
3534+
action="store_true",
3535+
default=argparse.SUPPRESS,
3536+
help=gpuhelp,
35123537
)
3513-
if sys.platform != "darwin" or PRINT_ALL:
3538+
3539+
elif PLATFORM != "darwin":
35143540
advancedencoding_group.add_argument(
3515-
"--gpu", dest="gpu", action="store_true", help=nonmac_gpu_help
3541+
"--no-gpu",
3542+
dest="gpu",
3543+
action="store_false",
3544+
default=argparse.SUPPRESS,
3545+
help="R|Disable use of GPU acceleration, this is the default.\n",
3546+
)
3547+
3548+
advancedencoding_group.add_argument(
3549+
"--gpu",
3550+
dest="gpu",
3551+
action="store_true",
3552+
default=argparse.SUPPRESS,
3553+
help="R|Use GPU acceleration, only enable if supported by hardware.\n"
3554+
" --gpu_type has to be provided as well when enabling this parameter",
35163555
)
35173556

35183557
advancedencoding_group.add_argument(
@@ -3733,7 +3772,7 @@ def main() -> int:
37333772
ffmpeg = ffmpeg_default if getattr(args, "ffmpeg", None) is None else args.ffmpeg
37343773
if which(ffmpeg) is None:
37353774
print(
3736-
f"{get_current_timestamp()}ffmpeg is a requirement, unable to find {ffmpeg} executable. Please ensure it exist and is located"
3775+
f"{get_current_timestamp()}ffmpeg is a requirement, unable to find {ffmpeg} executable. Please ensure it exist and is located "
37373776
f"within PATH environment or provide full path using parameter --ffmpeg."
37383777
)
37393778

@@ -3938,23 +3977,23 @@ def main() -> int:
39383977
if not args.no_timestamp and text_overlay_format is not None:
39393978
if layout_settings.font.font is None:
39403979
print(
3941-
f"{get_current_timestamp()}Unable to get a font file for platform {sys.platform}. Please provide valid font file using "
3980+
f"{get_current_timestamp()}Unable to get a font file for platform {PLATFORM}. Please provide valid font file using "
39423981
f"--font or disable timestamp using --no-timestamp."
39433982
)
39443983
return 0
39453984

39463985
# noinspection PyPep8
39473986
temp_font_file = (
39483987
f"c:\{layout_settings.font.font}"
3949-
if sys.platform == "win32"
3988+
if PLATFORM == "win32"
39503989
else layout_settings.font.font
39513990
)
39523991
if not os.path.isfile(temp_font_file):
39533992
print(
39543993
f"{get_current_timestamp()}Font file {temp_font_file} does not exist. Provide a valid font file using --font or"
39553994
f" disable timestamp using --no-timestamp"
39563995
)
3957-
if sys.platform == "linux":
3996+
if PLATFORM == "linux":
39583997
print(
39593998
f"{get_current_timestamp()}You can also install the fonts using for example: "
39603999
f"apt-get install ttf-freefont"
@@ -4002,7 +4041,11 @@ def main() -> int:
40024041

40034042
ffmpeg_params = ["-preset", args.compression, "-crf", MOVIE_QUALITY[args.quality]]
40044043

4005-
use_gpu = not args.gpu if sys.platform == "darwin" else args.gpu
4044+
use_gpu = (
4045+
getattr(args, "gpu", True)
4046+
if PLATFORM == "darwin" and PROCESSOR != "arm"
4047+
else getattr(args, "gpu", False)
4048+
)
40064049

40074050
video_encoding = []
40084051
if not "enc" in args:
@@ -4015,7 +4058,7 @@ def main() -> int:
40154058
# GPU acceleration enabled
40164059
if use_gpu:
40174060
print(f"{get_current_timestamp()}GPU acceleration is enabled")
4018-
if sys.platform == "darwin":
4061+
if PLATFORM == "darwin":
40194062
video_encoding = video_encoding + ["-allow_sw", "1"]
40204063
encoding = encoding + "_mac"
40214064

0 commit comments

Comments
 (0)