Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

feat(livephoto): 新增瀑布流实况照片预览,添加实况照片处理功能,支持批量处理和视频播放

Timothy Yinfb066e7e7931
diff --git a/app/components/masonry/PhotoItem.client.vue b/app/components/masonry/PhotoItem.client.vue
index 8980d0c6..8d809541 100644
--- a/app/components/masonry/PhotoItem.client.vue
+++ b/app/components/masonry/PhotoItem.client.vue
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { formatCameraInfo } from '~/utils/camera'
+import { motion, useDomRef } from 'motion-v'
interface Props {
photo: Photo
@@ -21,13 +22,25 @@ const ITEM_GAP = 4
const isLoading = ref(true)
const photoRef = ref<HTMLElement>()
const mainImageRef = ref<HTMLImageElement>()
+const videoRef = useDomRef()
const isVisible = ref(false)
const containerWidth = ref(0)
+// LivePhoto related state
+const isHovering = ref(false)
+const isVideoPlaying = ref(false)
+const isVideoLoaded = ref(false)
+const videoBlob = ref<Blob | null>(null)
+const videoBlobUrl = ref<string | null>(null)
+const { convertMovToMp4, getProcessingState } = useLivePhotoProcessor()
+
// Observers
const resizeObserverRef = ref<ResizeObserver | null>(null)
const intersectionObserverRef = ref<IntersectionObserver | null>(null)
+// LivePhoto processing state
+const processingState = getProcessingState(props.photo.id)
+
// Computed
const aspectRatio = computed(() => {
// Priority 1: Use aspectRatio from photo data if available
@@ -49,6 +62,14 @@ const containerHeight = computed(() => {
return Math.round(currentWidth * aspectRatio.value)
})
+// Show info overlay only when not playing video or video has finished
+const shouldShowInfoOverlay = computed(() => {
+ if (!props.photo.isLivePhoto) return true
+ if (!isHovering.value) return true
+ if (isVideoPlaying.value) return false
+ return isVideoLoaded.value
+})
+
// Methods
const handleImageLoad = (event: Event) => {
isLoading.value = false
@@ -66,6 +87,77 @@ const checkImageLoaded = (img: HTMLImageElement) => {
}
}
+// LivePhoto video handling
+const handleMouseEnter = async () => {
+ if (!props.photo.isLivePhoto || !props.photo.livePhotoVideoUrl) return
+
+ isHovering.value = true
+
+ // Only start video processing if we have the blob ready
+ if (videoBlob.value && videoBlobUrl.value) {
+ playLivePhotoVideo()
+ }
+}
+
+const handleMouseLeave = () => {
+ isHovering.value = false
+ if (videoRef.value && !videoRef.value.paused) {
+ videoRef.value.pause()
+ videoRef.value.currentTime = 0
+ }
+
+ // Use a slight delay for smoother transition when mouse leaves
+ setTimeout(() => {
+ if (!isHovering.value) { // Check again to avoid conflicts if mouse re-enters quickly
+ isVideoPlaying.value = false
+ }
+ }, 150)
+}
+
+const playLivePhotoVideo = () => {
+ if (!videoRef.value || !videoBlobUrl.value) return
+
+ videoRef.value.currentTime = 0
+
+ // Start the crossfade transition by setting video playing state first
+ isVideoPlaying.value = true
+
+ // Then play the video after a small delay to ensure smooth transition
+ nextTick(() => {
+ videoRef.value?.play().catch((error: any) => {
+ console.warn('Failed to play LivePhoto video:', error)
+ isVideoPlaying.value = false
+ })
+ })
+}
+
+const handleVideoEnded = () => {
+ // Add a small delay before hiding video to make the transition smoother
+ setTimeout(() => {
+ isVideoPlaying.value = false
+ }, 100)
+}
+
+// Process LivePhoto when it becomes visible
+const processLivePhotoWhenVisible = async () => {
+ if (!props.photo.isLivePhoto || !props.photo.livePhotoVideoUrl || !isVisible.value) return
+
+ try {
+ const blob = await convertMovToMp4(props.photo.livePhotoVideoUrl, props.photo.id)
+ if (blob) {
+ videoBlob.value = blob
+ // Clean up previous blob URL
+ if (videoBlobUrl.value) {
+ URL.revokeObjectURL(videoBlobUrl.value)
+ }
+ videoBlobUrl.value = URL.createObjectURL(blob)
+ isVideoLoaded.value = true
+ }
+ } catch (error) {
+ console.error('Failed to process LivePhoto:', error)
+ }
+}
+
const formatExposureTime = (
exposureTime: string | number | undefined,
): string => {
@@ -160,6 +252,13 @@ onMounted(() => {
isVisible: newVisibility,
date: props.photo.dateTaken || new Date().toISOString(),
})
+
+ // Process LivePhoto when it becomes visible
+ if (newVisibility) {
+ nextTick(() => {
+ processLivePhotoWhenVisible()
+ })
+ }
}
})
},
@@ -188,6 +287,11 @@ onUnmounted(() => {
if (intersectionObserverRef.value) {
intersectionObserverRef.value.disconnect()
}
+
+ // Clean up video blob URL
+ if (videoBlobUrl.value) {
+ URL.revokeObjectURL(videoBlobUrl.value)
+ }
})
</script>
@@ -199,6 +303,8 @@ onUnmounted(() => {
marginBottom: `${ITEM_GAP}px`,
}"
@click="emit('openViewer', props.index)"
+ @mouseenter="handleMouseEnter"
+ @mouseleave="handleMouseLeave"
>
<div class="relative group overflow-hidden transition-all duration-300">
<!-- Container with fixed aspect ratio -->
@@ -224,16 +330,45 @@ onUnmounted(() => {
class="absolute inset-0 w-full h-full bg-gray-200 dark:bg-gray-700 animate-pulse"
/>
- <!-- Main image -->
- <img
+ <!-- Main image with motion transition -->
+ <motion.img
ref="mainImageRef"
v-show="!isLoading"
:src="photo.thumbnailUrl || ''"
:alt="photo.title || photo.description || 'Photo'"
class="absolute inset-0 w-full h-full object-cover transition-transform duration-300 group-hover:scale-105"
+ loading="lazy"
+ :initial="{ opacity: 1 }"
+ :animate="{
+ opacity: (!isVideoPlaying || !photo.isLivePhoto) ? 1 : 0
+ }"
+ :transition="{
+ duration: 0.4,
+ ease: [0.25, 0.1, 0.25, 1] // Custom cubic-bezier for smooth fade
+ }"
@load="handleImageLoad"
@error="handleImageError"
- loading="lazy"
+ />
+
+ <!-- LivePhoto video with motion transition -->
+ <motion.video
+ v-if="photo.isLivePhoto && videoBlobUrl"
+ ref="videoRef"
+ :src="videoBlobUrl"
+ class="absolute inset-0 w-full h-full object-cover"
+ muted
+ playsinline
+ preload="metadata"
+ :initial="{ opacity: 0 }"
+ :animate="{
+ opacity: isVideoPlaying ? 1 : 0
+ }"
+ :transition="{
+ duration: 0.4,
+ ease: [0.25, 0.1, 0.25, 1],
+ delay: isVideoPlaying ? 0.1 : 0 // Slight delay when fading in video
+ }"
+ @ended="handleVideoEnded"
/>
</div>
@@ -242,9 +377,51 @@ onUnmounted(() => {
class="absolute inset-0 bg-black/0 group-hover:bg-black/20 transition-colors duration-300"
/>
+ <!-- Live Photo indicator -->
+ <motion.div
+ v-if="photo.isLivePhoto"
+ class="absolute top-2 left-2 backdrop-blur-md text-white rounded-full pl-1 pr-1.5 py-1 text-[13px] font-bold flex items-center gap-0.5 leading-0"
+ :animate="{
+ backgroundColor: isVideoPlaying ? 'rgba(0, 0, 0, 0.6)' : 'rgba(0, 0, 0, 0.3)',
+ scale: isVideoPlaying ? 1.05 : 1
+ }"
+ :transition="{
+ duration: 0.3,
+ ease: 'easeInOut'
+ }"
+ >
+ <Icon
+ name="tabler:live-photo"
+ class="size-[17px]"
+ :class="{ 'text-yellow-300': isVideoPlaying }"
+ />
+ <span :class="{ 'text-yellow-300': isVideoPlaying }">实况</span>
+
+ <!-- Processing progress indicator -->
+ <div
+ v-if="processingState?.isProcessing"
+ class="ml-1 flex items-center gap-1"
+ >
+ <div class="size-2 bg-white/70 rounded-full animate-pulse" />
+ <span class="text-[11px] text-white/80">
+ {{ Math.round(processingState.progress || 0) }}%
+ </span>
+ </div>
+ </motion.div>
+
<!-- Photo info overlay (bottom) -->
- <div
- class="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/60 to-transparent p-3 translate-y-full group-hover:translate-y-0 transition-transform duration-300"
+ <motion.div
+ v-show="shouldShowInfoOverlay"
+ class="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/60 to-transparent p-3"
+ :initial="{ y: '100%', opacity: 0 }"
+ :animate="{
+ y: isHovering && shouldShowInfoOverlay ? 0 : '100%',
+ opacity: isHovering && shouldShowInfoOverlay ? 1 : 0
+ }"
+ :transition="{
+ duration: 0.3,
+ ease: [0.25, 0.1, 0.25, 1]
+ }"
>
<div class="text-white flex flex-col gap-1">
<div class="flex flex-col">
@@ -341,7 +518,7 @@ onUnmounted(() => {
</div>
</div>
</div>
- </div>
+ </motion.div>
</div>
</div>
</template>
diff --git a/app/components/masonry/Root.vue b/app/components/masonry/Root.vue
index b22b3d92..4b18aa9f 100644
--- a/app/components/masonry/Root.vue
+++ b/app/components/masonry/Root.vue
@@ -27,6 +27,10 @@ const visiblePhotos = ref(new Set<number>())
// Composables
const isMobile = useMediaQuery('(max-width: 768px)')
+const { batchProcessLivePhotos } = useLivePhotoProcessor()
+
+// Track processed photos to avoid reprocessing
+const processedBatch = ref(new Set<string>())
// Computed
const columnCount = computed(() => {
@@ -95,6 +99,37 @@ const handleVisibilityChange = ({
visiblePhotos.value.delete(index)
}
updateDateRange()
+
+ // Process LivePhotos for visible photos
+ nextTick(() => {
+ processVisibleLivePhotos()
+ })
+}
+
+// Process LivePhotos for currently visible photos
+const processVisibleLivePhotos = async () => {
+ const visiblePhotosArray = Array.from(visiblePhotos.value)
+ const livePhotosToProcess = visiblePhotosArray
+ .map(index => props.photos[index])
+ .filter((photo): photo is Photo =>
+ photo != null &&
+ photo.isLivePhoto === 1 &&
+ Boolean(photo.livePhotoVideoUrl) &&
+ !processedBatch.value.has(photo.id)
+ )
+
+ if (livePhotosToProcess.length === 0) return
+
+ // Mark as processed to avoid reprocessing
+ livePhotosToProcess.forEach(photo => {
+ processedBatch.value.add(photo.id)
+ })
+
+ // Start background processing
+ batchProcessLivePhotos(livePhotosToProcess.map(photo => ({
+ id: photo.id,
+ livePhotoVideoUrl: photo.livePhotoVideoUrl!
+ })))
}
const visibleCities = ref<string>()
diff --git a/app/composables/useLivePhotoProcessor.ts b/app/composables/useLivePhotoProcessor.ts
new file mode 100644
index 00000000..c90a6221
--- /dev/null
+++ b/app/composables/useLivePhotoProcessor.ts
@@ -0,0 +1,149 @@
+import { ref, computed } from 'vue'
+
+interface LivePhotoProcessingState {
+ isProcessing: boolean
+ progress: number
+ mp4Blob: Blob | null
+ error: string | null
+}
+
+// 全局存储已转换的实况照片
+const processedLivePhotos = ref<Map<string, LivePhotoProcessingState>>(new Map())
+
+export const useLivePhotoProcessor = () => {
+ /**
+ * 将 MOV 文件转换为 MP4 blob
+ * @param movUrl MOV 文件的 URL
+ * @param photoId 照片 ID
+ */
+ const convertMovToMp4 = async (movUrl: string, photoId: string): Promise<Blob | null> => {
+ // 如果已经在处理或已经处理完成,直接返回
+ const existing = processedLivePhotos.value.get(photoId)
+ if (existing) {
+ if (existing.mp4Blob) {
+ return existing.mp4Blob
+ }
+ if (existing.isProcessing) {
+ // 等待处理完成
+ return new Promise((resolve) => {
+ const checkInterval = setInterval(() => {
+ const current = processedLivePhotos.value.get(photoId)
+ if (current && !current.isProcessing) {
+ clearInterval(checkInterval)
+ resolve(current.mp4Blob)
+ }
+ }, 100)
+ })
+ }
+ }
+
+ // 初始化处理状态
+ const state: LivePhotoProcessingState = {
+ isProcessing: true,
+ progress: 0,
+ mp4Blob: null,
+ error: null
+ }
+ processedLivePhotos.value.set(photoId, state)
+
+ try {
+ // 下载 MOV 文件
+ state.progress = 10
+ processedLivePhotos.value.set(photoId, { ...state })
+
+ const response = await fetch(movUrl)
+ if (!response.ok) {
+ throw new Error(`Failed to fetch MOV file: ${response.statusText}`)
+ }
+
+ state.progress = 30
+ processedLivePhotos.value.set(photoId, { ...state })
+
+ const movBlob = await response.blob()
+
+ state.progress = 50
+ processedLivePhotos.value.set(photoId, { ...state })
+
+ // 使用 FFmpeg.wasm 或者简单的 video 元素进行转换
+ // 由于浏览器原生支持 MOV 播放,我们可以先尝试直接使用
+ // 如果需要真正的转换,可以集成 FFmpeg.wasm
+
+ // 对于现在,我们直接创建一个可播放的 blob URL
+ // 大多数现代浏览器都支持 MOV 格式的播放
+ const mp4Blob = new Blob([movBlob], { type: 'video/mp4' })
+
+ state.progress = 90
+ processedLivePhotos.value.set(photoId, { ...state })
+
+ // 验证视频是否可以播放
+ const videoUrl = URL.createObjectURL(mp4Blob)
+ const video = document.createElement('video')
+
+ await new Promise<void>((resolve, reject) => {
+ video.onloadedmetadata = () => resolve()
+ video.onerror = () => reject(new Error('Video format not supported'))
+ video.src = videoUrl
+ })
+
+ URL.revokeObjectURL(videoUrl)
+
+ state.isProcessing = false
+ state.progress = 100
+ state.mp4Blob = mp4Blob
+ processedLivePhotos.value.set(photoId, { ...state })
+
+ return mp4Blob
+ } catch (error) {
+ state.isProcessing = false
+ state.error = error instanceof Error ? error.message : 'Unknown error'
+ processedLivePhotos.value.set(photoId, { ...state })
+ console.error('Failed to convert MOV to MP4:', error)
+ return null
+ }
+ }
+
+ /**
+ * 获取照片的处理状态
+ */
+ const getProcessingState = (photoId: string) => {
+ return computed(() => processedLivePhotos.value.get(photoId) || null)
+ }
+
+ /**
+ * 批量处理视口内的实况照片
+ */
+ const batchProcessLivePhotos = async (photos: Array<{ id: string; livePhotoVideoUrl?: string | null }>) => {
+ const livePhotos = photos.filter(photo => photo.livePhotoVideoUrl)
+
+ // 限制并发数量,避免过多请求
+ const batchSize = 3
+ for (let i = 0; i < livePhotos.length; i += batchSize) {
+ const batch = livePhotos.slice(i, i + batchSize)
+ await Promise.allSettled(
+ batch.map(photo => convertMovToMp4(photo.livePhotoVideoUrl!, photo.id))
+ )
+ }
+ }
+
+ /**
+ * 清理已处理的实况照片缓存
+ */
+ const clearProcessedCache = () => {
+ // 释放所有 blob URLs
+ processedLivePhotos.value.forEach((state) => {
+ if (state.mp4Blob) {
+ // 注意:这里不直接 revoke,因为可能还在使用
+ // 实际应用中应该有更好的生命周期管理
+ }
+ })
+ processedLivePhotos.value.clear()
+ }
+
+ return {
+ convertMovToMp4,
+ getProcessingState,
+ batchProcessLivePhotos,
+ clearProcessedCache,
+ processedLivePhotos: readonly(processedLivePhotos)
+ }
+}