当我用ffmpeg剪辑视频时,我似乎正在丢失帧。
这是我要采取的步骤:
[获取要剪切的帧号]-> [将帧号转换为hh:mm:ss.ms格式]-> [运行ffmpeg进程]
代码如下:
import subprocess
def frames_to_timecode(frame,frameRate):
'''
Convert frame into a timecode HH:MM:SS.MS
frame = The frame to convert into a time code
frameRate = the frame rate of the video
'''
#convert frames into seconds
seconds = frame / frameRate
#generate the time code
timeCode = '{h:02d}:{m:02d}:{s:02f}'.format(
h=int(seconds/3600),
m=int(seconds/60%60),
s=seconds%60)
return timeCode
frameRate = 24.0
inputVideo = r"C:\Users\aquamen\Videos\vlc-record-2018-10-23-17h11m11s-SEQ-0200_animatic_v4_20180827_short.mp4"
outputVideo = r"C:\Users\aquamen\Videos\ffmpeg_test_clip001.mp4"
ffmpeg = r"C:\ffmpeg\ffmpeg-20181028-e95987f-win64-static\bin\ffmpeg.exe"
endFrame = frames_to_timecode(29,frameRate)
startFrame = frames_to_timecode(10,frameRate)
subprocess.call([ffmpeg,'-i',inputVideo,'-ss',startFrame,'-to',endFrame,outputVideo])
这是原始视频和剪辑后的视频的图像,其中时间代码表示一帧丢失。时间代码应显示00:01:18:10而不是00:01:18:11。
答案 0 :(得分:1)
所以我的一个朋友知道了这一点。因此,如果将帧除以fps(Frame / fps),就可以知道何时需要使用-ss剪切该帧,但是问题是默认情况下python将小数点后第12位舍入。因此,您无需将数字四舍五入,而ffmpeg最多只能保留小数点后3位。
这是我针对任何遇到此问题的人修改的代码。如果要剪切帧号上的视频,请使用以下方法:
import subprocess
def frame_to_seconds(frame,frameRate):
'''
This will turn the frame into seconds.miliseconds
so you can cut on frames in ffmpeg
frame = The frame to convert into seconds
frameRate = the frame rate of the video
'''
frameRate = float(frameRate)
seconds = frame / frameRate
result = str(seconds - seconds % 0.001)
return result
inputVideo = "yourVideo.mp4"
outputVideo = "clipedVideo.mp4"
ffmpeg = r"C:\ffmpeg\bin\ffmpeg.exe"
frameRate = 24
subprocess.call([ffmpeg,
'-i',inputVideo,
'-ss',frame_to_seconds(10,frameRate),
'-to',frame_to_seconds(20,frameRate),
outputVideo])