fix: 模板导入bug
This commit is contained in:
parent
14d90b2254
commit
0a0c281ef6
|
|
@ -201,11 +201,11 @@ impl DraftContentParser {
|
||||||
material.template_id = template.id.clone();
|
material.template_id = template.id.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建素材映射表:素材ID -> original_id (用于片段关联)
|
// 构建素材映射表:素材ID -> 素材ID (用于片段关联)
|
||||||
// 片段的 material_id 对应素材的 id,需要映射到 original_id
|
// 片段的 material_id 直接对应 template_materials.id
|
||||||
let material_map: HashMap<String, String> = materials
|
let material_map: HashMap<String, String> = materials
|
||||||
.iter()
|
.iter()
|
||||||
.map(|m| (m.id.clone(), m.original_id.clone())) // 键是素材ID,值是original_id
|
.map(|m| (m.id.clone(), m.id.clone())) // 键是素材ID,值也是素材ID
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for material in materials {
|
for material in materials {
|
||||||
|
|
@ -608,11 +608,20 @@ impl DraftContentParser {
|
||||||
segment_index: u32,
|
segment_index: u32,
|
||||||
material_map: &HashMap<String, String>,
|
material_map: &HashMap<String, String>,
|
||||||
) -> Result<TrackSegment> {
|
) -> Result<TrackSegment> {
|
||||||
// 从 target_timerange 获取时间信息
|
// 使用 target_timerange 作为时间轴位置(这是片段在最终视频中的位置)
|
||||||
let start_time = segment_raw.target_timerange.start;
|
let start_time = segment_raw.target_timerange.start;
|
||||||
let duration = segment_raw.target_timerange.duration;
|
let target_duration = segment_raw.target_timerange.duration;
|
||||||
|
let end_time = start_time + target_duration;
|
||||||
|
|
||||||
let end_time = start_time + duration;
|
// 计算实际的播放时长(考虑播放速度)
|
||||||
|
let speed = segment_raw.speed.unwrap_or(1.0);
|
||||||
|
let actual_duration = if speed != 0.0 {
|
||||||
|
// 如果有播放速度,计算实际播放时长
|
||||||
|
// 例如:speed = 1.28306,则实际播放时长 = target_duration / speed
|
||||||
|
(target_duration as f64 / speed) as u64
|
||||||
|
} else {
|
||||||
|
target_duration
|
||||||
|
};
|
||||||
|
|
||||||
let segment_name = format!("片段{}", segment_index + 1);
|
let segment_name = format!("片段{}", segment_index + 1);
|
||||||
let mut segment = TrackSegment::new(
|
let mut segment = TrackSegment::new(
|
||||||
|
|
@ -624,19 +633,29 @@ impl DraftContentParser {
|
||||||
segment_index,
|
segment_index,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 更新片段的实际持续时间(考虑播放速度)
|
||||||
|
segment.duration = actual_duration;
|
||||||
|
|
||||||
// 关联素材
|
// 关联素材
|
||||||
if let Some(material_id) = material_map.get(&segment_raw.material_id) {
|
if let Some(material_id) = material_map.get(&segment_raw.material_id) {
|
||||||
segment.associate_material(material_id.clone());
|
segment.associate_material(material_id.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 存储原始属性
|
// 存储原始属性和计算后的时间信息
|
||||||
let properties = serde_json::json!({
|
let properties = serde_json::json!({
|
||||||
"original_material_id": segment_raw.material_id,
|
"original_material_id": segment_raw.material_id,
|
||||||
"target_timerange": segment_raw.target_timerange,
|
"target_timerange": segment_raw.target_timerange,
|
||||||
"source_timerange": segment_raw.source_timerange,
|
"source_timerange": segment_raw.source_timerange,
|
||||||
"speed": segment_raw.speed,
|
"speed": segment_raw.speed,
|
||||||
|
"actual_duration": actual_duration,
|
||||||
|
"target_duration": target_duration,
|
||||||
"visible": segment_raw.visible,
|
"visible": segment_raw.visible,
|
||||||
"volume": segment_raw.volume
|
"volume": segment_raw.volume,
|
||||||
|
"computed_info": {
|
||||||
|
"speed_applied": speed != 1.0,
|
||||||
|
"original_speed": speed,
|
||||||
|
"time_scaling_factor": if speed != 0.0 { 1.0 / speed } else { 1.0 }
|
||||||
|
}
|
||||||
});
|
});
|
||||||
segment.properties = Some(properties.to_string());
|
segment.properties = Some(properties.to_string());
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -434,6 +434,41 @@ impl Database {
|
||||||
let _ = conn.execute("ALTER TABLE templates RENAME TO templates_old", []);
|
let _ = conn.execute("ALTER TABLE templates RENAME TO templates_old", []);
|
||||||
let _ = conn.execute("ALTER TABLE templates_new RENAME TO templates", []);
|
let _ = conn.execute("ALTER TABLE templates_new RENAME TO templates", []);
|
||||||
|
|
||||||
|
// 修复轨道片段表的外键约束问题
|
||||||
|
// 重建 track_segments 表,恢复正确的 template_material_id 外键约束
|
||||||
|
let _ = conn.execute(
|
||||||
|
"CREATE TABLE IF NOT EXISTS track_segments_new (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
track_id TEXT NOT NULL,
|
||||||
|
template_material_id TEXT,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
start_time INTEGER NOT NULL,
|
||||||
|
end_time INTEGER NOT NULL,
|
||||||
|
duration INTEGER NOT NULL,
|
||||||
|
segment_index INTEGER NOT NULL,
|
||||||
|
properties TEXT,
|
||||||
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (track_id) REFERENCES tracks (id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (template_material_id) REFERENCES template_materials (id) ON DELETE SET NULL
|
||||||
|
)",
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
// 迁移轨道片段数据
|
||||||
|
let _ = conn.execute(
|
||||||
|
"INSERT OR IGNORE INTO track_segments_new
|
||||||
|
SELECT id, track_id, template_material_id, name, start_time, end_time,
|
||||||
|
duration, segment_index, properties, created_at, updated_at
|
||||||
|
FROM track_segments",
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
// 删除旧表并重命名新表
|
||||||
|
let _ = conn.execute("DROP TABLE IF EXISTS track_segments_old", []);
|
||||||
|
let _ = conn.execute("ALTER TABLE track_segments RENAME TO track_segments_old", []);
|
||||||
|
let _ = conn.execute("ALTER TABLE track_segments_new RENAME TO track_segments", []);
|
||||||
|
|
||||||
// 创建模板素材表索引
|
// 创建模板素材表索引
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"CREATE INDEX IF NOT EXISTS idx_template_materials_template_id ON template_materials (template_id)",
|
"CREATE INDEX IF NOT EXISTS idx_template_materials_template_id ON template_materials (template_id)",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue