package cn.reghao.tnb.content.app.media;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author reghao
 * @date 2026-02-12 21:25:16
 */
//@Component
public class VideoConsumer {
    private final RedisTemplate redisTemplate;

    public VideoConsumer(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @RabbitListener(queues = "transcoding_queue")
    public void handleTranscode(Map<String, Object> message) {
        String partPath = (String) message.get("partPath");
        String videoId = (String) message.get("videoId");

        // 执行转码命令：ffmpeg -i part.mp4 -c:v libx265 ... out.mp4
        boolean success = executeFFmpegTranscode(partPath);

        if (success) {
            // 更新 Redis 中的计数器
            Long completed = redisTemplate.opsForHash().increment("v_status:" + videoId, "done", 1);
            Long total = (Long) redisTemplate.opsForHash().get("v_status:" + videoId, "total");

            // 检查是否所有切片都转完了
            if (completed.equals(total)) {
                notifyMergeService(videoId); // 触发合并或生成 m3u8
            }
        }
    }

    private boolean executeFFmpegTranscode(String path) {
        return false;
    }

    private void notifyMergeService(String videoId) {
    }
}
