Concatenate Videos
Description
The concatenate_videos
function concatenates two video files into a single output video. It uses a temporary text file to list the input video files, then runs the ffmpeg
command to combine them.
Function Signature:
def concatenate_videos(input_file_path1: str, input_file_path2: str, output_file_path: str):
Parameters
- input_file_path1 (
str
): The file path of the first video to concatenate. - input_file_path2 (
str
): The file path of the second video to concatenate. - output_file_path (
str
): The file path where the concatenated video will be saved.
Returns
- None
Example Usage
concatenate_videos('video1.mp4', 'video2.mp4', 'output_video.mp4')
Notes
- The function generates a temporary text file containing the paths of the videos to concatenate.
- It uses
ffmpeg
to perform the concatenation operation with the-f concat
and-safe 0
options. - After the operation, it removes the temporary file used to list the video files.
- If an error occurs during concatenation or while cleaning up the temporary file, it is handled and printed to the console.
Error Handling
- If an error occurs during the
ffmpeg
command execution, asubprocess.CalledProcessError
is caught and an error message is printed. - If there is an issue removing the temporary file, an
OSError
is caught and printed.