Trim MP4 End
Description
The trim_mp4_end
function trims a specified number of seconds from the end of an MP4 video. It uses ffmpeg
to process the video and saves the trimmed result to a new file.
Function Signature:
def trim_mp4_end(file_path: str, clip_length_in_s: int, seconds_to_trim: int, new_file_path: str) -> int:
Parameters
- file_path (str): The path to the original MP4 video file.
- clip_length_in_s (int): The total length of the video in seconds.
- seconds_to_trim (int): The number of seconds to trim from the end of the video.
- new_file_path (str): The path where the trimmed video will be saved.
Returns
- int: The function returns
0
if the trimming is successful, or-1
if an error occurs.
Example Usage
input_file = "/path/to/video.mp4"
output_file = "/path/to/trimmed_video.mp4"
clip_length_in_s = 120 # Total length of the video in seconds
seconds_to_trim = 10 # Trim the last 10 seconds
result = trim_mp4_end(input_file, clip_length_in_s, seconds_to_trim, output_file)
if result == 0:
print("Video trimmed successfully.")
else:
print("Error occurred during video trimming.")
Notes
- The function uses
ffmpeg
with the-t
option to specify the end time for the trimmed video. - The video is copied without re-encoding (
-c copy
) to preserve quality and speed up the process. - The trimming process is done by specifying the end time in milliseconds.
Error Handling
- If any error occurs (e.g., invalid file paths,
ffmpeg
errors), the function will print the error message and return-1
.