Skip to content

Instantly share code, notes, and snippets.

@khuangaf
Created August 22, 2018 14:25
Show Gist options
  • Save khuangaf/7eba0599543c2a1ba34ad8cc9159fd45 to your computer and use it in GitHub Desktop.
Save khuangaf/7eba0599543c2a1ba34ad8cc9159fd45 to your computer and use it in GitHub Desktop.
def video2frames(video_id, video_path, processed_videos_dir, frame_path, sample_rate):
"""
Execute shell command which calls ffmpeg to split video into frames.
Parameters
----------
video_id : str
The video id of a video
video_path: str
The directory storing the videos
processed_video_path: str
The directory storing videos that have been split.
sample_rate: int
The sample rate for splitting the video.
"""
#concat path and video id
path_video = video_path + video_id + '.mp4'
video_duration = get_duration(path_video)
# split only the main part of the video
starting_time = 0.2 * video_duration
split_duration = 0.6 * video_duration
try:
#-loglevel panic: slience
#-hwaccel vdpau: hardware acceleration with GPU
# -ss starting time
# -t duration
cmd = f'ffmpeg -ss {starting_time} -t {split_duration} -i {path_video} -r {sample_rate} {frame_path}/{video_id}-%07d-{sample_rate}.png'.split(" ")
subprocess.run(cmd)
shutil.move(path_video, f"{processed_videos_dir}/{video_id}.mp4")
except Exception as e:
print(f'Failed to cut videos {video_id}: {e}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment