打印视频信息
This commit is contained in:
@@ -10,6 +10,8 @@ from pydantic import BaseModel, Field
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, func
|
||||
from sqlalchemy import select, func
|
||||
from app.models.generated_resource import GeneratedResource
|
||||
|
||||
|
||||
from app.dependencies import get_current_user, get_db
|
||||
from app.models.user import User
|
||||
@@ -462,4 +464,68 @@ async def get_upload_history(
|
||||
"message": f"查询上传任务历史失败:{str(e)}",
|
||||
}
|
||||
|
||||
|
||||
#读取指定资源id的素材信息,包括size大小,尺寸,帧率,编码格式,码率,高宽比例
|
||||
@router.get(
|
||||
"/upload-material/{resource_id}",
|
||||
summary="查询上传素材信息",
|
||||
description="查询指定上传素材的详细信息",
|
||||
)
|
||||
async def get_upload_material_info(
|
||||
resource_id: str,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Any | dict:
|
||||
try:
|
||||
upload_material = await db.execute(
|
||||
select(GeneratedResource)
|
||||
.where(
|
||||
GeneratedResource.id == resource_id,
|
||||
)
|
||||
)
|
||||
upload_material = upload_material.scalar_one_or_none()
|
||||
if not upload_material:
|
||||
return {"code": 1, "message": f"素材{resource_id}不存在"}
|
||||
storage_path = upload_material.storage_path
|
||||
|
||||
import ffmpeg
|
||||
# 使用 ffmpeg.probe 获取视频的元数据[reference:20]
|
||||
probe = ffmpeg.probe(storage_path)
|
||||
|
||||
# 从 'format' 中获取文件信息和码率[reference:21]
|
||||
format_info = probe['format']
|
||||
bit_rate = int(format_info.get('bit_rate', 0)) # 码率,单位 bps[reference:22]
|
||||
file_size = int(format_info.get('size', 0)) # 文件大小,单位 bytes[reference:23]
|
||||
duration = float(format_info.get('duration', 0)) # 时长,单位秒[reference:24]
|
||||
|
||||
# 从 'streams' 中查找视频流(通常是第一个视频流)
|
||||
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
|
||||
if video_stream is None:
|
||||
return None
|
||||
|
||||
width = int(video_stream['width'])
|
||||
height = int(video_stream['height'])
|
||||
# 帧率可能以分数形式表示,如 "30000/1001"[reference:25]
|
||||
r_frame_rate = video_stream.get('r_frame_rate', '0/0')
|
||||
if '/' in r_frame_rate:
|
||||
num, den = map(int, r_frame_rate.split('/'))
|
||||
fps = num / den if den != 0 else 0
|
||||
else:
|
||||
fps = float(r_frame_rate)
|
||||
|
||||
codec_name = video_stream.get('codec_name', 'unknown') # 编码格式名称,如 h264[reference:26]
|
||||
|
||||
return {
|
||||
"width": width,
|
||||
"height": height,
|
||||
"fps": fps,
|
||||
"codec": codec_name,
|
||||
"bit_rate": bit_rate, # 单位 bps
|
||||
"file_size": file_size, # 单位 bytes
|
||||
"duration": duration,
|
||||
"aspect_ratio": width / height
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
"code": 0,
|
||||
"message": f"查询上传素材信息失败:{str(e)}",
|
||||
}
|
||||
Reference in New Issue
Block a user