Last active
May 12, 2024 22:05
-
-
Save Antrikshy/483235fb445500f60cdc3bdb577e4418 to your computer and use it in GitHub Desktop.
Extract and rename BMW Drive Recorder video clips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Script to extract video files written by the BMW Drive Recorder in my 2024 model | |
and rename them using the date and time in the directory names that it writes | |
into USB storage. | |
This is free and unencumbered software released into the public domain and comes | |
with no warranties. | |
Requirements: | |
1. A Python 3 runtime installed. | |
2. (Optional, see below) The ffmpeg command line tool installed. | |
Place in same directory as the DriveRecorder directory extracted from the | |
vehicle. Run script using Python, like `python3 ./bmw_drive_recorder_extract.py`. | |
This assumes a directory structure like: | |
- DriveRecorder | |
- YYYY-MM-DD_HH-MM-SS(am|pm)_Manual | |
- Some_Video_File.ts | |
- Metadata | |
- ... | |
Output looks like: | |
- Extracted | |
- BMW_YYYY-MM-DD_HH-MM-SS(am|pm).(ts|mp4) | |
- ... | |
The script can convert to MP4 along the way using ffmpeg (and a default | |
codec). For that: | |
1. Install the ffmpeg command line utility. | |
2. Set CONVERT_TO_MP4_WITH_FFMPEG below to True before running. | |
Of course, you can edit the script further to change inputs and outputs. | |
""" | |
CONVERT_TO_MP4_WITH_FFMPEG = False | |
import os | |
import shutil | |
# Path to the DriveRecorder folder | |
drive_recorder_path = "./DriveRecorder" | |
output_path = "./Extracted" | |
os.mkdir(output_path) | |
# Iterate over each subdirectory | |
for subdir in os.listdir(drive_recorder_path): | |
subdir_path = os.path.join(drive_recorder_path, subdir) | |
if not os.path.isdir(subdir_path): | |
continue | |
for file in os.listdir(subdir_path): | |
# Find the video file | |
if file.endswith(".ts"): | |
dir_date_time = subdir.rsplit("_", 1)[0] | |
old_video_path = os.path.join(subdir_path, file) | |
# Rename and move the video file | |
new_video_name = f"BMW_{dir_date_time}" + ".mp4" if CONVERT_TO_MP4_WITH_FFMPEG else ".ts" | |
new_video_path = os.path.join(output_path, new_video_name) | |
if CONVERT_TO_MP4_WITH_FFMPEG: | |
import subprocess | |
subprocess.run(['ffmpeg', '-i', old_video_path, new_video_path]) | |
else: | |
shutil.move(old_video_path, new_video_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment