1
1
import cv2
2
2
import os
3
3
from pathlib import Path
4
+ from collections import namedtuple
4
5
5
6
import pandas as pd
6
7
import numpy as np
@@ -35,7 +36,43 @@ def video_info(video_path: str) -> Tuple[int, int, int]:
35
36
return video_w , video_h , n_frames
36
37
37
38
38
- def extract_frames (video_file : str , timestamps_df : pd .DataFrame , output_dir : str ):
39
+ VideoInfo = namedtuple ("VideoInfo" , ["width" , "height" , "n_frames" , "fps" , "codec" ])
40
+
41
+
42
+ def extract_video_info (video_path : str ) -> VideoInfo :
43
+ """
44
+ Uses the ffmpeg.probe function to retrieve information about a video file.
45
+
46
+ :param video_path: Path to a valid video file
47
+ :return: An instance of a VideoTuple named tuple with self-explaining information.
48
+ """
49
+
50
+ #
51
+ # Fetch video info
52
+ info = ffmpeg .probe (video_path )
53
+ # Get the list of all video streams
54
+ video_streams = [stream for stream in info ['streams' ] if stream ['codec_type' ] == 'video' ]
55
+ if len (video_streams ) == 0 :
56
+ raise BaseException ("No video streams found in file '{}'" .format (video_path ))
57
+
58
+ # retrieve the first stream of type 'video'
59
+ info_video = video_streams [0 ]
60
+
61
+ framerate_ratio_str = info_video ['r_frame_rate' ]
62
+ fps = eval (framerate_ratio_str )
63
+
64
+ out = VideoInfo (
65
+ width = info_video ['width' ],
66
+ height = info_video ['height' ],
67
+ n_frames = int (info_video ['nb_frames' ]),
68
+ fps = fps ,
69
+ codec = info_video ['codec_name' ]
70
+ )
71
+
72
+ return out
73
+
74
+
75
+ def extract_frames (video_file : str , timestamps_df : pd .DataFrame , output_dir : str ) -> None :
39
76
40
77
# Open the video file
41
78
cap = cv2 .VideoCapture (video_file )
@@ -60,7 +97,7 @@ def extract_frames(video_file: str, timestamps_df: pd.DataFrame, output_dir: str
60
97
cap .release ()
61
98
62
99
63
- def extract_frames_ffmpeg (video_file : str , timestamps_df : pd .DataFrame , output_dir : str ):
100
+ def extract_frames_ffmpeg (video_file : str , timestamps_df : pd .DataFrame , output_dir : str ) -> None :
64
101
video_w , video_h , _ = video_info (video_file )
65
102
66
103
ffmpeg_read_process = (
@@ -98,7 +135,7 @@ def extract_frames_ffmpeg(video_file: str, timestamps_df: pd.DataFrame, output_d
98
135
ffmpeg_read_process = None
99
136
100
137
101
- def rebuild_video (dir : Path , frames : pd .DataFrame , outfile : Path ) -> None :
138
+ def rebuild_video (dir : Path , frames : pd .DataFrame , fps : float , outfile : Path ) -> None :
102
139
103
140
# We don't know the target video size, yet.
104
141
frame_width = None
@@ -131,6 +168,7 @@ def rebuild_video(dir: Path, frames: pd.DataFrame, outfile: Path) -> None:
131
168
ffmpeg_video_out_process = (
132
169
ffmpeg
133
170
.input ('pipe:' , format = 'rawvideo' , pix_fmt = 'rgb24' , s = '{}x{}' .format (frame_width , frame_height ))
171
+ .filter ('fps' , fps = fps )
134
172
# -vf "drawtext=fontfile=Arial.ttf: fontsize=48: text=%{n}: x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000099"
135
173
.drawtext (text = "%{n}" , escape_text = False ,
136
174
#x=50, y=50,
0 commit comments