-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_downloader.py
83 lines (69 loc) · 2.81 KB
/
video_downloader.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import requests
from pathlib import Path
import os
from datetime import datetime
from tqdm import tqdm
class VideoDownloader:
def __init__(self):
"""Initialize the Video Downloader"""
self.output_dir = Path(__file__).parent / "videos"
self.output_dir.mkdir(exist_ok=True) # Create videos directory if it doesn't exist
def download_video(self, url, filename=None):
"""
Download video from URL
Args:
url (str): URL of the video to download
filename (str, optional): Custom filename for the video
If None, uses timestamp
Returns:
Path: Path to the downloaded video file
"""
try:
# Generate filename if not provided
if filename is None:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"video_{timestamp}.mp4"
# Ensure filename has .mp4 extension
if not filename.endswith('.mp4'):
filename += '.mp4'
# Full path for the video
video_path = self.output_dir / filename
# Download the video
print(f"Downloading video from {url}")
response = requests.get(url, stream=True)
response.raise_for_status()
# Get total file size for progress tracking
total_size = int(response.headers.get('content-length', 0))
# Write the file with progress tracking
with open(video_path, 'wb') as file, tqdm(
desc=filename,
total=total_size,
unit='iB',
unit_scale=True,
unit_divisor=1024,
) as progress_bar:
for data in response.iter_content(chunk_size=1024):
size = file.write(data)
progress_bar.update(size)
print(f"Video downloaded successfully to: {video_path}")
return video_path
except Exception as e:
raise Exception(f"Failed to download video: {str(e)}")
def get_video_info(self, video_path):
"""
Get basic information about the downloaded video
Args:
video_path (Path): Path to the video file
Returns:
dict: Video information
"""
try:
file_size = os.path.getsize(video_path)
return {
'path': video_path,
'size_bytes': file_size,
'size_mb': file_size / (1024 * 1024), # Convert to MB
'filename': video_path.name
}
except Exception as e:
raise Exception(f"Failed to get video info: {str(e)}")