Skip to content

Commit bbe6758

Browse files
sohei-tclaude
andcommitted
refactor: extract shared utilities and improve code structure
- Create common/utils.py with FFmpegLocator, MediaFileValidator, MediaFormatter, FFmpegRunner - Update audio-extractor, video-compressor, video-speed-changer, frame-extractor to use shared utilities - Add comprehensive type hints across modified modules using from __future__ import annotations - Add 41 tests for common utilities (tests/test_common.py) - Update pyproject.toml with build-system, dependencies, optional-dependencies, and entry points - Update existing test_video_speed_changer.py to use shared MediaFileValidator Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a553a60 commit bbe6758

9 files changed

Lines changed: 980 additions & 231 deletions

File tree

audio-extractor/audio_extractor.py

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,58 +15,24 @@
1515
Original: 動画からmp3抽出.py
1616
"""
1717

18+
from __future__ import annotations
19+
1820
import argparse
19-
import os
20-
import shutil
2121
import subprocess
2222
import sys
2323
from pathlib import Path
24-
from typing import List, Optional
25-
26-
27-
def find_ffmpeg() -> Optional[str]:
28-
"""ffmpegのパスを検出"""
29-
# 環境のPATHから検索
30-
ffmpeg_path = shutil.which("ffmpeg")
31-
if ffmpeg_path:
32-
return ffmpeg_path
33-
34-
# よくあるインストール場所を確認
35-
common_paths = [
36-
"/usr/local/bin/ffmpeg",
37-
"/opt/homebrew/bin/ffmpeg",
38-
"/usr/bin/ffmpeg",
39-
]
40-
for path in common_paths:
41-
if os.path.isfile(path):
42-
return path
43-
44-
return None
45-
46-
47-
def get_video_files(path: Path) -> List[Path]:
48-
"""指定パスから動画ファイルを取得"""
49-
extensions = {".mp4", ".avi", ".mkv", ".mov", ".flv", ".wmv", ".webm", ".m4v"}
50-
51-
if path.is_file():
52-
if path.suffix.lower() in extensions:
53-
return [path]
54-
return []
5524

56-
# ディレクトリの場合
57-
videos = []
58-
for file in path.iterdir():
59-
if file.is_file() and file.suffix.lower() in extensions:
60-
videos.append(file)
25+
# Add parent directory to path for common module imports
26+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
6127

62-
return sorted(videos)
28+
from common.utils import FFmpegLocator, MediaFileValidator
6329

6430

6531
def extract_audio(
6632
ffmpeg_path: str,
6733
input_file: Path,
6834
output_file: Path,
69-
bitrate: Optional[int] = None,
35+
bitrate: int | None = None,
7036
quality: int = 2,
7137
) -> bool:
7238
"""動画から音声を抽出"""
@@ -165,7 +131,7 @@ def main():
165131
args = parser.parse_args()
166132

167133
# ffmpegを検出
168-
ffmpeg_path = args.ffmpeg or find_ffmpeg()
134+
ffmpeg_path = args.ffmpeg or FFmpegLocator.find_ffmpeg()
169135
if not ffmpeg_path:
170136
print("エラー: ffmpegが見つかりません")
171137
print("インストール: brew install ffmpeg (macOS)")
@@ -180,15 +146,15 @@ def main():
180146
if not input_path.exists():
181147
print(f"警告: ファイルが見つかりません: {input_path}")
182148
continue
183-
video_files.extend(get_video_files(input_path))
149+
video_files.extend(MediaFileValidator.get_video_files(input_path))
184150
elif args.input_dir:
185151
if not args.input_dir.exists():
186152
print(f"エラー: ディレクトリが存在しません: {args.input_dir}")
187153
sys.exit(1)
188-
video_files = get_video_files(args.input_dir)
154+
video_files = MediaFileValidator.get_video_files(args.input_dir)
189155
else:
190156
# カレントディレクトリから検索
191-
video_files = get_video_files(Path.cwd())
157+
video_files = MediaFileValidator.get_video_files(Path.cwd())
192158

193159
if not video_files:
194160
print("エラー: 動画ファイルが見つかりません")

common/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Shared utilities for python-video-tools.
3+
4+
This package provides common classes and functions used across multiple
5+
video/audio/image processing tools in this project, reducing code duplication
6+
and ensuring consistent behavior.
7+
"""
8+
9+
from common.utils import (
10+
FFmpegLocator,
11+
FFmpegRunner,
12+
MediaFileValidator,
13+
MediaFormatter,
14+
)
15+
16+
__all__ = [
17+
"FFmpegLocator",
18+
"FFmpegRunner",
19+
"MediaFileValidator",
20+
"MediaFormatter",
21+
]

0 commit comments

Comments
 (0)