aboutsummaryrefslogtreecommitdiffstats
path: root/youtube/ytdlp_integration.py
blob: f520e64b2a5e57bcd39bfa328fe0869ea0d3c21b (plain)
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
#!/usr/bin/env python3
"""
yt-dlp integration wrapper for backward compatibility.

This module now uses the centralized ytdlp_service for all operations.
"""
import logging
from youtube.ytdlp_service import (
    extract_video_info,
    get_language_name,
    clear_cache,
    get_cache_info,
)

logger = logging.getLogger(__name__)


def extract_video_info_ytdlp(video_id):
    """
    Extract video information using yt-dlp (with caching).

    This is a wrapper around ytdlp_service.extract_video_info()
    for backward compatibility.

    Args:
        video_id: YouTube video ID

    Returns:
        Dictionary with audio_tracks, formats, title, duration
    """
    logger.debug(f'Extracting video info (legacy API): {video_id}')

    info = extract_video_info(video_id)

    # Convert to legacy format for backward compatibility
    return {
        'audio_tracks': info.get('audio_tracks', []),
        'all_audio_formats': info.get('formats', []),
        'formats': info.get('formats', []),
        'title': info.get('title', ''),
        'duration': info.get('duration', 0),
        'error': info.get('error'),
    }


def get_audio_formats_for_language(video_id, language='en'):
    """
    Get available audio formats for a specific language.

    Args:
        video_id: YouTube video ID
        language: Language code (default: 'en')

    Returns:
        List of audio format dicts
    """
    info = extract_video_info_ytdlp(video_id)

    if 'error' in info:
        logger.warning(f'Cannot get audio formats: {info["error"]}')
        return []

    audio_formats = []
    for track in info.get('audio_tracks', []):
        if track['language'] == language:
            audio_formats.append(track)

    logger.debug(f'Found {len(audio_formats)} {language} audio formats')
    return audio_formats


__all__ = [
    'extract_video_info_ytdlp',
    'get_audio_formats_for_language',
    'get_language_name',
    'clear_cache',
    'get_cache_info',
]