Spaces:
Sleeping
Sleeping
created video parser
Browse files- video_parser.py +64 -0
video_parser.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import requests
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
from urllib.parse import urlparse, parse_qs
|
| 6 |
+
import yt_dlp
|
| 7 |
+
|
| 8 |
+
class VideoParser:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.temp_dir = tempfile.mkdtemp()
|
| 11 |
+
|
| 12 |
+
def download_youtube_video(self, url: str) -> str:
|
| 13 |
+
"""Download YouTube video and return local path"""
|
| 14 |
+
ydl_opts = {
|
| 15 |
+
'format': 'best[height<=720]',
|
| 16 |
+
'outtmpl': os.path.join(self.temp_dir, '%(title)s.%(ext)s'),
|
| 17 |
+
'quiet': True
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 21 |
+
info = ydl.extract_info(url, download=True)
|
| 22 |
+
return ydl.prepare_filename(info)
|
| 23 |
+
|
| 24 |
+
def analyze_video_frames(self, video_path: str, sample_rate: int = 30):
|
| 25 |
+
"""Analyze video frames for object detection/counting"""
|
| 26 |
+
cap = cv2.VideoCapture(video_path)
|
| 27 |
+
frame_count = 0
|
| 28 |
+
results = []
|
| 29 |
+
|
| 30 |
+
while cap.isOpened():
|
| 31 |
+
ret, frame = cap.read()
|
| 32 |
+
if not ret:
|
| 33 |
+
break
|
| 34 |
+
|
| 35 |
+
if frame_count % sample_rate == 0:
|
| 36 |
+
# Basic frame analysis - you'd integrate with object detection here
|
| 37 |
+
results.append({
|
| 38 |
+
'frame': frame_count,
|
| 39 |
+
'timestamp': frame_count / cap.get(cv2.CAP_PROP_FPS),
|
| 40 |
+
'frame_data': frame
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
frame_count += 1
|
| 44 |
+
|
| 45 |
+
cap.release()
|
| 46 |
+
return results
|
| 47 |
+
|
| 48 |
+
def extract_audio(self, video_path: str) -> str:
|
| 49 |
+
"""Extract audio from video for speech analysis"""
|
| 50 |
+
audio_path = video_path.rsplit('.', 1)[0] + '.wav'
|
| 51 |
+
|
| 52 |
+
# Use ffmpeg to extract audio
|
| 53 |
+
import subprocess
|
| 54 |
+
subprocess.run([
|
| 55 |
+
'ffmpeg', '-i', video_path, '-vn', '-acodec', 'pcm_s16le',
|
| 56 |
+
'-ar', '16000', '-ac', '1', audio_path, '-y'
|
| 57 |
+
], capture_output=True)
|
| 58 |
+
|
| 59 |
+
return audio_path
|
| 60 |
+
|
| 61 |
+
def cleanup(self):
|
| 62 |
+
"""Clean up temporary files"""
|
| 63 |
+
import shutil
|
| 64 |
+
shutil.rmtree(self.temp_dir, ignore_errors=True)
|