Skip to content

Commit 1d1a160

Browse files
committed
Fix: post-processed videos have the same fps of the input one.
1 parent 68986dd commit 1d1a160

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

PostProcessing/PostProcessVideos.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from video import extract_frames
1313
from video import rebuild_video
14+
from video import extract_video_info
1415

1516

1617
THRESHOLD_NS = 10 * 1000 * 1000 # millis * micros * nanos
@@ -125,8 +126,10 @@ def main(input_dir: Path, output_dir: Path):
125126
extract_frames(video_file=video_file, timestamps_df=orig_df, output_dir=tmp_dir)
126127

127128
# Reconstruct videos
129+
vinfo = extract_video_info(video_path=video_file)
130+
input_fps = vinfo.fps
128131
video_out_filepath = output_dir / (cID + ".mp4")
129-
rebuild_video(dir=Path(tmp_dir), frames=trimmed_df, outfile=video_out_filepath)
132+
rebuild_video(dir=Path(tmp_dir), frames=trimmed_df, fps=input_fps, outfile=video_out_filepath)
130133
# And save also the CSV
131134
csv_out_filepath = video_out_filepath.with_suffix(".csv")
132135
trimmed_df.to_csv(path_or_buf=csv_out_filepath, header=True, index=False)

PostProcessing/video.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cv2
22
import os
33
from pathlib import Path
4+
from collections import namedtuple
45

56
import pandas as pd
67
import numpy as np
@@ -35,7 +36,43 @@ def video_info(video_path: str) -> Tuple[int, int, int]:
3536
return video_w, video_h, n_frames
3637

3738

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:
3976

4077
# Open the video file
4178
cap = cv2.VideoCapture(video_file)
@@ -60,7 +97,7 @@ def extract_frames(video_file: str, timestamps_df: pd.DataFrame, output_dir: str
6097
cap.release()
6198

6299

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:
64101
video_w, video_h, _ = video_info(video_file)
65102

66103
ffmpeg_read_process = (
@@ -98,7 +135,7 @@ def extract_frames_ffmpeg(video_file: str, timestamps_df: pd.DataFrame, output_d
98135
ffmpeg_read_process = None
99136

100137

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:
102139

103140
# We don't know the target video size, yet.
104141
frame_width = None
@@ -131,6 +168,7 @@ def rebuild_video(dir: Path, frames: pd.DataFrame, outfile: Path) -> None:
131168
ffmpeg_video_out_process = (
132169
ffmpeg
133170
.input('pipe:', format='rawvideo', pix_fmt='rgb24', s='{}x{}'.format(frame_width, frame_height))
171+
.filter('fps', fps=fps)
134172
# -vf "drawtext=fontfile=Arial.ttf: fontsize=48: text=%{n}: x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000099"
135173
.drawtext(text="%{n}", escape_text=False,
136174
#x=50, y=50,

0 commit comments

Comments
 (0)