0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Snippet】動画フレームを静止画として切り出す

Posted at
import argparse
import os
import subprocess
from glob import glob


def create_arg_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument('--dir', type=str, default='./')
    parser.add_argument('--dryrun', type=bool, default=False)
    args = parser.parse_args()  
    return args


def extract_frame(file_path, save_dir, dryrun=False):
    basename = os.path.basename(file_path)
    basename_without_ext = os.path.splitext(basename)[0]
    cmd = r'ffmpeg -i {} -r 2 -vcodec png -filter:v "crop=w=960:h=1280:x=0:y=0" {}/{}_%04d.png'.format(
        file_path, save_dir, basename_without_ext)
    print('Execute:', cmd)
    if not dryrun:
        subprocess.run(cmd)


def main():
    """
    dir/
    ├ sample01/
    ├   ├ file01.mp4
    ├   └ file02.mp4
    ├ sample02/
    └ sample03/
    """
    args = create_arg_parser()
    dir_list = glob(os.path.join(args.dir, '*'))
    if len(dir_list) == 0:
        raise ValueError('No directories')
    
    for i_dir in dir_list:
        mov_file_list = glob(os.path.join(i_dir, '*.mp4'))
        if len(mov_file_list) == 0:
            print('No files:', i_dir)
        
        for i_mov_file in mov_file_list:
            extract_frame(i_mov_file, i_dir, dryrun=False)         
 
    print('全て終了しました')



if __name__ == '__main__':
    main()
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?