#!/bin/bash

# 指定目录
target_folder="/app/video/lihan"

# 遍历目标目录下的所有视频和音频文件
find "$target_folder" -type f -name "*.mp4" | while read -r file; do
    # 输出正在处理的文件
    echo "Processing file: $file"
    #提取文件名和路径
    filename=$(basename "$file")
    filename="${filename%.*}"
    directory=$(dirname "$file")
    # 使用ffmpeg进行重新封装，使用copy指令
    nohup ffmpeg -i "$file" -c:v copy -c:a copy "${directory}/${filename}_copy.mp4" &
    
    #获取ffmpeg命令的PID
    ffmpeg_pid=$!
	
    # 等待ffmpgeg处理完成
    while true;do

	#检查目标文件是否正在被写入
	if [-f "${directory}/${filename}_copy.mp4"]; then
		echo "Output file '${directory}/${filename}_copy.mp4' has been generated."
		break
	else
		echo "FFmpeg is still processing..."
		# 等待一段时间再次检查
		sleep 1
	fi
    done
    
    # 等待ffmpeg命令结束
    wait $ffmpeg_pid

    # rm "$file"
    # mv "${directory}/${filename}_copy.mp4" "${directory}/${filename}.mp4"

done

echo "Conversion complete!"

