ffmpeg常用功能(随时更新)

1.帧率转变,将视频角度旋转到正面(opencv无法解码拍摄时的角度信息)
ffmpeg_cmd = ['ffmpeg', '-y', '-i', str(video_file_path), '-vcodec', 'libx264', '-acodec', 'copy', '-r',str(fps),des_name]
2.根据起始和结束帧位置截取视频,适应非固定帧率
cmds = ['ffmpeg','-i',str(video_path),'-vf',"select=between(n\,{}\,{})".format(start_ind,end_ind),'-vsync','2','-y','-acodec','copy',str(des_video_path)]

3.将多段视频合并为一个视频
file_list = str(video_path.stem)+'.txt' with open(file_list,'w') as f: for line in merge_videos: f.write('file '+str(line)+'\n') merge_cmds = ['ffmpeg','-f','concat','-safe','0','-i'] + [file_list] + ['-c', 'copy', str(des_merged_video)]
4.视频抽帧,先利用ffprobe获取视频头信息,再抽帧
`
ffprobe_cmd = ('ffprobe -v error -select_streams v:0 '
'-of default=noprint_wrappers=1:nokey=1 -show_entries '
'stream=width,height,avg_frame_rate,duration').split()
ffprobe_cmd.append(str(video_file_path))

p = subprocess.run(ffprobe_cmd, capture_output=True)
res = p.stdout.decode('utf-8').splitlines()
if len(res) < 4:
return

frame_rate = [float(r) for r in res[2].split('/')]
frame_rate = frame_rate[0] / frame_rate[1]
duration = float(res[3])
n_frames = int(frame_rate * duration)

name = video_file_path.stem
dst_dir_path = dst_root_path / name
dst_dir_path.mkdir(exist_ok=True)
n_exist_frames = len([
x for x in dst_dir_path.iterdir()
if x.suffix == '.jpg' and x.name[0] != '.'
])

if n_exist_frames >= n_frames:
return

width = int(res[0])
height = int(res[1])

if width > height:
vf_param = 'scale=-1:{}'.format(size)
else:
vf_param = 'scale={}:-1'.format(size)

if fps > 0:

vf_param += ',minterpolate={}'.format(fps)

ffmpeg_cmd = ['ffmpeg', '-i', str(video_file_path), '-vf', vf_param,'-r',str(fps), '-qscale:v','2']
ffmpeg_cmd += ['-threads', '1', '{}/image_%05d.jpg'.format(dst_dir_path)]
print(ffmpeg_cmd)
subprocess.run(ffmpeg_cmd)
5.将一个文件夹下有序命名的图像序列转换为视频
cmds = ['ffmpeg', '-start_number', start_num, '-i', video_path + '/image_%05d.jpg', '-frames:v', frames_num, '-qscale:v', '2', '-r', 10, des_video_path]
`

posted @ 2021-01-27 10:50  duduheihei  阅读(758)  评论(0)    收藏  举报