Fabrice-TIERCELIN commited on
Commit
022fa80
·
verified ·
1 Parent(s): 0ef866e

Save the prompt in the metadata

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py CHANGED
@@ -4,6 +4,11 @@
4
  from __future__ import annotations
5
 
6
  import os
 
 
 
 
 
7
  import pathlib
8
  import random
9
  import sys
@@ -1074,6 +1079,52 @@ def _make_handler(mode_name: str, h: dict):
1074
 
1075
  return handler
1076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1077
  async def run_example(dummy_image):
1078
  example_inputs = {
1079
  "prompt": prompt_debug_value[0],
@@ -1127,6 +1178,8 @@ async def run_example(dummy_image):
1127
  else:
1128
  count = int(count) - 1
1129
 
 
 
1130
  return final_output[1]
1131
 
1132
  if __name__ == "__main__":
 
4
  from __future__ import annotations
5
 
6
  import os
7
+ import imageio_ffmpeg
8
+ import tempfile
9
+ import shutil
10
+ import subprocess
11
+ from datetime import datetime
12
  import pathlib
13
  import random
14
  import sys
 
1079
 
1080
  return handler
1081
 
1082
+ def set_mp4_comments_imageio_ffmpeg(input_file, comments):
1083
+ try:
1084
+ # Get the path to the bundled FFmpeg binary from imageio-ffmpeg
1085
+ ffmpeg_path = imageio_ffmpeg.get_ffmpeg_exe()
1086
+
1087
+ # Check if input file exists
1088
+ if not os.path.exists(input_file):
1089
+ #print(f"Error: Input file {input_file} does not exist")
1090
+ return False
1091
+
1092
+ # Create a temporary file path
1093
+ temp_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False).name
1094
+
1095
+ # FFmpeg command using the bundled binary
1096
+ command = [
1097
+ ffmpeg_path, # Use imageio-ffmpeg's FFmpeg
1098
+ '-i', input_file, # input file
1099
+ '-metadata', f'comment={comments}', # set comment metadata
1100
+ '-c:v', 'copy', # copy video stream without re-encoding
1101
+ '-c:a', 'copy', # copy audio stream without re-encoding
1102
+ '-y', # overwrite output file if it exists
1103
+ temp_file # temporary output file
1104
+ ]
1105
+
1106
+ # Run the FFmpeg command
1107
+ result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
1108
+
1109
+ if result.returncode == 0:
1110
+ # Replace the original file with the modified one
1111
+ shutil.move(temp_file, input_file)
1112
+ #print(f"Successfully added comments to {input_file}")
1113
+ return True
1114
+ else:
1115
+ # Clean up temp file if FFmpeg fails
1116
+ if os.path.exists(temp_file):
1117
+ os.remove(temp_file)
1118
+ #print(f"Error: FFmpeg failed with message:\n{result.stderr}")
1119
+ return False
1120
+
1121
+ except Exception as e:
1122
+ # Clean up temp file in case of other errors
1123
+ if 'temp_file' in locals() and os.path.exists(temp_file):
1124
+ os.remove(temp_file)
1125
+ print(f"Error saving prompt to video metadata, ffmpeg may be required: "+str(e))
1126
+ return False
1127
+
1128
  async def run_example(dummy_image):
1129
  example_inputs = {
1130
  "prompt": prompt_debug_value[0],
 
1178
  else:
1179
  count = int(count) - 1
1180
 
1181
+ set_mp4_comments_imageio_ffmpeg(final_output, f"Prompt: {example_inputs["prompt"]} | Negative Prompt: {example_inputs["negative_prompt"]}");
1182
+ print("Video exported: " + final_output)
1183
  return final_output[1]
1184
 
1185
  if __name__ == "__main__":