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

相关推荐
用户96715113916726 小时前
Rust 如何轻松实现 RTMP 流媒体推送?深入解析直播推流场景与解决方案
rust·ffmpeg
小小码农Come on6 小时前
ffmpeg命令整理
ffmpeg
暮云星影7 小时前
三、FFmpeg学习笔记
linux·ffmpeg
逼子格11 小时前
五种音频器件综合对比——《器件手册--音频器件》
嵌入式硬件·音视频·硬件工程师·硬件测试·电子器件·硬件笔试真题·音频器件
EasyGBS12 小时前
视频设备轨迹回放平台EasyCVR打造视频智能融合新平台,驱动智慧机场迈向数字新时代
网络·人工智能·安全·音视频
EasyGBS13 小时前
视频设备轨迹回放平台EasyCVR综合智能化,搭建运动场体育赛事直播方案
网络·安全·音视频
SKYDROID云卓小助手17 小时前
三轴云台之相机技术篇
运维·服务器·网络·数码相机·音视频
yunteng52118 小时前
音视频(一)ZLMediaKit搭建部署
音视频·zlmediakit·安装搭建
Merokes21 小时前
关于Gstreamer+MPP硬件加速推流问题:视频输入video0被占用
c++·音视频·rk3588
EasyGBS1 天前
NVR接入录像回放平台EasyCVR视频系统守护舌尖上的安全,打造“明厨亮灶”云监管平台
安全·音视频