Skip to content

Commit

Permalink
Replace ffmpeg with pymediainfo
Browse files Browse the repository at this point in the history
  • Loading branch information
wongcheehong authored Dec 1, 2021
1 parent edbc4a4 commit 733b32d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 61 deletions.
36 changes: 20 additions & 16 deletions how_much_I_have_to_watch.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import os
import subprocess
import sys
import re
from colorama import init
from colorama import Fore, Back
from colorama import Fore
from pymediainfo import MediaInfo


init(autoreset=True)


def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
def get_video_duration(video_path):
clip_info = MediaInfo.parse(video_path)
duration_seconds = clip_info.tracks[0].duration / 1000

return os.path.join(base_path, relative_path)
return duration_seconds


def natural_sort_key(s):
Expand All @@ -30,7 +25,18 @@ def format_duration(duration):
hours = int(duration / 3600)
minutes = int((duration - hours * 3600) / 60)
seconds = int(duration - hours * 3600 - minutes * 60)
return f"{hours}h{minutes}m{seconds}s"

message = ""
if hours > 0:
message += str(hours) + "h "
if minutes > 0:
message += str(minutes) + "m "
if seconds > 0:
message += str(seconds) + "s"
if not message:
message = "0s"

return message


def watch_until_which_video(need_to_watch, start_folder="", start_video=""):
Expand Down Expand Up @@ -68,10 +74,8 @@ def watch_until_which_video(need_to_watch, start_folder="", start_video=""):
run_only_once = False
for video in video_list:
video_path = os.path.join(subdir, video)
duration = subprocess.Popen([resource_path('ffprobe'), '-v', 'error', '-show_entries', 'format=duration', '-of',
'default=noprint_wrappers=1:nokey=1', video_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
duration = duration.stdout.read().decode('utf-8')
duration_sum += float(duration)
duration = get_video_duration(video_path)
duration_sum += duration
if duration_sum >= need_to_watch:
watch_until_folder = os.path.relpath(subdir, current_dir)
print("Watch until this folder > " +
Expand Down
66 changes: 40 additions & 26 deletions videoDurationSum.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import os
import subprocess
import sys
import re
from colorama import init
from colorama import Fore
from pymediainfo import MediaInfo

init(autoreset=True)

def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")

return os.path.join(base_path, relative_path)
def natural_sort_key(s):
_nsre = re.compile('([0-9]+)')
return [int(text) if text.isdigit() else text.lower()
for text in re.split(_nsre, s)]


def get_video_duration(video_path):
clip_info = MediaInfo.parse(video_path)
duration_seconds = clip_info.tracks[0].duration / 1000

return duration_seconds


def format_duration(duration):
hours = int(duration / 3600)
minutes = int((duration - hours * 3600) / 60)
seconds = int(duration - hours * 3600 - minutes * 60)

message = ""
if hours > 0:
message += str(hours) + "h "
if minutes > 0:
message += str(minutes) + "m "
if seconds > 0:
message += str(seconds) + "s"
if not message:
message = "0s"

return message


# Sum up all the video duration in current directory
Expand All @@ -25,29 +49,19 @@ def videoDurationSum():
for file in fileList:
if file.endswith('.mp4'):
videoList.append(file)
videoList.sort(key=natural_sort_key)
# Get the duration of each video file
durationList = []
for video in videoList:
duration = subprocess.Popen([resource_path('ffprobe'), '-v', 'error', '-show_entries', 'format=duration', '-of',
'default=noprint_wrappers=1:nokey=1', video], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
duration = duration.stdout.read().decode('utf-8')
duration = float(duration)
durationList.append(duration)
# Sum up the duration of all the video files
durationSum = 0
for duration in durationList:
for video in videoList:
duration = get_video_duration(video)
durationSum += duration
print(f"{Fore.GREEN}{video}: {Fore.YELLOW}{format_duration(duration)} {Fore.RESET}(Cumulative Sum: {Fore.CYAN}{format_duration(durationSum)}{Fore.RESET})")
# Return the duration sum
return durationSum


duration_in_seconds = videoDurationSum()
# Convert the duration to hours, minutes and seconds
hours = int(duration_in_seconds / 3600)
minutes = int((duration_in_seconds - hours * 3600) / 60)
seconds = int(duration_in_seconds - hours * 3600 - minutes * 60)
# Print the duration
print('The duration of all the videos in the current directory is:')
print(str(hours) + ' hours ' + str(minutes) +
' minutes ' + str(seconds) + ' seconds')
print('\nThe duration of all the videos in the current directory is: ' + Fore.GREEN +
format_duration(duration_in_seconds))
input("Enter any key to exit")
41 changes: 22 additions & 19 deletions videoDurationSum_subdir.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
import os
import subprocess
import sys
import re
from pymediainfo import MediaInfo


_nsre = re.compile('([0-9]+)')
def natural_sort_key(s):
return [int(text) if text.isdigit() else text.lower()
for text in re.split(_nsre, s)]
def get_video_duration(video_path):
clip_info = MediaInfo.parse(video_path)
duration_seconds = clip_info.tracks[0].duration / 1000

return duration_seconds

def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")

return os.path.join(base_path, relative_path)
def natural_sort_key(s):
_nsre = re.compile('([0-9]+)')
return [int(text) if text.isdigit() else text.lower()
for text in re.split(_nsre, s)]


def format_duration(duration):
hours = int(duration / 3600)
minutes = int((duration - hours * 3600) / 60)
seconds = int(duration - hours * 3600 - minutes * 60)
return f"{hours}h{minutes}m{seconds}s"

message = ""
if hours > 0:
message += str(hours) + "h "
if minutes > 0:
message += str(minutes) + "m "
if seconds > 0:
message += str(seconds) + "s"
if not message:
message = "0s"

return message


def videoDurationInThisPath(path):
Expand All @@ -37,10 +43,7 @@ def videoDurationInThisPath(path):
durationList = []
for video in videoList:
video_path = os.path.join(path, video)
duration = subprocess.Popen([resource_path('ffprobe'), '-v', 'error', '-show_entries', 'format=duration', '-of',
'default=noprint_wrappers=1:nokey=1', video_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
duration = duration.stdout.read().decode('utf-8')
duration = float(duration)
duration = get_video_duration(video_path)
durationList.append(duration)
# Sum up the duration of all the video files
durationSum = sum(durationList)
Expand Down

0 comments on commit 733b32d

Please sign in to comment.