因为写了一个相册系统,所以发现这个问题,这年头谁还只是傻傻的照相啊,肯定是外带拍N多的视频啦。好吧,代码再改改,添加上传视频的功能。

只是说起来简单,做起来啰嗦。这视频不如照片信息的格式统一,不过还好,基本上应付市面上机器拍摄的视频信息提取函数也大概写出来了。

这是基于FFMpeg的,所以不管是Windows还是Linux都是安装FFMpeg才行。

import subprocess
import json

def get_video_metadata_ffmpeg(file_path):
    cmd = [
        'ffprobe',
        '-v', 'quiet',
        '-print_format', 'json',
        '-show_format',
        '-show_streams',
        file_path
    ]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    if result.returncode != 0:
        return None
    
    metadata = json.loads(result.stdout)
    
    # 提取关键信息
    info = {
        'creation_time': metadata.get('format', {}).get('tags', {}).get('creation_time'),
        'location': None,
        'duration': metadata.get('format', {}).get('duration'),
        'format': metadata.get('format', {}).get('format_name')
    }
    
    # 检查视频流中的位置信息
    for stream in metadata.get('streams', []):
        if stream.get('tags', {}).get('location'):
            info['location'] = stream['tags']['location']
            break
            
    return info

metadata = get_video_metadata_ffmpeg('test.mp4')
print(metadata)

然后PHP的

function getVideoMetadataFFmpeg($filePath) {
    $cmd = "ffprobe -v quiet -print_format json -show_format -show_streams " . escapeshellarg($filePath);
    $output = shell_exec($cmd);
    $metadata = json_decode($output, true);

    $info = [
        'creation_time' => $metadata['format']['tags']['creation_time'] ?? null,
        'location' => null,
        'duration' => $metadata['format']['duration'] ?? null,
        'format' => $metadata['format']['format_name'] ?? null
    ];

    foreach ($metadata['streams'] ?? [] as $stream) {
        if (isset($stream['tags']['location'])) {
            $info['location'] = $stream['tags']['location'];
            break;
        }
    }

    return $info;
}

$metadata = getVideoMetadataFFmpeg('test.mp4');
print_r($metadata);

另外查资料发现了exiftool这个工具,也整理一下

import subprocess
import json

def get_video_metadata_exiftool(file_path):
    cmd = ['exiftool', '-json', '-gps*', '-createdate', '-track*', file_path]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    if result.returncode != 0:
        return None
    
    metadata = json.loads(result.stdout)[0]
    
    return {
        'creation_date': metadata.get('CreateDate'),
        'gps_latitude': metadata.get('GPSLatitude'),
        'gps_longitude': metadata.get('GPSLongitude'),
        'make': metadata.get('Make'),
        'model': metadata.get('Model')
    }

metadata = get_video_metadata_exiftool('test.mp4')
print(metadata)

PHP的

function getVideoMetadataExiftool($filePath) {
    $cmd = "exiftool -json " . escapeshellarg($filePath);
    $output = shell_exec($cmd);
    $metadata = json_decode($output, true)[0];

    return [
        'creation_date' => $metadata['CreateDate'] ?? null,
        'gps_latitude' => $metadata['GPSLatitude'] ?? null,
        'gps_longitude' => $metadata['GPSLongitude'] ?? null,
        'make' => $metadata['Make'] ?? null,
        'model' => $metadata['Model'] ?? null
    ];
}

$metadata = getVideoMetadataExiftool('test.mp4');
print_r($metadata);