bluenevus commited on
Commit
64832c1
·
verified ·
1 Parent(s): eb57b1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -13,6 +13,7 @@ import mimetypes
13
  import urllib.parse
14
  import subprocess
15
  import json
 
16
 
17
  # Configure logging
18
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -100,25 +101,31 @@ def download_file(url):
100
  response = session.get(url, stream=True, allow_redirects=True)
101
  response.raise_for_status()
102
 
103
- # Check if we can get the filename from Content-Disposition header
104
- content_disposition = response.headers.get('Content-Disposition')
105
- if content_disposition:
106
- filename = re.findall("filename=(.+)", content_disposition)[0].strip('"')
107
- else:
108
- # If not, use a default name with .mp4 extension
109
- filename = 'downloaded_video.mp4'
110
 
111
  # Save the content to a temporary file with .mp4 extension
112
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_file:
 
113
  for chunk in response.iter_content(chunk_size=8192):
114
- if chunk:
115
- temp_file.write(chunk)
 
116
  temp_file_path = temp_file.name
117
 
 
 
 
 
 
 
118
  logger.info(f"File downloaded and saved as: {temp_file_path}")
 
119
  return temp_file_path
120
 
121
-
122
  def get_file_info(file_path):
123
  try:
124
  result = subprocess.run(['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', file_path],
 
13
  import urllib.parse
14
  import subprocess
15
  import json
16
+ from tqdm import tqdm
17
 
18
  # Configure logging
19
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
101
  response = session.get(url, stream=True, allow_redirects=True)
102
  response.raise_for_status()
103
 
104
+ # Get the total file size
105
+ total_size = int(response.headers.get('content-length', 0))
106
+
107
+ # Use a default name with .mp4 extension
108
+ filename = 'downloaded_video.mp4'
 
 
109
 
110
  # Save the content to a temporary file with .mp4 extension
111
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_file:
112
+ progress_bar = tqdm(total=total_size, unit='iB', unit_scale=True, desc=filename)
113
  for chunk in response.iter_content(chunk_size=8192):
114
+ size = temp_file.write(chunk)
115
+ progress_bar.update(size)
116
+ progress_bar.close()
117
  temp_file_path = temp_file.name
118
 
119
+ # Check if the downloaded file size matches the expected size
120
+ actual_size = os.path.getsize(temp_file_path)
121
+ if total_size != 0 and actual_size != total_size:
122
+ logger.error(f"Downloaded file size ({actual_size} bytes) does not match expected size ({total_size} bytes)")
123
+ raise Exception("Incomplete download")
124
+
125
  logger.info(f"File downloaded and saved as: {temp_file_path}")
126
+ logger.info(f"File size: {actual_size} bytes")
127
  return temp_file_path
128
 
 
129
  def get_file_info(file_path):
130
  try:
131
  result = subprocess.run(['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', file_path],