Skip to content

Commit

Permalink
Merge pull request #72 from akamhy/videoduration
Browse files Browse the repository at this point in the history
  • Loading branch information
akamhy authored Nov 29, 2021
2 parents a73bda2 + e91b2cc commit 95904f4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
20 changes: 20 additions & 0 deletions tests/test_videoduration.py
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
1 change: 1 addition & 0 deletions videohash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"""

from .videohash import VideoHash
from .videoduration import video_duration

from .__version__ import (
__title__,
Expand Down
2 changes: 1 addition & 1 deletion videohash/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)

__url__ = "https://akamhy.github.io/videohash/"
__version__ = "2.1.5"
__version__ = "2.1.6"
__status__ = "production"
__author__ = "Akash Mahanty"
__author_email__ = "akamhy@yahoo.com"
Expand Down
43 changes: 43 additions & 0 deletions videohash/videoduration.py
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)
2 changes: 2 additions & 0 deletions videohash/videohash.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .collagemaker import MakeCollage
from .downloader import Download
from .videoduration import video_duration
from .framesextractor import FramesExtractor
from .exceptions import DidNotSupplyPathOrUrl, StoragePathDoesNotExist
from .utils import (
Expand Down Expand Up @@ -95,6 +96,7 @@ def __init__(

self.image = Image.open(self.collage_path)
self.bits_in_hash = 64
self.video_duration = video_duration(self.video_path)

self._calc_hash()

Expand Down

0 comments on commit 95904f4

Please sign in to comment.