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

相关推荐
yangshuo12812 小时前
如何将手机的画面和音频全部传输到电脑显示和使用电脑外放输出
智能手机·音视频
陈皮话梅糖@4 小时前
iOS 集成ffmpeg
ios·ffmpeg
芥末的无奈5 小时前
GStreamer 简明教程(九):插件开发,以一个音频特效插件为例
音视频·gstreamer
winxp-pic20 小时前
视频行为分析系统,可做安全行为检测,比如周界入侵,打架
安全·音视频
姓学名生1 天前
李沐vscode配置+github管理+FFmpeg视频搬运+百度API添加翻译字幕
vscode·python·深度学习·ffmpeg·github·视频
学习嵌入式的小羊~1 天前
RV1126+FFMPEG推流项目(11)编码音视频数据 + FFMPEG时间戳处理
ffmpeg·音视频
刘大猫.1 天前
vue3使用音频audio标签
音视频·audio·preload·加载音频文件·vue3使用audio·vue3使用音频·audio标签
优联前端2 天前
Web 音视频(二)在浏览器中解析视频
前端·javascript·音视频·优联前端·webav
我真不会起名字啊2 天前
“深入浅出”系列之音视频开发:(3)音视频开发的学习路线和必备知识
音视频
是店小二呀2 天前
【2024年CSDN平台总结:新生与成长之路】
数据库·人工智能·程序人生·aigc·音视频