File size: 2,545 Bytes
4d96a11
 
4cd8c66
d4e3b06
4cd8c66
 
d4e3b06
4d96a11
 
 
 
d4e3b06
4d96a11
 
9c75488
d4e3b06
4cd8c66
 
d4e3b06
4cd8c66
d4e3b06
 
 
4d96a11
 
4cd8c66
9c75488
4cd8c66
d4e3b06
 
 
4cd8c66
d4e3b06
4cd8c66
d4e3b06
4cd8c66
 
 
d4e3b06
 
4cd8c66
 
 
d4e3b06
4cd8c66
 
 
 
 
 
4d96a11
4cd8c66
 
4d96a11
4cd8c66
 
 
9c75488
4cd8c66
 
 
 
9c75488
4cd8c66
 
9c75488
4d96a11
4cd8c66
 
116ad44
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
from pathlib import Path
import yt_dlp
import tempfile
import streamlit as st
from video_processor import VideoProcessor
from audio_processor import AudioProcessor
import re
import os


class LinkDownloader:
    def __init__(self, output_dir):
        self.output_dir = Path(output_dir)
        self.output_dir.mkdir(parents=True, exist_ok=True)

    def sanitize_filename(self, title):
        """Sanitize filename by removing special characters and limiting length"""
        # Replace special characters with underscore
        clean_name = re.sub(r'[^a-zA-Z0-9.]', '_', title)
        # Limit length
        if len(clean_name) > 50:
            clean_name = clean_name[:50]
        return clean_name

    def download_from_url(self, url):
        """Download video from URL using yt-dlp"""
        try:
            # Create a temporary filename template
            temp_filename = 'downloaded_video.%(ext)s'
            temp_filepath = str(self.output_dir / temp_filename)

            # Configure yt-dlp options
            ydl_opts = {
                'format': 'best',
                'outtmpl': temp_filepath,
                'quiet': False,  # Enable output for debugging
                'no_warnings': False,
                'extract_audio': False,
            }

            st.info("Starting video download...")
            print(f"Download directory: {self.output_dir}")
            print(f"Download template: {temp_filepath}")

            # Download the video
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                # Extract info first
                info = ydl.extract_info(url, download=False)
                # Get the actual extension
                ext = info.get('ext', 'mp4')

                # Download the video
                ydl.download([url])

                # Construct the actual file path
                actual_filepath = str(self.output_dir / f"downloaded_video.{ext}")
                print(f"Expected downloaded file: {actual_filepath}")

                # Verify file exists
                if not os.path.exists(actual_filepath):
                    print(f"Files in directory: {os.listdir(self.output_dir)}")
                    raise FileNotFoundError(f"Downloaded file not found at {actual_filepath}")

                print(f"File size: {os.path.getsize(actual_filepath)} bytes")
                return actual_filepath

        except Exception as e:
            st.error(f"Error during download: {str(e)}")
            print(f"Download error details: {str(e)}")
            return None