pythonで動画の解像度とフレームレート(FPS)を変えてみた | たくまのブログ

たくまのブログ

皆さんに見て頂きたい反面、
ただの記録としても使います。

ChatGPTに質問すれば

1発目の回答で正しく動作出来ました。

 

楽すぎる。以下コードです

(変数input_file等の変数は環境に合わせて変更してください)

 

 

import cv2

 

def change_resolution_and_fps(input_file, output_file, new_resolution=(640, 480), new_fps=30):

    # 入力ビデオファイルを開く

    cap = cv2.VideoCapture(input_file)

 

    # 入力ビデオのコーデックを取得

    fourcc = cv2.VideoWriter_fourcc(*'mp4v')

 

    # 現在の解像度とFPSを取得

    current_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

    current_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    current_fps = cap.get(cv2.CAP_PROP_FPS)

 

    print("現在の解像度:", (current_width, current_height))

    print("現在のFPS:", current_fps)

 

    # 出力ビデオを書き込むためのVideoWriterオブジェクトを作成

    out = cv2.VideoWriter(output_file, fourcc, new_fps, new_resolution)

 

    while cap.isOpened():

        ret, frame = cap.read()

        if not ret:

            break

 

        # フレームを新しい解像度にリサイズ

        frame = cv2.resize(frame, new_resolution)

 

        # フレームを出力ビデオに書き込む

        out.write(frame)

 

    # VideoCaptureとVideoWriterオブジェクトを解放

    cap.release()

    out.release()

    cv2.destroyAllWindows()

 

    print("ビデオの解像度とFPSが正常に変更されました。")

 

# 例:

input_file = 'you_select_movie.mp4'

output_file = 'output_video.mp4'

new_resolution = (600, 480)  # 解像度を1280x720に変更

new_fps = 30  # FPSを25に変更

change_resolution_and_fps(input_file, output_file, new_resolution, new_fps)