File size: 2,822 Bytes
c4d6ad3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
"""
Funkce pro extrakci technických a EXIF metadat.
"""
from datetime import datetime
import os

def extract_video_metadata(video_path):
    """
    Extract technical metadata from video file
    Returns dictionary of metadata
    """
    import cv2
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        return None
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = cap.get(cv2.CAP_PROP_FPS)
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    duration = frame_count / fps
    cap.release()

    file_stats = os.stat(video_path)
    return {
        'filename': os.path.basename(video_path),
        'path': video_path,
        'resolution': f"{width}x{height}",
        'fps': fps,
        'duration': duration,
        'frame_count': frame_count,
        'size': file_stats.st_size,
        'created': datetime.fromtimestamp(file_stats.st_ctime),
        'modified': datetime.fromtimestamp(file_stats.st_mtime)
    }


def extract_audio_metadata(audio_path):
    """
    Extract technical metadata from audio file
    Returns dictionary of metadata
    """
    import wave
    try:
        with wave.open(audio_path, 'rb') as audio_file:
            channels = audio_file.getnchannels()
            sample_width = audio_file.getsampwidth()
            framerate = audio_file.getframerate()
            frames = audio_file.getnframes()
            duration = frames / float(framerate)
        file_stats = os.stat(audio_path)
        return {
            'filename': os.path.basename(audio_path),
            'path': audio_path,
            'channels': channels,
            'sample_width': sample_width,
            'sample_rate': framerate,
            'duration': duration,
            'size': file_stats.st_size,
            'created': datetime.fromtimestamp(file_stats.st_ctime),
            'modified': datetime.fromtimestamp(file_stats.st_mtime)
        }
    except:
        return None


def extract_exif_data(image_path):
    """
    Extract EXIF metadata from image file
    Returns dictionary of EXIF data
    """
    from PIL import Image, ExifTags
    try:
        img = Image.open(image_path)
        exif_data = img._getexif()
        if not exif_data:
            return None
        exif = {}
        for tag, value in exif_data.items():
            decoded = ExifTags.TAGS.get(tag, tag)
            exif[decoded] = value
        file_stats = os.stat(image_path)
        exif.update({
            'filename': os.path.basename(image_path),
            'path': image_path,
            'size': file_stats.st_size,
            'created': datetime.fromtimestamp(file_stats.st_ctime),
            'modified': datetime.fromtimestamp(file_stats.st_mtime)
        })
        return exif
    except:
        return None