Skip to content

Commit

Permalink
refactor: updated helper function to calculate video resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
shihanxiong committed May 31, 2023
1 parent b006688 commit 6b230d1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/test_video_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ def test_trim_mp4_by_timestamp():
)
assert os.path.exists(trimmed_video_path) == True
assert VideoUtils.get_video_duration(trimmed_video_path) == 5


def test_get_video_resolution():
width, height = VideoUtils.get_video_resolution(test_video_path)
assert width == 1920
assert height == 1080
10 changes: 4 additions & 6 deletions src/video_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,9 @@ def calculate_output_resolutions(self):

# Get information about the videos
for video in self.video_list:
cmd = f"ffprobe -v error -show_entries stream=width,height -of csv=p=0 {video}"
output = subprocess.check_output(cmd, shell=True)
width, height = output.decode().strip().split(",")
self.output_width = max(self.output_width, int(width))
self.output_height = max(self.output_height, int(height))
width, height = VideoUtils.get_video_resolution(video)
self.output_width = max(self.output_width, width)
self.output_height = max(self.output_height, height)

self.master.status_component.set_and_log_status(
f"output resolution is determined at {self.output_width} x {self.output_height}"
Expand Down Expand Up @@ -266,7 +264,7 @@ def add_intro_outro(self, final_file):
VideoUtils.concatenate_videos(
videos,
self.output_directory,
f"{TimeUtils.get_current_timestamp()}.mp4",
f"{TimeUtils.get_current_timestamp()}_with_intro_outro.mp4",
self.ffmpeg_preset_value,
)
except Exception as err:
Expand Down
25 changes: 25 additions & 0 deletions src/video_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,28 @@ def trim_mp4_by_timestamp(
subprocess.run(cmd, text=True, check=True)
except Exception as err:
logging.error(f"{VideoUtils.__name__}: {str(err)}")

@staticmethod
def get_video_resolution(video):
try:
cmd = [
"ffprobe",
"-v",
"error",
"-show_entries",
"stream=width,height",
"-of",
"csv=p=0",
video,
]

result = subprocess.run(cmd, capture_output=True, text=True, check=True)
output = result.stdout

try:
width, height = output.split(",")
return int(width), int(height)
except (json.JSONDecodeError, KeyError):
return None, None
except Exception as err:
logging.error(f"{VideoUtils.__name__}: {str(err)}")

0 comments on commit 6b230d1

Please sign in to comment.