Skip to content

Commit

Permalink
video tools
Browse files Browse the repository at this point in the history
  • Loading branch information
sronilsson committed May 15, 2024
1 parent 438f3ff commit e12aabb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Binary file added docs/_static/img/video_to_bw.webm
Binary file not shown.
5 changes: 3 additions & 2 deletions simba/ui/pop_ups/video_processing_pop_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -2081,15 +2081,15 @@ def __init__(self):
def run_video(self):
video_path = self.selected_video.file_path
check_file_exist_and_readable(file_path=video_path)
self.brightness, self.contrast = brightness_contrast_ui(video_path=video_path)
self.brightness, self.contrast = brightness_contrast_ui(data=video_path)
self.video_paths = [video_path]
self.apply()

def run_directory(self):
video_dir = self.selected_dir.folder_path
check_if_dir_exists(in_dir=video_dir, source=self.__class__.__name__)
self.video_paths = find_files_of_filetypes_in_directory(directory=video_dir, extensions=Options.ALL_VIDEO_FORMAT_OPTIONS.value, raise_error=True)
self.brightness, self.contrast = brightness_contrast_ui(video_path=self.video_paths[0])
self.brightness, self.contrast = brightness_contrast_ui(data=self.video_paths[0])
self.apply()

def apply(self):
Expand All @@ -2106,6 +2106,7 @@ def apply(self):
timer.stop_timer()
stdout_success(f'{len(self.video_paths)} video(s) converted.', elapsed_time=timer.elapsed_time_str)

#BrightnessContrastPopUp()

class InteractiveClahePopUp(PopUpMixin):
"""
Expand Down
29 changes: 29 additions & 0 deletions simba/video_processors/video_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3445,6 +3445,35 @@ def superimpose_elapsed_time(video_path: Union[str, os.PathLike],
timer.stop_timer()
stdout_success(msg=f'Super-imposed time on {len(video_paths)} video(s), saved in {save_dir}', elapsed_time=timer.elapsed_time_str)


def video_to_bw(video_path: Union[str, os.PathLike],
threshold: Optional[float] = 0.5) -> None:
"""
Convert video to black and white using passed threshold.
.. video:: _static/img/video_to_bw.webm
:loop:
:param Union[str, os.PathLike] video_path: Path to the video
:param Optional[float] threshold: Value between 0 and 1. Lower values gives more white and vice versa.
:return: None.
:example:
>>> video_to_bw(video_path='/Users/simon/Downloads/1_LH_clipped_cropped_eq_20240515135926.mp4', threshold=0.02)
"""

check_float(name=f'{video_to_bw.__name__} threshold', value=threshold, min_value=0, max_value=1.0)
threshold = int(255 * threshold)
check_ffmpeg_available(raise_error=True)
timer = SimbaTimer(start=True)
_ = get_video_meta_data(video_path=video_path)
dir, video_name, ext = get_fn_ext(video_path)
save_path = os.path.join(dir, f'{video_name}_bw{ext}')
cmd = f"ffmpeg -i '{video_path}' -vf \"format=gray,geq=lum_expr='if(lt(lum(X,Y),{threshold}),0,255)'\" -pix_fmt yuv420p '{save_path}' -y"
subprocess.call(cmd, shell=True, stdout=subprocess.PIPE)
timer.stop_timer()
stdout_success(msg=f'Video {video_name} converted.', elapsed_time=timer.elapsed_time_str)

# video_paths = ['/Users/simon/Desktop/envs/simba/troubleshooting/beepboop174/project_folder/merge/Trial 10_clipped_gantt.mp4',
# '/Users/simon/Desktop/envs/simba/troubleshooting/beepboop174/project_folder/merge/Trial 10_clipped.mp4',
# '/Users/simon/Desktop/envs/simba/troubleshooting/beepboop174/project_folder/merge/Trial 10_clipped_line.mp4',
Expand Down

0 comments on commit e12aabb

Please sign in to comment.