Skip to content

Commit 95904f4

Browse files
authored
Merge pull request #72 from akamhy/videoduration
2 parents a73bda2 + e91b2cc commit 95904f4

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

tests/test_videoduration.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
import os
3+
from videohash.videoduration import video_duration
4+
5+
this_dir = os.path.dirname(os.path.realpath(__file__))
6+
7+
8+
def test_video_duration():
9+
10+
video_path = (
11+
this_dir
12+
+ os.path.sep
13+
+ os.path.pardir
14+
+ os.path.sep
15+
+ "assets"
16+
+ os.path.sep
17+
+ "rocket.mkv"
18+
)
19+
20+
assert (video_duration(video_path) - 52.08) < 0.1

videohash/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"""
7474

7575
from .videohash import VideoHash
76+
from .videoduration import video_duration
7677

7778
from .__version__ import (
7879
__title__,

videohash/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
)
77

88
__url__ = "https://akamhy.github.io/videohash/"
9-
__version__ = "2.1.5"
9+
__version__ = "2.1.6"
1010
__status__ = "production"
1111
__author__ = "Akash Mahanty"
1212
__author_email__ = "akamhy@yahoo.com"

videohash/videoduration.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import re
2+
from shutil import which
3+
from subprocess import Popen, PIPE
4+
5+
from typing import Optional
6+
7+
# Module to determine the length of video.
8+
# The length is found by the FFmpeg, the output of video_duration is in seconds.
9+
10+
11+
def video_duration(video_path: str, ffmpeg_path: Optional[str] = None) -> float:
12+
"""
13+
Retrieve the exact video duration as echoed by the FFmpeg and return
14+
the duration in seconds. Maximum duration supported is 999 hours, above
15+
which the regex is doomed to fail(no match).
16+
17+
:param video_path: Absolute path of the video file.
18+
19+
:param ffmpeg_path: Path of the FFmpeg software if not in path.
20+
21+
:return: Video length(duration) in seconds.
22+
23+
:rtype: float
24+
"""
25+
26+
if not ffmpeg_path:
27+
ffmpeg_path = str(which("ffmpeg"))
28+
29+
command = f'"{ffmpeg_path}" -i "{video_path}"'
30+
process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
31+
output, error = process.communicate()
32+
33+
match = re.search(
34+
r"Duration\:(\s\d?\d\d\:\d\d\:\d\d\.\d\d)\,",
35+
(output.decode() + error.decode()),
36+
)
37+
38+
if match:
39+
duration_string = match.group(1)
40+
41+
hours, minutes, seconds = duration_string.strip().split(":")
42+
43+
return float(hours) * 60.00 * 60.00 + float(minutes) * 60.00 + float(seconds)

videohash/videohash.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from .collagemaker import MakeCollage
1111
from .downloader import Download
12+
from .videoduration import video_duration
1213
from .framesextractor import FramesExtractor
1314
from .exceptions import DidNotSupplyPathOrUrl, StoragePathDoesNotExist
1415
from .utils import (
@@ -95,6 +96,7 @@ def __init__(
9596

9697
self.image = Image.open(self.collage_path)
9798
self.bits_in_hash = 64
99+
self.video_duration = video_duration(self.video_path)
98100

99101
self._calc_hash()
100102

0 commit comments

Comments
 (0)