Skip to content

Commit

Permalink
WIP: get n_frames on trodes vid
Browse files Browse the repository at this point in the history
  • Loading branch information
CBroz1 committed Nov 5, 2024
1 parent 58bb47f commit 46818b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
68 changes: 34 additions & 34 deletions src/spyglass/position/v1/dlc_utils_makevid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import shutil
import subprocess
from concurrent.futures import ProcessPoolExecutor, as_completed
from os import system as os_system
from pathlib import Path
from typing import Tuple

Expand Down Expand Up @@ -128,9 +127,32 @@ def _set_frame_info(self):
"""Set the frame information for the video."""
logger.debug("Setting frame information")

width, height, self.frame_rate = self._get_input_stats()
ret = subprocess.run(
[
"ffprobe",
"-v",
"error",
"-select_streams",
"v",
"-show_entries",
"stream=width,height,r_frame_rate,nb_frames",
"-of",
"csv=p=0:s=x",
str(self.video_filename),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
if ret.returncode != 0:
raise ValueError(f"Error getting video dimensions: {ret.stderr}")

stats = ret.stdout.strip().split("x")
self.width, self.height = tuple(map(int, stats[:2]))
self.frame_rate = eval(stats[2])

self.frame_size = (
(width, height)
(self.width, self.height)
if not self.crop
else (
self.crop[1] - self.crop[0],
Expand All @@ -144,45 +166,21 @@ def _set_frame_info(self):
)
self.fps = int(np.round(self.frame_rate))

if self.frames is None:
if self.frames is None and self.video_frame_inds is not None:
self.n_frames = int(
len(self.video_frame_inds) * self.percent_frames
)
self.frames = np.arange(0, self.n_frames)
else:
elif self.frames is not None:
self.n_frames = len(self.frames)
else:
self.n_frames = int(stats[3])

self.pad_len = len(str(self.n_frames))

def _get_input_stats(self, video_filename=None) -> Tuple[int, int]:
def _set_input_stats(self, video_filename=None) -> Tuple[int, int]:
"""Get the width and height of the video."""
logger.debug("Getting video dimensions")

video_filename = video_filename or self.video_filename
ret = subprocess.run(
[
"ffprobe",
"-v",
"error",
"-select_streams",
"v",
"-show_entries",
"stream=width,height,r_frame_rate",
"-of",
"csv=p=0:s=x",
str(video_filename),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
if ret.returncode != 0:
raise ValueError(f"Error getting video dimensions: {ret.stderr}")

stats = ret.stdout.strip().split("x")
width, height = tuple(map(int, stats[:-1]))
frame_rate = eval(stats[-1])

return width, height, frame_rate
logger.debug("Getting video stats with ffprobe")

def _set_plot_bases(self):
"""Create the figure and axes for the video."""
Expand Down Expand Up @@ -507,6 +505,7 @@ def ffmpeg_stitch_partial(self, start_frame, output_partial_video):
)
except subprocess.CalledProcessError as e:
logger.error(f"Error stitching partial video: {e.stderr}")
logger.debug(f"stderr: {ret.stderr}")

def concat_partial_videos(self):
"""Concatenate all the partial videos into one final video."""
Expand Down Expand Up @@ -540,6 +539,7 @@ def concat_partial_videos(self):
)
except subprocess.CalledProcessError as e:
logger.error(f"Error stitching partial video: {e.stderr}")
logger.debug(f"stderr: {ret.stderr}")


def make_video(**kwargs):
Expand Down
4 changes: 3 additions & 1 deletion src/spyglass/position/v1/position_trodes_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,5 +358,7 @@ def make(self, key):
position_time=position_time,
output_video_filename=output_video_filename,
cm_to_pixels=meters_per_pixel * M_TO_CM,
key_hash=dj.hash.key_hash(key),
)
self.insert1(dict(**key, has_video=True))

# self.insert1(dict(**key, has_video=True)) # INTENTIONAL FAIL

0 comments on commit 46818b2

Please sign in to comment.