#!/usr/bin/env python

import argparse
import multiprocessing
import os.path as osp
import shlex
import subprocess


def speed_up_video(video_file, times):
    base, ext = osp.splitext(video_file)
    out_file = base + '_x{times}'.format(times=times) + ext

    cmd = 'ffmpeg -i {in_file} -filter:v "setpts={rate}*PTS" {out_file}'
    cmd = cmd.format(
        in_file=video_file,
        rate=1.0 / times,
        out_file=out_file,
    )

    subprocess.call(shlex.split(cmd))


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('video_files', nargs='+', help='File to convert.')
    parser.add_argument('-x', '--times', type=int, required=True,
                        help='How many times to speed/slow up/down the video.')
    args = parser.parse_args()

    video_files = args.video_files
    times = args.times

    for file_ in video_files:
        kwargs = {'video_file': file_, 'times': times}
        p = multiprocessing.Process(
            target=speed_up_video,
            kwargs=kwargs,
        )
        p.start()


if __name__ == '__main__':
    main()
