ffmpeg.exe 命令使用

server/2024/12/23 0:48:39/

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_timestamps 1 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_timestamps 1:每个分片从0开始,否则分片在前一片基础上开始,VLC播放时在前面分片累积时长基础上开播播放

-segment_time 60或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 -copyts cut.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_ts 1 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


http://www.ppmy.cn/server/152361.html

相关文章

【网络安全】用 Frida 修改软件为你所用

用 Frida 修改软件为你所用 Frida是一个强大的设备操作工具,它允许我们分析、修改和与运行中的应用程序交互。Frida通过在目标进程中创建一个线程,并通过这个线程执行一些启动代码来实现交互功能。这种交互被称为“代理”,它允许我们添加Jav…

SpringBoot配置Swagger和MybatisPlus

SpringBoot配置Swagger和MybatisPlus 前言1. 配置Swagger1)导入依赖2)修改配置文件application.yml3)在启动类Application.java中开启4)创建一个testController.java测试5)启动项目测试 2. 配置MybatisPlus1&#xff0…

python:正则表达式

正则表达式(Regular Expressions,简称 regex)是一种强大的文本处理工具,用于匹配字符串中的字符组合。Python 提供了 re 模块来支持正则表达式的操作。以下是一些常用的正则表达式操作和示例: 导入 re 模块 首先&…

故障诊断 | 一个小创新:特征提取+KAN分类

往期精彩内容: Python-凯斯西储大学(CWRU)轴承数据解读与分类处理 基于FFT CNN - BiGRU-Attention 时域、频域特征注意力融合的轴承故障识别模型-CSDN博客 基于FFT CNN - Transformer 时域、频域特征融合的轴承故障识别模型-CSDN博客 P…

优先队列【东北大学oj数据结构9-3】C++

优先队列 优先级队列是一种数据结构,其中保存了一组数据 S,其中每个元素都有一个键,并执行以下操作: insert(S, k):将元素k插入集合S extractMax(S):从S中取出S中key最大的元素并返回其值 创建一个程序&am…

Vue.js前端框架教程3:Vue setup语法糖和异步操作

文章目录 script setup基本语法使用 Composition API组件定义使用生命周期钩子模板引用使用 defineProps 和 defineEmits组合多个 <script setup> 标签 Vue异步操作1. 使用 async 和 await2. 使用 Promise3. 在 created 或 mounted 钩子中执行异步操作4. 使用 watch 或 w…

24届FPGA秋招经验分享

学员客户&#xff1a;首先自我介绍一下&#xff0c;我本科就读于一所985高校&#xff0c;专业是电子信息工程&#xff0c;硕士阶段则专注于FPGA方向的研究。虽然有着相对扎实的理论基础&#xff0c;但在秋招过程中&#xff0c;我仍然遇到了不少挑战。以下是我结合自己的亲身经历…

单元测试使用记录

什么是单元测试 简单来说就是对一个类中的方法进行测试&#xff0c;对输出的结果检查判断是否符合预期结果 但是在多年的工作中&#xff0c;从来没有哪个项目中真正系统的用到了单元测试&#xff0c;因此对它还是很陌生的&#xff0c;也就造成更加不会在项目中区使用它。 如何…