-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from akamhy/videoduration
- Loading branch information
Showing
5 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pytest | ||
import os | ||
from videohash.videoduration import video_duration | ||
|
||
this_dir = os.path.dirname(os.path.realpath(__file__)) | ||
|
||
|
||
def test_video_duration(): | ||
|
||
video_path = ( | ||
this_dir | ||
+ os.path.sep | ||
+ os.path.pardir | ||
+ os.path.sep | ||
+ "assets" | ||
+ os.path.sep | ||
+ "rocket.mkv" | ||
) | ||
|
||
assert (video_duration(video_path) - 52.08) < 0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import re | ||
from shutil import which | ||
from subprocess import Popen, PIPE | ||
|
||
from typing import Optional | ||
|
||
# Module to determine the length of video. | ||
# The length is found by the FFmpeg, the output of video_duration is in seconds. | ||
|
||
|
||
def video_duration(video_path: str, ffmpeg_path: Optional[str] = None) -> float: | ||
""" | ||
Retrieve the exact video duration as echoed by the FFmpeg and return | ||
the duration in seconds. Maximum duration supported is 999 hours, above | ||
which the regex is doomed to fail(no match). | ||
:param video_path: Absolute path of the video file. | ||
:param ffmpeg_path: Path of the FFmpeg software if not in path. | ||
:return: Video length(duration) in seconds. | ||
:rtype: float | ||
""" | ||
|
||
if not ffmpeg_path: | ||
ffmpeg_path = str(which("ffmpeg")) | ||
|
||
command = f'"{ffmpeg_path}" -i "{video_path}"' | ||
process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) | ||
output, error = process.communicate() | ||
|
||
match = re.search( | ||
r"Duration\:(\s\d?\d\d\:\d\d\:\d\d\.\d\d)\,", | ||
(output.decode() + error.decode()), | ||
) | ||
|
||
if match: | ||
duration_string = match.group(1) | ||
|
||
hours, minutes, seconds = duration_string.strip().split(":") | ||
|
||
return float(hours) * 60.00 * 60.00 + float(minutes) * 60.00 + float(seconds) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters