乐闻世界logo
搜索文章和话题

FFmpeg : How to split video efficiently?

1个答案

1

FFmpeg is a powerful tool used for handling multimedia files, such as transcoding and splitting videos. There are several common methods to split videos using FFmpeg:

1. Splitting Videos Using -ss and -t Parameters

This is the most common method for splitting videos. The -ss parameter specifies the start time for cutting, while the -t parameter specifies the duration from the start time. For example, to extract a 30-second segment starting from the 10th second of a video, you can use the following command:

bash
ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:30 -c copy output.mp4

Here, -c copy indicates using copy stream mode to avoid re-encoding, which allows for faster processing without quality loss.

2. Using -to Parameter to Specify End Time

Unlike -t, the -to parameter directly specifies the end time for extraction rather than the duration. For instance, to extract from the 10th second to the 40th second, you can use:

bash
ffmpeg -i input.mp4 -ss 00:00:10 -to 00:00:40 -c copy output.mp4

3. Splitting into Multiple Files

If you need to split a video into multiple smaller segments, you can use a simple script to loop through FFmpeg commands. For example, to split every 30 seconds into a new video:

bash
#!/bin/bash input="input.mp4" duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$input") clip_length=30 start_time=0 while [ $(bc <<< "$start_time < $duration") -eq 1 ]; do ffmpeg -i "$input" -ss $(date -u -d @$start_time +%H:%M:%S) -t $clip_length -c copy "output_$start_time.mp4" start_time=$(($start_time + $clip_length)) done

4. Smart Splitting Using Scene Detection

FFmpeg can be combined with ffprobe for scene change detection, allowing for more natural video splitting based on visual changes. This method avoids cutting in the middle of scene transitions.

bash
ffmpeg -i input.mp4 -filter_complex "select='gt(scene,0.4)',metadata=print:file=scene.txt" -vsync vfr img%03d.png

The above command outputs segments with significant scene changes as images and prints relevant information to the scene.txt file. You can then split the video based on the timestamps in this file.

Conclusion:

The choice of method depends on your specific requirements, such as whether you need precise time control or consider encoding efficiency. When processing videos with FFmpeg, selecting parameters appropriately can significantly improve processing efficiency and output quality.

2024年6月29日 12:07 回复

你的答案