-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
38 lines (34 loc) · 1.28 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import cv2
import numpy as np
from datetime import timedelta
import subprocess
def format_timestamp(seconds):
"""Convert seconds to HH:MM:SS.mmm format"""
td = timedelta(seconds=seconds)
hours = td.seconds // 3600
minutes = (td.seconds % 3600) // 60
seconds = td.seconds % 60
milliseconds = round(seconds % 1 * 1000)
return f"{hours:02d}:{minutes:02d}:{int(seconds):02d}.{milliseconds:03d}"
def parse_timestamp(timestamp):
"""Convert HH:MM:SS.mmm format to seconds"""
hours, minutes, seconds = timestamp.split(':')
seconds, milliseconds = seconds.split('.')
total_seconds = (
int(hours) * 3600 +
int(minutes) * 60 +
int(seconds) +
int(milliseconds) / 1000
)
return total_seconds
def has_audio(video_file):
"""Check if a video file has an audio stream using ffmpeg"""
cmd = ['ffprobe', '-i', video_file, '-show_streams', '-select_streams', 'a', '-loglevel', 'error']
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return bool(result.stdout)
def detect_text(image):
"""Detect and count text in image using pytesseract"""
import pytesseract
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
text = pytesseract.image_to_string(gray)
return len(text.strip())