added transcoding to mp4 in import to ensure web playback.

This commit is contained in:
Bryan Van Deusen
2026-01-20 16:10:19 -05:00
parent cb3897c0b0
commit 87bcb633f6
4 changed files with 175 additions and 31 deletions
+35 -8
View File
@@ -48,7 +48,8 @@ def import_media_file(self, task_id: int):
extract_metadata, calculate_hash, calculate_perceptual_hash,
is_mostly_transparent, is_mostly_single_color, find_similar_image,
build_hashed_dest_path, load_import_settings, supersede_image,
generate_thumbnail, generate_video_thumbnail_mirrored
generate_thumbnail, generate_video_thumbnail_mirrored,
transcode_video_to_mp4, needs_transcode
)
from app.utils.metadata_enrichment import find_sidecar_json
@@ -149,8 +150,33 @@ def import_media_file(self, task_id: int):
# Normal import - copy to destination
target_dir = os.path.join(dest_dir, artist)
os.makedirs(target_dir, exist_ok=True)
dest_path = build_hashed_dest_path(target_dir, filename, file_hash)
shutil.copy2(src_path, dest_path)
# Transcode video if needed (non-MP4 formats to H.264 MP4)
transcoded_temp = None
actual_src = src_path
actual_filename = filename
if needs_transcode(src_path):
log.info(f"Transcoding video: {filename}")
# Transcode to temp location first
transcoded_temp = transcode_video_to_mp4(src_path)
if transcoded_temp:
actual_src = transcoded_temp
# Update filename to .mp4 extension
actual_filename = os.path.splitext(filename)[0] + '.mp4'
# Recalculate hash for transcoded file
file_hash = calculate_hash(transcoded_temp)
task.file_hash = file_hash
log.info(f"Transcoded successfully: {actual_filename}")
else:
log.warning(f"Transcode failed for {filename}, importing original")
dest_path = build_hashed_dest_path(target_dir, actual_filename, file_hash)
shutil.copy2(actual_src, dest_path)
# Clean up transcoded temp file
if transcoded_temp and os.path.exists(transcoded_temp):
os.remove(transcoded_temp)
# Generate thumbnail
try:
@@ -159,20 +185,21 @@ def import_media_file(self, task_id: int):
else:
thumb_path = generate_thumbnail(dest_path)
except Exception as e:
log.warning(f"Thumbnail generation failed for {filename}: {e}")
log.warning(f"Thumbnail generation failed for {actual_filename}: {e}")
thumb_path = None
# Create ImageRecord
# Use actual_filename in case video was transcoded to .mp4
record = ImageRecord(
filename=filename,
filename=actual_filename,
filepath=dest_path,
thumb_path=thumb_path,
hash=file_hash,
perceptual_hash=str(phash) if phash else None,
file_size=metadata['file_size'],
file_size=os.path.getsize(dest_path), # Use actual file size after transcode
width=metadata['width'],
height=metadata['height'],
format=metadata['format'],
format=metadata['format'] if not needs_transcode(src_path) else 'mp4',
camera_model=metadata.get('camera_model'),
taken_at=metadata.get('taken_at'),
imported_at=datetime.utcnow()
@@ -200,7 +227,7 @@ def import_media_file(self, task_id: int):
from app.tasks.scan import update_batch_stats
update_batch_stats.delay(task.batch_id)
log.info(f"Imported: {filename} -> {dest_path}")
log.info(f"Imported: {actual_filename} -> {dest_path}")
return {
'status': 'imported',