simba-uw-tf-dev 4.7.4__py3-none-any.whl → 4.7.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of simba-uw-tf-dev might be problematic. Click here for more details.

simba/sandbox/av1.py ADDED
@@ -0,0 +1,5 @@
1
+ from simba.video_processors.video_processing import convert_to_webm
2
+
3
+
4
+
5
+ convert_to_webm(path=r"D:\troubleshooting\batch_fps\2025-09-08_13-06-38-AloneH-1.mp4", codec='vp9')
@@ -0,0 +1,266 @@
1
+ """
2
+ Function to apply hqdn3d (high-quality denoise 3D) filter to a video file using ffmpeg.
3
+
4
+ The hqdn3d filter is a spatial-temporal denoise filter that reduces noise while preserving
5
+ video quality and details.
6
+ """
7
+
8
+ import os
9
+ import subprocess
10
+ from typing import Union, Optional
11
+
12
+ from simba.utils.checks import (
13
+ check_ffmpeg_available,
14
+ check_file_exist_and_readable,
15
+ check_if_dir_exists,
16
+ check_nvidea_gpu_available
17
+ )
18
+ from simba.utils.errors import FFMPEGCodecGPUError
19
+ from simba.utils.printing import SimbaTimer, stdout_success
20
+ from simba.utils.read_write import get_fn_ext
21
+
22
+
23
+ def denoise_bm3d(file_path: Union[str, os.PathLike],
24
+ save_path: Optional[Union[str, os.PathLike]] = None,
25
+ gpu: Optional[bool] = False,
26
+ quality: int = 60,
27
+ sigma: Optional[float] = None,
28
+ block: Optional[int] = None,
29
+ bstep: Optional[int] = None,
30
+ group: Optional[int] = None) -> None:
31
+ """
32
+ Apply bm3d (Block-Matching 3D) denoise filter to a video file.
33
+
34
+ BM3D is a more advanced denoising algorithm than hqdn3d, often better at removing texture
35
+ and background noise while preserving details. It's slower but produces better results.
36
+
37
+ **For removing background texture (e.g., sawdust pellets):**
38
+ - **sigma** is MOST IMPORTANT - controls noise level/denoising strength (higher = more denoising)
39
+ - **block** - block size (default: 4, larger = more smoothing but slower)
40
+ - **bstep** - block step (default: 4, smaller = better quality but slower)
41
+ - **group** - group size (default: 1, larger = better denoising but slower)
42
+
43
+ :param Union[str, os.PathLike] file_path: Path to input video file.
44
+ :param Optional[Union[str, os.PathLike]] save_path: Optional save location for the denoised video. If None, then the new video is saved in the same directory as the input video with the ``_bm3d_denoised`` suffix.
45
+ :param Optional[bool] gpu: If True, use NVIDEA GPU codecs. Default False.
46
+ :param int quality: Video quality percentage (1-100). Higher values = higher quality. Default 60.
47
+ :param Optional[float] sigma: Noise level/denoising strength (default: 1.0). **MOST IMPORTANT** - higher values = more denoising. For background texture removal, try 5-20.
48
+ :param Optional[int] block: Block size (default: 4). Larger values = more smoothing but slower processing.
49
+ :param Optional[int] bstep: Block step (default: 4). Smaller values = better quality but slower.
50
+ :param Optional[int] group: Group size (default: 1). Larger values = better denoising but slower.
51
+ :returns: None. If save_path is not passed, the result is stored in the same directory as the input file with the ``_bm3d_denoised.mp4`` suffix.
52
+
53
+ .. note::
54
+ Codec is automatically selected: libx264 for CPU encoding (ignored if gpu=True).
55
+ BM3D is slower than hqdn3d but often produces better results for texture removal.
56
+ For background texture removal, start with sigma=10-15 and adjust from there.
57
+
58
+ :example:
59
+ >>> denoise_bm3d(file_path='project_folder/videos/Video_1.avi', sigma=10)
60
+ >>> denoise_bm3d(file_path='/Users/simon/Desktop/test/noisy_video.mp4', sigma=15, block=8, quality=80)
61
+ """
62
+
63
+ check_ffmpeg_available(raise_error=True)
64
+ if gpu and not check_nvidea_gpu_available():
65
+ raise FFMPEGCodecGPUError(
66
+ msg="No GPU found (as evaluated by nvidea-smi returning None)",
67
+ source=denoise_bm3d.__name__
68
+ )
69
+
70
+ timer = SimbaTimer(start=True)
71
+ check_file_exist_and_readable(file_path=file_path)
72
+
73
+ dir, file_name, ext = get_fn_ext(filepath=file_path)
74
+
75
+ if save_path is None:
76
+ save_name = os.path.join(dir, f"{file_name}_bm3d_denoised.mp4")
77
+ else:
78
+ check_if_dir_exists(
79
+ in_dir=os.path.dirname(save_path),
80
+ source=f'{denoise_bm3d.__name__} save_path',
81
+ create_if_not_exist=True
82
+ )
83
+ save_name = save_path
84
+
85
+ # Set default bm3d parameters if not provided
86
+ sigma_val = sigma if sigma is not None else 1.0
87
+ block_val = block if block is not None else 4
88
+ bstep_val = bstep if bstep is not None else 4
89
+ group_val = group if group is not None else 1
90
+
91
+ # Check if bm3d filter is available first (may not be compiled into all ffmpeg builds)
92
+ check_cmd = 'ffmpeg -filters 2>&1 | findstr /i "bm3d"'
93
+ result = subprocess.run(check_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
94
+ if not result.stdout.strip():
95
+ raise FFMPEGCodecGPUError(
96
+ msg="BM3D filter not available in your ffmpeg build. BM3D may not be compiled into your ffmpeg installation. Try using hqdn3d or nlmeans instead.",
97
+ source=denoise_bm3d.__name__
98
+ )
99
+
100
+ # Build bm3d filter string with named parameters
101
+ # Format: bm3d=sigma=value:block=value:bstep=value:group=value
102
+ filter_str = f'bm3d=sigma={sigma_val}:block={block_val}:bstep={bstep_val}:group={group_val}'
103
+
104
+ # Build ffmpeg command with bm3d filter
105
+ if gpu:
106
+ # GPU encoding with bm3d filter
107
+ from simba.utils.lookups import quality_pct_to_crf
108
+ quality_crf = quality_pct_to_crf(pct=int(quality))
109
+ cmd = f'ffmpeg -hwaccel auto -c:v h264_cuvid -i "{file_path}" -vf {filter_str} -rc vbr -cq {quality_crf} -c:v h264_nvenc -c:a copy "{save_name}" -loglevel error -stats -hide_banner -y'
110
+ else:
111
+ # CPU encoding with bm3d filter
112
+ from simba.utils.lookups import quality_pct_to_crf
113
+ quality_crf = quality_pct_to_crf(pct=int(quality))
114
+ cmd = f'ffmpeg -i "{file_path}" -vf {filter_str} -c:v libx264 -crf {quality_crf} -c:a copy "{save_name}" -loglevel error -stats -hide_banner -y'
115
+
116
+ print(f"Applying bm3d denoise filter (sigma={sigma_val}, block={block_val}, bstep={bstep_val}, group={group_val}) to {file_name}...")
117
+ print(f"Command: {cmd}")
118
+
119
+ process = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
120
+ if process.returncode != 0:
121
+ error_msg = process.stderr if process.stderr else (process.stdout if process.stdout else "Unknown error")
122
+ print(f"Error output: {error_msg}")
123
+ raise FFMPEGCodecGPUError(
124
+ msg=f"FFmpeg bm3d filter failed: {error_msg}",
125
+ source=denoise_bm3d.__name__
126
+ )
127
+ timer.stop_timer()
128
+ stdout_success(
129
+ msg=f"SIMBA COMPLETE: Video denoised with BM3D! {save_name} generated!",
130
+ elapsed_time=timer.elapsed_time_str,
131
+ source=denoise_bm3d.__name__
132
+ )
133
+
134
+
135
+ def denoise_hqdn3d(file_path: Union[str, os.PathLike],
136
+ save_path: Optional[Union[str, os.PathLike]] = None,
137
+ gpu: Optional[bool] = False,
138
+ quality: int = 60,
139
+ luma_spatial: Optional[float] = None,
140
+ luma_temporal: Optional[float] = None,
141
+ chroma_spatial: Optional[float] = None,
142
+ chroma_temporal: Optional[float] = None) -> None:
143
+ """
144
+ Apply hqdn3d (high-quality denoise 3D) filter to a video file.
145
+
146
+ The hqdn3d filter has 4 parameters that control denoising strength:
147
+ - Higher values = more denoising but may blur details
148
+ - Lower values = less denoising but preserves more detail
149
+
150
+ **For removing background texture (e.g., sawdust pellets):**
151
+ - **luma_spatial** and **chroma_spatial** are MOST IMPORTANT - these control spatial smoothing within each frame
152
+ - **luma_temporal** and **chroma_temporal** are less critical - these smooth across frames (helpful for motion noise, not static background)
153
+ - For background texture removal, focus on high spatial values (50-200+) and moderate temporal values (10-20)
154
+
155
+ .. note::
156
+ hqdn3d may not be ideal for removing static background texture. Consider background subtraction or blur filters instead.
157
+ For background texture removal, try: luma_spatial=100-200, chroma_spatial=100-200, luma_temporal=10-20, chroma_temporal=10-20
158
+
159
+ :param Union[str, os.PathLike] file_path: Path to input video file.
160
+ :param Optional[Union[str, os.PathLike]] save_path: Optional save location for the denoised video. If None, then the new video is saved in the same directory as the input video with the ``_denoised`` suffix.
161
+ :param Optional[bool] gpu: If True, use NVIDEA GPU codecs. Default False.
162
+ :param int quality: Video quality percentage (1-100). Higher values = higher quality. Default 60.
163
+ :param Optional[float] luma_spatial: Spatial luma strength (default: 4.0). Controls detail preservation in luma channel. **MOST IMPORTANT for background texture removal.**
164
+ :param Optional[float] luma_temporal: Temporal luma strength (default: 3.0). Controls motion blur artifacts in luma channel. Less critical for static background.
165
+ :param Optional[float] chroma_spatial: Spatial chroma strength (default: 3.0). Controls detail preservation in chroma channel. **MOST IMPORTANT for background texture removal.**
166
+ :param Optional[float] chroma_temporal: Temporal chroma strength (default: 6.0). Controls motion blur artifacts in chroma channel. Less critical for static background.
167
+ :returns: None. If save_path is not passed, the result is stored in the same directory as the input file with the ``_denoised.mp4`` suffix.
168
+
169
+ .. note::
170
+ Codec is automatically selected: libx264 for CPU encoding (ignored if gpu=True).
171
+ Default parameters are conservative. For stronger denoising, try: luma_spatial=8, luma_temporal=6, chroma_spatial=6, chroma_temporal=9
172
+ For very noisy videos, you may need values of 10-15 or higher.
173
+
174
+ :example:
175
+ >>> denoise_hqdn3d(file_path='project_folder/videos/Video_1.avi')
176
+ >>> denoise_hqdn3d(file_path='/Users/simon/Desktop/test/noisy_video.mp4', luma_spatial=8, luma_temporal=6, quality=80)
177
+ >>> # For removing background texture (sawdust pellets):
178
+ >>> denoise_hqdn3d(file_path='video.mp4', luma_spatial=150, chroma_spatial=150, luma_temporal=15, chroma_temporal=15)
179
+ """
180
+
181
+ check_ffmpeg_available(raise_error=True)
182
+ if gpu and not check_nvidea_gpu_available():
183
+ raise FFMPEGCodecGPUError(
184
+ msg="No GPU found (as evaluated by nvidea-smi returning None)",
185
+ source=denoise_hqdn3d.__name__
186
+ )
187
+
188
+ timer = SimbaTimer(start=True)
189
+ check_file_exist_and_readable(file_path=file_path)
190
+
191
+ dir, file_name, ext = get_fn_ext(filepath=file_path)
192
+
193
+ if save_path is None:
194
+ save_name = os.path.join(dir, f"{file_name}_denoised.mp4")
195
+ else:
196
+ check_if_dir_exists(
197
+ in_dir=os.path.dirname(save_path),
198
+ source=f'{denoise_hqdn3d.__name__} save_path',
199
+ create_if_not_exist=True
200
+ )
201
+ save_name = save_path
202
+
203
+ # Set default hqdn3d parameters if not provided
204
+ luma_sp = luma_spatial if luma_spatial is not None else 4.0
205
+ luma_tmp = luma_temporal if luma_temporal is not None else 3.0
206
+ chroma_sp = chroma_spatial if chroma_spatial is not None else 3.0
207
+ chroma_tmp = chroma_temporal if chroma_temporal is not None else 6.0
208
+
209
+ # Build hqdn3d filter string with parameters
210
+ # Format: hqdn3d=luma_spatial:chroma_spatial:luma_temporal:chroma_temporal
211
+ filter_str = f'hqdn3d={luma_sp}:{chroma_sp}:{luma_tmp}:{chroma_tmp}'
212
+
213
+ # Build ffmpeg command with hqdn3d filter
214
+ if gpu:
215
+ # GPU encoding with hqdn3d filter
216
+ from simba.utils.lookups import quality_pct_to_crf
217
+ quality_crf = quality_pct_to_crf(pct=int(quality))
218
+ cmd = f'ffmpeg -hwaccel auto -c:v h264_cuvid -i "{file_path}" -vf {filter_str} -rc vbr -cq {quality_crf} -c:v h264_nvenc -c:a copy "{save_name}" -loglevel error -stats -hide_banner -y'
219
+ else:
220
+ # CPU encoding with hqdn3d filter
221
+ from simba.utils.lookups import quality_pct_to_crf
222
+ quality_crf = quality_pct_to_crf(pct=int(quality))
223
+ cmd = f'ffmpeg -i "{file_path}" -vf {filter_str} -c:v libx264 -crf {quality_crf} -c:a copy "{save_name}" -loglevel error -stats -hide_banner -y'
224
+
225
+ print(f"Applying hqdn3d denoise filter (luma_spatial={luma_sp}, chroma_spatial={chroma_sp}, luma_temporal={luma_tmp}, chroma_temporal={chroma_tmp}) to {file_name}...")
226
+
227
+ subprocess.call(cmd, shell=True, stdout=subprocess.PIPE)
228
+ timer.stop_timer()
229
+ stdout_success(
230
+ msg=f"SIMBA COMPLETE: Video denoised! {save_name} generated!",
231
+ elapsed_time=timer.elapsed_time_str,
232
+ source=denoise_hqdn3d.__name__
233
+ )
234
+
235
+ #
236
+ # For removing background texture (sawdust pellets):
237
+ # Focus on HIGH spatial values (most important) and moderate temporal values
238
+ # Spatial parameters blur within each frame - critical for static background texture
239
+ # Temporal parameters smooth across frames - less critical for static background
240
+ # denoise_hqdn3d(
241
+ # file_path=r"E:\open_video\open_field_4\2.mp4",
242
+ # luma_spatial=200, # VERY HIGH - most important for background texture removal
243
+ # luma_temporal=40, # Moderate - 200 is overkill for static background
244
+ # chroma_spatial=200, # VERY HIGH - most important for background texture removal
245
+ # chroma_temporal=40 # Moderate - 200 is overkill for static background
246
+ # )
247
+
248
+ # BM3D - Better for texture removal, slower but often produces better results
249
+ # sigma is the most important parameter - controls denoising strength
250
+ # denoise_bm3d(
251
+ # file_path=r"E:\open_video\open_field_4\2.mp4",
252
+ # sigma=15, # High denoising strength for background texture removal
253
+ # block=8, # Larger blocks = more smoothing
254
+ # bstep=2, # Smaller step = better quality
255
+ # group=1 # Default group size
256
+ # )
257
+ #
258
+ # # BM3D - Better for texture removal, slower but often produces better results
259
+ # # sigma is the most important parameter - controls denoising strength
260
+ # denoise_bm3d(
261
+ # file_path=r"E:\open_video\open_field_4\2.mp4",
262
+ # sigma=15, # High denoising strength for background texture removal
263
+ # block=8, # Larger blocks = more smoothing
264
+ # bstep=2, # Smaller step = better quality
265
+ # group=1 # Default group size
266
+ # )
@@ -0,0 +1,126 @@
1
+ """
2
+ Function to extract random N frames from all videos in a directory.
3
+
4
+ Each frame is saved as videoname_framenumber.png in the same directory as the video.
5
+ """
6
+
7
+ import os
8
+ import random
9
+ from typing import Union, Optional
10
+ import numpy as np
11
+ import cv2
12
+
13
+ from simba.utils.checks import (
14
+ check_if_dir_exists,
15
+ check_int,
16
+ check_file_exist_and_readable
17
+ )
18
+ from simba.utils.read_write import (
19
+ get_video_meta_data,
20
+ find_all_videos_in_directory,
21
+ read_frm_of_video,
22
+ get_fn_ext
23
+ )
24
+ from simba.utils.printing import SimbaTimer, stdout_success
25
+
26
+
27
+ def extract_random_frames_from_directory(
28
+ directory: Union[str, os.PathLike],
29
+ n_frames: int = 10,
30
+ save_dir: Optional[Union[str, os.PathLike]] = None,
31
+ verbose: Optional[bool] = True
32
+ ) -> None:
33
+ """
34
+ Extract random N frames from all videos in a directory.
35
+
36
+ For each video, randomly samples N frames and saves them as individual PNG files.
37
+ Frames are saved with the naming convention: videoname_framenumber.png
38
+
39
+ :param Union[str, os.PathLike] directory: Path to directory containing video files.
40
+ :param int n_frames: Number of random frames to extract from each video. Default: 10.
41
+ :param Optional[Union[str, os.PathLike]] save_dir: Optional directory where extracted frames will be saved.
42
+ If None, frames are saved in the same directory as each video file. Default: None.
43
+ :param Optional[bool] verbose: If True, prints progress messages during extraction. Default: True.
44
+ :return: None. Frames are saved to disk.
45
+
46
+ :example:
47
+ >>> extract_random_frames_from_directory(directory='project_folder/videos', n_frames=20)
48
+ >>> extract_random_frames_from_directory(directory='/Users/simon/Desktop/videos', n_frames=5, save_dir='/Users/simon/Desktop/frames')
49
+ """
50
+
51
+ timer = SimbaTimer(start=True)
52
+ check_if_dir_exists(in_dir=directory, source=extract_random_frames_from_directory.__name__)
53
+ check_int(name="n_frames", value=n_frames, min_value=1)
54
+
55
+ # Find all videos in directory
56
+ video_paths = find_all_videos_in_directory(
57
+ directory=directory,
58
+ as_dict=False,
59
+ raise_error=True
60
+ )
61
+
62
+ if not video_paths or video_paths == ["No videos found"]:
63
+ raise ValueError(f"No videos found in directory: {directory}")
64
+
65
+ total_frames_extracted = 0
66
+
67
+ for video_cnt, video_name in enumerate(video_paths):
68
+ video_path = os.path.join(directory, video_name)
69
+ check_file_exist_and_readable(file_path=video_path)
70
+
71
+ # Get video metadata
72
+ video_meta_data = get_video_meta_data(video_path=video_path)
73
+ total_frames = video_meta_data["frame_count"]
74
+ _, video_name_only, _ = get_fn_ext(filepath=video_path) # Returns (directory, filename, extension)
75
+
76
+ # Determine save directory
77
+ if save_dir is None:
78
+ video_save_dir = directory # Save to the same directory as the videos
79
+ else:
80
+ video_save_dir = save_dir
81
+
82
+ # Create save directory if it doesn't exist
83
+ if not os.path.exists(video_save_dir):
84
+ os.makedirs(video_save_dir)
85
+
86
+ # Randomly sample N frames (or all frames if video has fewer than N frames)
87
+ n_samples = min(n_frames, total_frames)
88
+ if total_frames < n_frames:
89
+ if verbose:
90
+ print(f"Video {video_name_only} has only {total_frames} frames. Extracting all {total_frames} frames.")
91
+ selected_frames = list(range(total_frames))
92
+ else:
93
+ selected_frames = sorted(random.sample(range(total_frames), n_samples))
94
+
95
+ # Extract and save frames
96
+ cap = cv2.VideoCapture(video_path)
97
+ for frame_idx, frame_number in enumerate(selected_frames):
98
+ # Seek to the correct frame
99
+ cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
100
+ ret, frame = cap.read()
101
+
102
+ if not ret:
103
+ if verbose:
104
+ print(f"Warning: Could not read frame {frame_number} from {video_name_only}. Skipping...")
105
+ continue
106
+
107
+ # Save frame with naming convention: videoname_framenumber.png
108
+ # video_name_only is the video filename without extension
109
+ save_path = os.path.join(video_save_dir, f"{video_name_only}_{frame_number}.png")
110
+ cv2.imwrite(save_path, frame, [cv2.IMWRITE_PNG_COMPRESSION, 3])
111
+ total_frames_extracted += 1
112
+
113
+ if verbose:
114
+ print(f"Video {video_cnt + 1}/{len(video_paths)}: Frame {frame_number} saved from {video_name_only} ({frame_idx + 1}/{len(selected_frames)})")
115
+
116
+ cap.release()
117
+
118
+ timer.stop_timer()
119
+ stdout_success(
120
+ msg=f"SIMBA COMPLETE: Extracted {total_frames_extracted} random frames from {len(video_paths)} video(s)!",
121
+ elapsed_time=timer.elapsed_time_str,
122
+ source=extract_random_frames_from_directory.__name__
123
+ )
124
+
125
+
126
+ extract_random_frames_from_directory(directory=r"D:\maplight_tg2576_yolo\videos", n_frames=35, save_dir=r'D:\maplight_tg2576_yolo\frames')
@@ -1,80 +1,80 @@
1
- """
2
- Function to remove N seconds from the end of a video file.
3
-
4
- Similar to simba.video_processors.video_processing.remove_beginning_of_video
5
- but removes from the end instead of the beginning.
6
- """
7
-
8
- import os
9
- import subprocess
10
- from typing import Union, Optional
11
-
12
- from simba.utils.checks import (
13
- check_ffmpeg_available,
14
- check_file_exist_and_readable,
15
- check_int,
16
- check_if_dir_exists,
17
- check_nvidea_gpu_available
18
- )
19
- from simba.utils.errors import FFMPEGCodecGPUError, InvalidInputError
20
- from simba.utils.printing import SimbaTimer, stdout_success
21
- from simba.utils.read_write import get_fn_ext, get_video_meta_data
22
- from simba.utils.lookups import quality_pct_to_crf
23
-
24
-
25
- def remove_end_of_video(file_path: Union[str, os.PathLike],
26
- time: int,
27
- quality: int = 60,
28
- save_path: Optional[Union[str, os.PathLike]] = None,
29
- gpu: Optional[bool] = False) -> None:
30
- """
31
- Remove N seconds from the end of a video file.
32
-
33
- :param Union[str, os.PathLike] file_path: Path to video file
34
- :param int time: Number of seconds to remove from the end of the video.
35
- :param int quality: Video quality percentage (1-100). Higher values = higher quality. Default 60.
36
- :param Optional[Union[str, os.PathLike]] save_path: Optional save location for the shortened video. If None, then the new video is saved in the same directory as the input video with the ``_shortened`` suffix.
37
- :param Optional[bool] gpu: If True, use NVIDEA GPU codecs. Default False.
38
- :returns: None. If save_path is not passed, the result is stored in the same directory as the input file with the ``_shorten.mp4`` suffix.
39
-
40
- .. note::
41
- Codec is automatically selected: libx264 for CPU encoding (ignored if gpu=True).
42
-
43
- :example:
44
- >>> _ = remove_end_of_video(file_path='project_folder/videos/Video_1.avi', time=10)
45
- >>> remove_end_of_video(file_path=f'/Users/simon/Desktop/imgs_4/test/blahhhh.mp4', save_path='/Users/simon/Desktop/imgs_4/test/CUT.mp4', time=3)
46
- """
47
-
48
- check_ffmpeg_available(raise_error=True)
49
- if gpu and not check_nvidea_gpu_available():
50
- raise FFMPEGCodecGPUError(msg="No GPU found (as evaluated by nvidea-smi returning None)", source=remove_end_of_video.__name__)
51
- timer = SimbaTimer(start=True)
52
- check_file_exist_and_readable(file_path=file_path)
53
- video_meta_data = get_video_meta_data(video_path=file_path)
54
- check_int(name="Cut time", value=time, min_value=1)
55
- check_int(name=f'{remove_end_of_video.__name__} quality', value=quality, min_value=1, max_value=100, raise_error=True)
56
- quality_crf = quality_pct_to_crf(pct=int(quality))
57
- time = int(time)
58
- dir, file_name, ext = get_fn_ext(filepath=file_path)
59
- if video_meta_data['video_length_s'] <= time:
60
- raise InvalidInputError(msg=f"The cut time {time}s is invalid for video {file_name} with length {video_meta_data['video_length_s']}s", source=remove_end_of_video.__name__)
61
- if save_path is None:
62
- save_name = os.path.join(dir, f"{file_name}_shorten.mp4")
63
- else:
64
- check_if_dir_exists(in_dir=os.path.dirname(save_path), source=f'{remove_end_of_video.__name__} save_path', create_if_not_exist=True)
65
- save_name = save_path
66
- duration = video_meta_data['video_length_s'] - time
67
- if gpu:
68
- cmd = f'ffmpeg -hwaccel auto -c:v h264_cuvid -i "{file_path}" -t {duration} -rc vbr -cq {quality_crf} -c:v h264_nvenc -c:a aac "{save_name}" -loglevel error -stats -hide_banner -y'
69
- else:
70
- cmd = f'ffmpeg -i "{file_path}" -t {duration} -c:v libx264 -crf {quality_crf} -c:a aac "{save_name}" -loglevel error -stats -hide_banner -y'
71
- print(f"Removing final {time}s from {file_name}... ")
72
- subprocess.call(cmd, shell=True, stdout=subprocess.PIPE)
73
- timer.stop_timer()
74
- stdout_success(msg=f"SIMBA COMPLETE: Video converted! {save_name} generated!", elapsed_time=timer.elapsed_time_str, source=remove_end_of_video.__name__)
75
-
76
-
77
- if __name__ == "__main__":
78
- # Example usage
79
- # remove_end_of_video(file_path='path/to/video.mp4', time=10)
80
- pass
1
+ """
2
+ Function to remove N seconds from the end of a video file.
3
+
4
+ Similar to simba.video_processors.video_processing.remove_beginning_of_video
5
+ but removes from the end instead of the beginning.
6
+ """
7
+
8
+ import os
9
+ import subprocess
10
+ from typing import Union, Optional
11
+
12
+ from simba.utils.checks import (
13
+ check_ffmpeg_available,
14
+ check_file_exist_and_readable,
15
+ check_int,
16
+ check_if_dir_exists,
17
+ check_nvidea_gpu_available
18
+ )
19
+ from simba.utils.errors import FFMPEGCodecGPUError, InvalidInputError
20
+ from simba.utils.printing import SimbaTimer, stdout_success
21
+ from simba.utils.read_write import get_fn_ext, get_video_meta_data
22
+ from simba.utils.lookups import quality_pct_to_crf
23
+
24
+
25
+ def remove_end_of_video(file_path: Union[str, os.PathLike],
26
+ time: int,
27
+ quality: int = 60,
28
+ save_path: Optional[Union[str, os.PathLike]] = None,
29
+ gpu: Optional[bool] = False) -> None:
30
+ """
31
+ Remove N seconds from the end of a video file.
32
+
33
+ :param Union[str, os.PathLike] file_path: Path to video file
34
+ :param int time: Number of seconds to remove from the end of the video.
35
+ :param int quality: Video quality percentage (1-100). Higher values = higher quality. Default 60.
36
+ :param Optional[Union[str, os.PathLike]] save_path: Optional save location for the shortened video. If None, then the new video is saved in the same directory as the input video with the ``_shortened`` suffix.
37
+ :param Optional[bool] gpu: If True, use NVIDEA GPU codecs. Default False.
38
+ :returns: None. If save_path is not passed, the result is stored in the same directory as the input file with the ``_shorten.mp4`` suffix.
39
+
40
+ .. note::
41
+ Codec is automatically selected: libx264 for CPU encoding (ignored if gpu=True).
42
+
43
+ :example:
44
+ >>> _ = remove_end_of_video(file_path='project_folder/videos/Video_1.avi', time=10)
45
+ >>> remove_end_of_video(file_path=f'/Users/simon/Desktop/imgs_4/test/blahhhh.mp4', save_path='/Users/simon/Desktop/imgs_4/test/CUT.mp4', time=3)
46
+ """
47
+
48
+ check_ffmpeg_available(raise_error=True)
49
+ if gpu and not check_nvidea_gpu_available():
50
+ raise FFMPEGCodecGPUError(msg="No GPU found (as evaluated by nvidea-smi returning None)", source=remove_end_of_video.__name__)
51
+ timer = SimbaTimer(start=True)
52
+ check_file_exist_and_readable(file_path=file_path)
53
+ video_meta_data = get_video_meta_data(video_path=file_path)
54
+ check_int(name="Cut time", value=time, min_value=1)
55
+ check_int(name=f'{remove_end_of_video.__name__} quality', value=quality, min_value=1, max_value=100, raise_error=True)
56
+ quality_crf = quality_pct_to_crf(pct=int(quality))
57
+ time = int(time)
58
+ dir, file_name, ext = get_fn_ext(filepath=file_path)
59
+ if video_meta_data['video_length_s'] <= time:
60
+ raise InvalidInputError(msg=f"The cut time {time}s is invalid for video {file_name} with length {video_meta_data['video_length_s']}s", source=remove_end_of_video.__name__)
61
+ if save_path is None:
62
+ save_name = os.path.join(dir, f"{file_name}_shorten.mp4")
63
+ else:
64
+ check_if_dir_exists(in_dir=os.path.dirname(save_path), source=f'{remove_end_of_video.__name__} save_path', create_if_not_exist=True)
65
+ save_name = save_path
66
+ duration = video_meta_data['video_length_s'] - time
67
+ if gpu:
68
+ cmd = f'ffmpeg -hwaccel auto -c:v h264_cuvid -i "{file_path}" -t {duration} -rc vbr -cq {quality_crf} -c:v h264_nvenc -c:a aac "{save_name}" -loglevel error -stats -hide_banner -y'
69
+ else:
70
+ cmd = f'ffmpeg -i "{file_path}" -t {duration} -c:v libx264 -crf {quality_crf} -c:a aac "{save_name}" -loglevel error -stats -hide_banner -y'
71
+ print(f"Removing final {time}s from {file_name}... ")
72
+ subprocess.call(cmd, shell=True, stdout=subprocess.PIPE)
73
+ timer.stop_timer()
74
+ stdout_success(msg=f"SIMBA COMPLETE: Video converted! {save_name} generated!", elapsed_time=timer.elapsed_time_str, source=remove_end_of_video.__name__)
75
+
76
+
77
+ if __name__ == "__main__":
78
+ # Example usage
79
+ # remove_end_of_video(file_path='path/to/video.mp4', time=10)
80
+ pass