Skip to content

anbeime/skill / skills/video-creation-collaborator/video-creation-collaborator

video-creation-collaborator

Verified

内容创作者与自媒体创作者在制作短视频时,若遇生图畸形、合成瑕疵或协同混乱,当需规范化多智能体流程时必用此技能。一键调度11个AI智能体,自动完成文案、分镜、音视频到视频合成的5阶段闭环,前置质检拦截瑕疵,高效产出统一可控的高质量短视频。

scripts/video_quality_checker.py

View on GitHub
#!/usr/bin/env python3
"""
视频质量检测工具
用于检测短视频成片的质量,包括分辨率、帧率、音画同步、字幕遮挡等问题
"""

import os
import sys
from typing import Dict, Tuple, Any, List


def check_video_quality(video_path: str,
                       expected_resolution: Tuple[int, int] = (1920, 1080),
                       expected_duration: int = 30,
                       expected_fps: int = 25,
                       min_bitrate: float = 8000000,
                       check_av_sync: bool = True,
                       max_av_sync_offset: float = 0.1) -> Dict[str, Any]:
    """
    检测视频质量

    参数:
        video_path: 视频文件路径
        expected_resolution: 期望分辨率 (宽, 高)
        expected_duration: 期望时长(秒)
        expected_fps: 期望帧率
        min_bitrate: 最小码率
        check_av_sync: 是否检测音画同步
        max_av_sync_offset: 最大音画偏移量(秒)

    返回:
        检测结果字典,包含各项指标的状态和详细信息
    """
    try:
        import cv2
        from moviepy.editor import VideoFileClip
        import numpy as np
    except ImportError as e:
        return {
            'success': False,
            'error': f'缺少必要依赖库: {str(e)}。请先安装: pip install opencv-python moviepy numpy'
        }

    # 检查文件是否存在
    if not os.path.exists(video_path):
        return {
            'success': False,
            'error': f'视频文件不存在: {video_path}'
        }

    results = {
        'success': True,
        'file_path': video_path,
        'file_size_mb': 0,
        'resolution': {},
        'duration': {},
        'fps': {},
        'bitrate': {},
        'video_quality': {},
        'audio': {},
        'av_sync': {},
        'subtitle': {},
        'overall': False
    }

    try:
        # 获取文件大小
        file_size = os.path.getsize(video_path) / (1024 * 1024)
        results['file_size_mb'] = round(file_size, 2)

        # 使用OpenCV检测视频信息
        cap = cv2.VideoCapture(video_path)
        if not cap.isOpened():
            raise Exception("无法打开视频文件")

        # 检测分辨率
        width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        actual_resolution = (width, height)
        resolution_match = actual_resolution == expected_resolution

        results['resolution'] = {
            'expected': expected_resolution,
            'actual': actual_resolution,
            'match': resolution_match
        }

        # 检测帧率
        actual_fps = int(cap.get(cv2.CAP_PROP_FPS))
        fps_match = abs(actual_fps - expected_fps) <= 1  # 允许1帧误差

        results['fps'] = {
            'expected': expected_fps,
            'actual': actual_fps,
            'match': fps_match
        }

        # 检测时长
        total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        actual_duration = total_frames / actual_fps if actual_fps > 0 else 0
        duration_match = abs(actual_duration - expected_duration) <= 1.0  # 允许1秒误差

        results['duration'] = {
            'expected': expected_duration,
            'actual': round(actual_duration, 1),
            'match': duration_match
        }

        cap.release()

        # 使用MoviePy检测音频和详细信息
        try:
            clip = VideoFileClip(video_path)

            # 检测码率
            actual_bitrate = (file_size * 8 * 1024) / actual_duration if actual_duration > 0 else 0
            bitrate_match = actual_bitrate >= min_bitrate

            results['bitrate'] = {
                'expected': min_bitrate,
                'actual': int(actual_bitrate),
                'unit': 'bps',
                'match': bitrate_match
            }

            # 检测音频
            if clip.audio is not None:
                audio_sample_rate = int(clip.audio.fps)
                audio_channels = clip.audio.nchannels
                audio_duration = clip.audio.duration

                audio_duration_match = abs(audio_duration - actual_duration) <= 0.1

                results['audio'] = {
                    'has_audio': True,
                    'sample_rate': audio_sample_rate,
                    'channels': audio_channels,
                    'duration': round(audio_duration, 2),
                    'sync_match': audio_duration_match,
                    'match': True
                }

                # 检测音画同步(简单版本:对比音频和视频时长)
                if check_av_sync:
                    av_offset = abs(audio_duration - actual_duration)
                    av_sync_match = av_offset <= max_av_sync_offset

                    results['av_sync'] = {
                        'offset': round(av_offset, 3),
                        'threshold': max_av_sync_offset,
                        'match': av_sync_match
                    }
            else:
                results['audio'] = {
                    'has_audio': False,
                    'match': False
                }

            # 检测画面质量(采样检测)
            try:
                # 随机采样10帧检测卡顿和画面撕裂
                sample_indices = np.linspace(0, total_frames - 1, min(10, total_frames), dtype=int)
                frame_issues = []

                for i in sample_indices:
                    clip.save_frame(f"/tmp/frame_{i}.jpg", t=(i / actual_fps) if actual_fps > 0 else 0)

                # 这里可以添加更复杂的画面质量检测
                # 例如检测黑帧、卡顿、撕裂等
                video_quality_match = True

                results['video_quality'] = {
                    'sampled_frames': len(sample_indices),
                    'issues': frame_issues,
                    'match': video_quality_match
                }

            except Exception as e:
                results['video_quality'] = {
                    'error': str(e),
                    'match': False
                }

            # 检测字幕(简单版本:检测是否有字幕文件或画面中的文字区域)
            # 实际项目中需要更复杂的OCR或字幕文件解析
            results['subtitle'] = {
                'detected': False,  # 默认未检测到字幕
                'blocking_check': 'not_implemented',  # 字幕遮挡检测需要额外实现
                'match': True  # 暂时通过
            }

            clip.close()

        except Exception as e:
            results['audio'] = {
                'error': str(e),
                'match': False
            }

        # 判定整体是否合格
        all_checks = [
            results['resolution']['match'],
            results['duration']['match'],
            results['fps']['match'],
            results['bitrate']['match'],
            results['audio'].get('match', False),
            results['video_quality'].get('match', True)
        ]

        if check_av_sync and results.get('av_sync'):
            all_checks.append(results['av_sync']['match'])

        results['overall'] = all(all_checks)

        # 收集问题
        issues = []
        if not results['resolution']['match']:
            issues.append(f"分辨率不符合要求: 期望{expected_resolution}, 实际{actual_resolution}")
        if not results['duration']['match']:
            issues.append(f"时长不符合要求: 期望{expected_duration}秒, 实际{actual_duration}秒")
        if not results['fps']['match']:
            issues.append(f"帧率不符合要求: 期望{expected_fps}fps, 实际{actual_fps}fps")
        if not results['bitrate']['match']:
            issues.append(f"码率过低: {results['bitrate']['actual']}bps低于阈值{min_bitrate}bps")
        if not results['audio'].get('match', False):
            if not results['audio'].get('has_audio', True):
                issues.append("缺少音频轨道")
            elif not results['audio'].get('sync_match', True):
                issues.append(f"音频时长不匹配,可能存在音画不同步")
        if check_av_sync and not results.get('av_sync', {}).get('match', True):
            issues.append(f"音画不同步: 偏移量{results['av_sync']['offset']}秒超过阈值{max_av_sync_offset}秒")

        results['issues'] = issues

        return results

    except Exception as e:
        return {
            'success': False,
            'error': f'检测过程中发生错误: {str(e)}'
        }


def print_quality_report(results: Dict[str, Any]):
    """
    打印质量检测报告

    参数:
        results: check_video_quality() 返回的检测结果
    """
    if not results['success']:
        print(f"❌ 检测失败: {results.get('error', '未知错误')}")
        return

    print("=" * 60)
    print("视频质量检测报告")
    print("=" * 60)
    print(f"文件路径: {results['file_path']}")
    print(f"文件大小: {results['file_size_mb']} MB")
    print()

    # 分辨率
    res = results['resolution']
    status = "✅ 通过" if res['match'] else "❌ 不合格"
    print(f"分辨率: {status}")
    print(f"  期望: {res['expected'][0]}x{res['expected'][1]}")
    print(f"  实际: {res['actual'][0]}x{res['actual'][1]}")
    print()

    # 时长
    dur = results['duration']
    status = "✅ 通过" if dur['match'] else "❌ 不合格"
    print(f"时长: {status}")
    print(f"  期望: {dur['expected']}秒")
    print(f"  实际: {dur['actual']}秒")
    print()

    # 帧率
    fps = results['fps']
    status = "✅ 通过" if fps['match'] else "❌ 不合格"
    print(f"帧率: {status}")
    print(f"  期望: {fps['expected']} fps")
    print(f"  实际: {fps['actual']} fps")
    print()

    # 码率
    bitrate = results['bitrate']
    status = "✅ 通过" if bitrate['match'] else "❌ 不合格"
    print(f"码率: {status}")
    print(f"  期望: ≥{bitrate['expected']} bps")
    print(f"  实际: {bitrate['actual']} bps")
    print()

    # 音频
    audio = results['audio']
    if 'error' in audio:
        print(f"音频: ❌ 检测失败 - {audio['error']}")
    elif not audio.get('has_audio', True):
        print(f"音频: ❌ 未检测到音频轨道")
    else:
        status = "✅ 通过" if audio['match'] else "❌ 不合格"
        print(f"音频: {status}")
        print(f"  采样率: {audio['sample_rate']} Hz")
        print(f"  声道数: {audio['channels']}")
        print(f"  时长: {audio['duration']}秒")
        if not audio.get('sync_match', True):
            print(f"  ⚠️  音频时长与视频不匹配")
    print()

    # 音画同步
    if 'av_sync' in results and 'error' not in results['av_sync']:
        av_sync = results['av_sync']
        status = "✅ 通过" if av_sync['match'] else "❌ 不合格"
        print(f"音画同步: {status}")
        print(f"  偏移量: {av_sync['offset']}秒")
        print(f"  阈值: {av_sync['threshold']}秒")
        print()

    # 画面质量
    video_q = results['video_quality']
    if 'error' in video_q:
        print(f"画面质量: ❌ 检测失败 - {video_q['error']}")
    else:
        status = "✅ 通过" if video_q['match'] else "❌ 不合格"
        print(f"画面质量: {status}")
        print(f"  采样帧数: {video_q['sampled_frames']}")
        if video_q['issues']:
            print(f"  问题: {', '.join(video_q['issues'])}")
    print()

    # 总体结果
    overall_status = "✅ 全部合格" if results['overall'] else "❌ 存在不合格项"
    print("=" * 60)
    print(f"总体结果: {overall_status}")
    print("=" * 60)

    if not results['overall']:
        print("\n问题列表:")
        for i, issue in enumerate(results.get('issues', []), 1):
            print(f"  {i}. {issue}")


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='视频质量检测工具')
    parser.add_argument('video_path', type=str, help='视频文件路径')
    parser.add_argument('--resolution', type=str, default='1920x1080',
                       help='期望分辨率 (格式: WIDTHxHEIGHT)')
    parser.add_argument('--duration', type=int, default=30,
                       help='期望时长(秒)')
    parser.add_argument('--fps', type=int, default=25,
                       help='期望帧率')
    parser.add_argument('--min-bitrate', type=float, default=8000000,
                       help='最小码率')
    parser.add_argument('--no-av-sync-check', action='store_true',
                       help='跳过音画同步检测')
    parser.add_argument('--max-av-offset', type=float, default=0.1,
                       help='最大音画偏移量(秒)')

    args = parser.parse_args()

    # 解析分辨率
    try:
        width, height = map(int, args.resolution.split('x'))
        resolution = (width, height)
    except:
        print("错误: 分辨率格式不正确,应为 WIDTHxHEIGHT (例如: 1920x1080)")
        sys.exit(1)

    # 执行检测
    results = check_video_quality(
        video_path=args.video_path,
        expected_resolution=resolution,
        expected_duration=args.duration,
        expected_fps=args.fps,
        min_bitrate=args.min_bitrate,
        check_av_sync=not args.no_av_sync_check,
        max_av_sync_offset=args.max_av_offset
    )

    # 打印报告
    print_quality_report(results)

    # 返回状态码
    sys.exit(0 if results.get('success', False) and results.get('overall', False) else 1)
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall