Эх сурвалжийг харах

时光相册-视频生成 ffmpeg多版本使用

linhaohong 11 сар өмнө
parent
commit
c0f74042ba

+ 20 - 15
CeleryTask/TimeAlbumTask.py

@@ -38,7 +38,7 @@ def generate_video(image_files, output_path):
     start_xvfb()
     try:
         video_files = []
-        music_path = "/web/test/ASJServer/static/temp/music.mp3"
+        music_path = "static/ffmpeg/music.mp3"
         # 定义特效列表
         transitions = [
             "BowTieHorizontal.glsl",
@@ -58,36 +58,41 @@ def generate_video(image_files, output_path):
             video_files.append(output_video)
 
             cmd = [
-                'ffmpeg', '-loop', '1', '-i', image_files[i],
+                'static/ffmpeg/ffmpeg4', '-loop', '1', '-i', image_files[i],
                 '-loop', '1', '-i', image_files[i + 1],
                 '-filter_complex',
-                f'gltransition=duration=1:offset=0.7:source=/web/test/ASJServer/static/gl-transitions/{transitions[i]}',
+                f'gltransition=duration=1:offset=0.7:source=static/gl-transitions/{transitions[i]}',
                 '-t', '2', output_video
             ]
 
             subprocess.run(cmd, check=True)
 
         # 生成拼接列表文件
-        with open(os.path.join(output_path, 'filelist.txt'), 'w') as f:
+        filelist_path = os.path.join(output_path, 'filelist.txt')
+        with open(filelist_path, 'w') as f:
             for video in video_files:
-                f.write(f"file '{video}'\n")
+                f.write(f"file '{os.path.relpath(video, output_path)}'\n")  # 使用相对路径
 
-        # 拼接视频
-        final_output = os.path.join(output_path, 'final_output.mp4')
+        # 拼接并生成背景音乐
+        final_output_with_music = os.path.join(output_path, 'final_output_with_music.mp4')
         subprocess.run(
-            ['ffmpeg', '-f', 'concat', '-safe', '0', '-i', '/web/test/ASJServer/static/temp/filelist.txt', '-c', 'copy',
-             final_output],
-            check=True)
+            ['static/ffmpeg/ffmpeg6', '-f', 'concat', '-safe', '0', '-i', filelist_path, '-i', music_path,
+             '-c:v', 'copy', '-c:a', 'aac', '-b:a', '192k', '-movflags', '+faststart', '-shortest',
+             final_output_with_music],
+            check=True
+        )
+        video420acc = os.path.join(output_path, 'video420acc.mp4')
 
-        # 添加背景音乐
-        final_output_with_music = os.path.join(output_path, 'final_output_with_music.mp4')
-        subprocess.run(['ffmpeg', '-i', final_output, '-i', music_path, '-c:v', 'copy', '-c:a', 'aac', '-b:a', '192k',
-                        '-shortest', final_output_with_music], check=True)
+        subprocess.run(
+            ['static/ffmpeg/ffmpeg6', '-i', final_output_with_music, '-pix_fmt', 'yuv420p', video420acc],
+            check=True
+        )
 
         # 清理临时文件
         for video in video_files:
             os.remove(video)
-        os.remove('/web/test/ASJServer/static/temp/filelist.txt')
+        os.remove(filelist_path)
+        os.remove(final_output_with_music)
     except Exception as e:
         LOGGER.info(f'视频合成失败: {e}')
 

+ 4 - 4
Controller/TimeAlbumController.py

@@ -32,11 +32,11 @@ class TimeAlbum(View):
         # 下载图片保存地址
 
         # 收集图片文件路径
-        image_files = ['/web/test/ASJServer/static/temp/image1.jpg', '/web/test/ASJServer/static/temp/image2.jpg',
-                       '/web/test/ASJServer/static/temp/image3.jpg', '/web/test/ASJServer/static/temp/image4.jpg',
-                       '/web/test/ASJServer/static/temp/image5.jpg']
+        image_files = ['static/temp/image1.jpg', 'static/temp/image2.jpg',
+                       'static/temp/image3.jpg', 'static/temp/image4.jpg',
+                       'static/temp/image5.jpg']
 
-        output_path = '/web/test/ASJServer/static/temp'
+        output_path = 'static/temp'
 
         # 调用 Celery 任务
         generate_video.delay(image_files, output_path)