Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1011327
  • 博文数量: 157
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1388
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-09 15:37
文章分类

全部博文(157)

文章存档

2023年(9)

2022年(2)

2021年(18)

2020年(7)

2017年(13)

2016年(53)

2015年(55)

我的朋友

分类: Python/Ruby

2021-01-15 17:26:35

场景:想从某个大视频中截出来有某个人脸的那段视频片,因为视频片短的才几秒,所以还需要把所有包含这个人脸的视频片再合成一个大视频

操作:
一.切分:
ffmpeg -i file_origin  -ss  start_time_s  to  end_time_s  file_out
其中file_originfile_out可以是flv也可以是mp4,mkv等等
注意:里面不要写上-c copy,eg: mkv文件后保存为mp4文件,但mkv文件的音频格式是opus时就会出现问题,导致
截出的一些片异常,没有任何视频信息。

二.合并:
1.  因为是一个文件(或一个电视剧的多集)截出来的,所以编码格式、帧率、分辨率等什么的都是一样的,所以用下面直接拼接命令一般来说会成功
ffmpeg -f concat -i filenamelist.txt -c copy output_compose.mp4
filenamelist.txt中的格式及内容:
file 001.mp4
file 002.mp4
file 003.mp4

2.  但也有不成功的(暂时没遇到,但感觉如果视频含B帧应该在合成时也会有问题),当不是一个
类型的文件截出来的时候,更容易出现一些问题(我就遇到了),按照上面的方法合出来的视频比预期长了很多,但后面的视频像是卡住了其实是只有一小段正常的视频后面的没有拼接上。
我查了一个视频截出来的视频片的编码格式、分辨率、帧率等一些信息,发现基本相同看不出来什么,于是上网查了下发现一个词-B帧,如果存在B帧,我猜B帧参考的前后帧在拼接时就会乱了不对应了所以拼不成功,所以去掉B帧(拼接时加参数 -bf 0) ,试了一个问题视频解决了。
补充查看视频帧类型的命令:

ffprobe -select_streams v -of xml -show_entries frame=pkt_pts,pkt_dts,pkt_size,pkt_duration,pict_type 065.mp4 > a.xml

cat a.xml |grep 'pict_type="B"'


3.  但还有个视频还是失败,于是我又看了这个视频片的基本信息,发现分辨率和帧率有不同的,值相差较大,于是我尝试转分辨率+转帧率+去B帧,转后再拼成功了。
但我不知道分辨率和帧率哪个是影响因素,于是单一变量测试,发现影响因素是帧率。
注意:转帧率的时候不能带-c copy,视频编码参数啥都没带其实就是走了转码。
把视频片再处理脚本:concat.py (windows和linux上都可运行)
# -*- coding: utf-8 -*-
#执行命令:  c:\Python36\python.exe concat.py F:\test,在F:\test\tmp下生成处理过的视频片
import os
import sys
dest_path = sys.argv[1]+os.sep+"tmp"
if not os.path.isdir(dest_path):
    os.mkdir(dest_path)

for root, dirs, files in os.walk(sys.argv[1]):
    for file in files:
        command = "ffmpeg  -i "+sys.argv[1]+os.sep+file+" -bf 0  -r 25 -y "+dest_path+os.sep+file
        os.system(command)
处理后的视频片再执行拼接命令,注意拼接命令中的filelist.txt和视频片要在一个目录,不在一个目录下我还不知道怎么弄。

4.  这个拼接是可以了,但是播放看比实际长了,最后面是画面不动了还有声音,感觉可能是音视频不同步导致的吧,执行ffmpeg时有下面的信息看着像视频重复,因为我们只关心视频所以我粗暴的把后面那段静止的截去了。

ffmpeg -f concat -i filenamelist.txt -c copy output_compose.mp4
结尾处会有下面的信息,时间戳问题是一直都有,最后重复了视频,时长增加了,至今还不知道为什么?还得研究下再说



[mp4 @ 0000018f2c805040] Non-monotonous DTS in output stream 0:1; previous: 36061934, current: 36061760; changing to 36061935. This may result in incorrect timestamps in the output file.
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000018f2c2b5640] Auto-inserting h264_mp4toannexb bitstream filter
    Last message repeated 4 times
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000018f2c83e040] Auto-inserting h264_mp4toannexb bitstream filter
    Last message repeated 3 times
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000018f2c2b5640] Auto-inserting h264_mp4toannexb bitstream filter
    Last message repeated 2 times
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000018f2c83e040] Auto-inserting h264_mp4toannexb bitstream filter
    Last message repeated 2 times
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000018f2c86af00] Auto-inserting h264_mp4toannexb bitstream filter
    Last message repeated 1 times
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000018f2c83e040] Auto-inserting h264_mp4toannexb bitstream filter
    Last message repeated 1 times
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000018f2c83e040] Auto-inserting h264_mp4toannexb bitstream filter3x
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000018f2c86af00] Auto-inserting h264_mp4toannexb bitstream filter
    Last message repeated 1 times
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000018f2c8b2380] Auto-inserting h264_mp4toannexb bitstream filter
frame=25144 fps=15613 q=-1.0 Lsize=  119967kB time=00:18:17.09 bitrate= 895.8kbits/s speed= 681x
video:103918kB audio:15496kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.462540%

阅读(2064) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~