'22/09/03更新:動画編集の進捗状況をprint出力によりわかるようにしました。また、完了後に、処理に要した時間をprint出力するコードも追記しました。
本記事では、表題の雛形コードを載せました。FPS(Frame Per Second)で速度調整します。一枚あたりのフレーム数の意味で、値が大きい程速くなります。実施例は下記リンクです。
www.youtube.com
■本プログラム
import os
import cv2
import time
def movie2movie_fps_scale_func(input_file, out_file, fps_scale):
video = cv2.VideoCapture(input_file)
fps = int(video.get(cv2.CAP_PROP_FPS))
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
num_of_frame = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
frame_rate = int(fps * fps_scale)
print('frame_rate -> ', frame_rate)
writer = cv2.VideoWriter(out_file,
cv2.VideoWriter_fourcc('m', 'p', '4', 'v'),
frame_rate,
(width, height),
True)
for i in range(num_of_frame):
ret, frame = video.read()
if i % 100 == 0:
print(i, '/', num_of_frame)
writer.write(frame)
video.release()
writer.release()
if __name__ == '__main__':
start_time = time.time()
input_video = 'IMG_5424.mp4'
fps_scale = 20
base_name = os.path.splitext(os.path.basename(input_video))[0]
out_video = base_name + '_fps' + str(fps_scale) + 'times.mp4'
movie2movie_fps_scale_func(input_video, out_video, fps_scale)
processing_time = time.time() - start_time
print('processing_time -> ', processing_time / 60, 'min')
print("finished")
ちなみに、タートルグラフィックスを使う例は下記リンク先を参照下さい。
hk29.hatenablog.jp
倍速再生動画はこちらもどうぞ
www.youtube.com
以上
<広告>
リンク
リンク