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 小时前
WebRTC中的几个Rtp*Sender
网络·网络协议·音视频·webrtc·媒体
EasyDSS4 小时前
EasyRTC嵌入式音视频通信SDK助力物联网/视频物联网音视频打造全场景应用
人工智能·音视频
aqi004 小时前
FFmpeg开发笔记(六十三)FFmpeg使用vvenc把视频转为H.266编码
ffmpeg·音视频·直播·流媒体
babytiger6 小时前
rk3588 上运行smolvlm-realtime-webcam,将视频转为文字描述
音视频
Icoolkj6 小时前
Komiko 视频到视频功能炸裂上线!
人工智能·音视频
sukalot7 小时前
window 显示驱动开发-DirectX 视频加速 2.0
音视频
冷凌爱7 小时前
每天总结一个html标签——Audio音频标签
前端·html·音视频
思通数科多模态大模型10 小时前
重构城市应急指挥布控策略 ——无人机智能视频监控的破局之道
人工智能·深度学习·安全·重构·数据挖掘·音视频·无人机
武子康20 小时前
AI炼丹日志-28 - Audiblez 将你的电子书epub转换为音频mp3 做有声书
人工智能·爬虫·gpt·算法·机器学习·ai·音视频
gomogomono1 天前
【面试】音视频面试
音视频