ffmpeg.exe 命令使用

  1. 视频分片:裁剪分割视频成小片段,

ffmpeg Documentation

Seeking -- FFmpeg

1.指定持续时间

使用-t命令。前者要比后者快。

ffmpeg -ss [start] -i [input] -t [duration] -c copy [output]

ffmpeg -i [input] -ss [start] -t [duration] -c copy [output]

2.指定结束时间

使用-to命令。外网资料说可以传入-copyts来保持-ss的读取位置,但是我没成功。

ffmpeg -i [input] -ss [start] -to [end] -c copy [output]

  1. 视频切片1:根据关键帧将视频分割成多段,每隔60秒裁剪为一个文件

ffmpeg.exe -i input.mp4 -c copy -map 0 -segment_time 00:00:60-f segment output%03d.mp4

ffmpeg.exe -i input.mp4 -c copy -map 0 -segment_time 60 -f segment -reset_timestamps1 cut%03d.mp4

ffmpeg -i "url" -f segment -segment_time 60 -segment_format mp4 -strftime 1 out%Y-%m-%d_%H-%M-%S.mp4

-reset_timestamps1:每个分片从0开始,否则分片在前一片基础上开始,VLC播放时在前面分片累积时长基础上开播播放

-segment_time60或00:00:60表示分片时长,time unit formats: sexagesimal (HOURS:MM:SS.MILLISECONDS, as in 01:23:45.678), or in seconds.

-map[-]input_file_id[:stream_specifier][?] | [linklabel] :有-表示移除,第input_file_id个输入文件的视频流/音频流/字幕流的第n个流

  1. 以下3种顺序输出结果相同:每个分片60秒

ffmpeg -ss $startTime -i input.mp4 -t 60 -c copy -avoid_negative_ts 1 cut%3d.mp4

ffmpeg -i ./input.mp4 -ss startTime -**to**endTime -c copy -avoid_negative_ts 1 $i.mp4

ffmpeg -ss startTime -**to** endTime -i./input.mp4 -c copy -avoid_negative_ts 1 $i.mp4

如果 -i在-ss -i -to中间,则输出结果不同

  1. 合并视频

ffmpeg.exe -f concat -i "list.txt" -c copy output.mp4

Seeking -- FFmpeg

Cutting small sections

To extract only a small segment in the middle of a movie, it can be used in combination with -t which specifies the duration, like -ss 60 -t 10 to capture from second 60 to 70. Or you can use the -to option to specify an out point, like -ss 60 -to 70 to capture from second 60 to 70. -t and -to are mutually exclusive. If you use both, -t will be used.

Note that if you specify -ss before -i only, the timestamps will be reset to zero, so -t and -to will have the same effect. If you want to keep the original timestamps, add the -copyts option.

The first command will cut from 00:01:00 to 00:03:00 (in the original), using the faster seek.

The second command will cut from 00:01:00 to 00:02:00, as intended, using the slower seek.

The third command will cut from 00:01:00 to 00:02:00, as intended, using the faster seek.

ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy cut.mp4 //-ss在-i前

ffmpeg -i video.mp4 -ss 00:01:00 -to 00:02:00 -c copy cut.mp4 //-ss在-i后

ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy -copytscut.mp4 //-ss在-i前,保持原时间戳

If you cut with stream copy (-c copy) you need to use the -avoid_negative_ts 1 option if you want to use that segment with the concat demuxer .

Example:

ffmpeg -ss 00:03:00 -i video.mp4 -t 60 -c copy -avoid_negative_ts1 cut.mp4

If you have to re-encode anyway, e.g., to apply filters like afade, which can be very slow, make sure to use, e.g., -ss 120 -i some.mov -to 60 to get one minute from 120s to 120+60s, not -to 180 for three minutes starting at 120s.

Time unit syntax

Note that you can use two different time unit formats: sexagesimal (HOURS:MM:SS.MILLISECONDS, as in 01:23:45.678), or in seconds. If a fraction is used, such as 02:30.05, this is interpreted as "5 100ths of a second", not as frame 5. For instance, 02:30.5 would be 2 minutes, 30 seconds, and a half a second, which would be the same as using 150.5 in seconds.

Seeking while doing a codec copy

**Using -ss as input option together with -c:v copy might not be accurate since ffmpeg is forced to only use/split on i-frames.**Though it will---if possible---adjust the start time of the stream to a negative value to compensate for that. Basically, if you specify "second 157" and there is no key frame until second 159, it will include two seconds of audio (with no video) at the start, then will start from the first key frame. So be careful when splitting and doing codec copy.

#脚本video_cut.sh

#!/bin/bash

startTime=0 #开始时间

endTime=0 #结束时间

length=60 #视频长度

i=0

while [ endTime -le length ]; do

#statements

i=\[i+1]

endTime=\[startTime+60] #分段间隔时间

ffmpeg -i ./input.mp4 -ss startTime **-to** endTime -acodec copy -vcodec copy $i.mp4 #每个分片60秒,时长与前面分片无关

startTime=$[endTime]

done

mp4转m3u8,5秒一个ts分片

ffmpeg.exe -i sp.mp4 -map 0 -segment_time 5 -f segment -segment_list "output/sp/sp.m3u8" -r 30 -b:v 5000k -s 1080x1920 -c:a copy "output/sp/sp%03d.ts" -y -hide_banner

mp4转m3u8,5秒一个ts分片

./ffmpeg -i sp.mp4 -c:v libx264 -crf 20 -g 30 -b:v 5000k -codec:a aac -strict experimental -f ssegment -segment_list output/sp.m3u8 -segment_time 5 -c:v libx264 -c:a aac -b:a 128k output/sp%03d.ts

相关推荐
清月电子1 小时前
KT148A语音芯片发码很难播放_将4脚对地一下再发正常,什么原因?
单片机·嵌入式硬件·物联网·音视频
路溪非溪8 小时前
关于ffmpeg的简介和使用总结
ffmpeg
gushansanren8 小时前
基于WSL用MSVC编译ffmpeg7.1
windows·ffmpeg
科技小E9 小时前
WebRTC实时音视频通话技术EasyRTC嵌入式音视频通信SDK,助力智慧物流打造实时高效的物流管理体系
人工智能·音视频
Icoolkj13 小时前
可灵 AI:开启 AI 视频创作新时代
人工智能·音视频
SuperW1 天前
视频编码原理讲解一:VCL层和NAL层的讲解
音视频
Panesle1 天前
HunyuanCustom:文生视频框架论文速读
人工智能·算法·音视频·文生视频
程序员JerrySUN2 天前
驱动开发硬核特训 · Day 30(下篇): 深入解析 lm48100q I2C 音频编解码器驱动模型(基于 i.MX8MP)
linux·驱动开发·架构·音视频
读心悦2 天前
5000字总结 HTML5 中的音频和视频,关羽标签、属性、API 和最佳实践
前端·音视频·html5
东风西巷2 天前
BLURRR剪辑软件免费版:创意剪辑,轻松上手,打造个性视频
android·智能手机·音视频·生活·软件需求