Spaces:
Running
on
Zero
Running
on
Zero
refactor: Enhance FFMPEG command generation with step-by-step thinking and improved command extraction
Browse files
app.py
CHANGED
|
@@ -132,6 +132,7 @@ You are given:
|
|
| 132 |
Your objective is to generate the SIMPLEST POSSIBLE single ffmpeg command to create the requested video.
|
| 133 |
|
| 134 |
Key requirements:
|
|
|
|
| 135 |
- Use the absolute minimum number of ffmpeg options needed
|
| 136 |
- Avoid complex filter chains or filter_complex if possible
|
| 137 |
- Prefer simple concatenation, scaling, and basic filters
|
|
@@ -147,15 +148,19 @@ Remember: Simpler is better. Only use advanced ffmpeg features if absolutely nec
|
|
| 147 |
},
|
| 148 |
{
|
| 149 |
"role": "user",
|
| 150 |
-
"content": f"""Always output the media as video/mp4 and output file with "output.mp4".
|
| 151 |
-
The current assets and objective follow.
|
| 152 |
|
| 153 |
AVAILABLE ASSETS LIST:
|
| 154 |
|
| 155 |
{files_info_string}
|
| 156 |
|
| 157 |
OBJECTIVE: {prompt} and output at "output.mp4"
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
""",
|
| 160 |
},
|
| 161 |
]
|
|
@@ -188,13 +193,23 @@ YOUR FFMPEG COMMAND:
|
|
| 188 |
# Find content between ```sh or ```bash and the next ```
|
| 189 |
import re
|
| 190 |
|
| 191 |
-
|
| 192 |
-
if
|
| 193 |
-
command =
|
| 194 |
else:
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
else:
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
# remove output.mp4 with the actual output file path
|
| 200 |
command = command.replace("output.mp4", "")
|
|
@@ -270,7 +285,11 @@ def update(
|
|
| 270 |
f"\n=== EXECUTING FFMPEG COMMAND ===\nffmpeg {' '.join(final_command[1:])}\n"
|
| 271 |
)
|
| 272 |
subprocess.run(final_command, cwd=temp_dir)
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
return output_file_path, gr.update(value=generated_command)
|
| 275 |
except Exception as e:
|
| 276 |
attempts += 1
|
|
|
|
| 132 |
Your objective is to generate the SIMPLEST POSSIBLE single ffmpeg command to create the requested video.
|
| 133 |
|
| 134 |
Key requirements:
|
| 135 |
+
- First, think step-by-step about what the user is asking for and reformulate it into a clear technical specification
|
| 136 |
- Use the absolute minimum number of ffmpeg options needed
|
| 137 |
- Avoid complex filter chains or filter_complex if possible
|
| 138 |
- Prefer simple concatenation, scaling, and basic filters
|
|
|
|
| 148 |
},
|
| 149 |
{
|
| 150 |
"role": "user",
|
| 151 |
+
"content": f"""Always output the media as video/mp4 and output file with "output.mp4".
|
| 152 |
+
The current assets and objective follow.
|
| 153 |
|
| 154 |
AVAILABLE ASSETS LIST:
|
| 155 |
|
| 156 |
{files_info_string}
|
| 157 |
|
| 158 |
OBJECTIVE: {prompt} and output at "output.mp4"
|
| 159 |
+
|
| 160 |
+
First, think step-by-step about what I'm asking for and reformulate it into a clear technical specification.
|
| 161 |
+
Then provide the FFMPEG command that will accomplish this task.
|
| 162 |
+
|
| 163 |
+
YOUR RESPONSE:
|
| 164 |
""",
|
| 165 |
},
|
| 166 |
]
|
|
|
|
| 193 |
# Find content between ```sh or ```bash and the next ```
|
| 194 |
import re
|
| 195 |
|
| 196 |
+
command_match = re.search(r"```(?:sh|bash)?\n(.*?)\n```", content, re.DOTALL)
|
| 197 |
+
if command_match:
|
| 198 |
+
command = command_match.group(1).strip()
|
| 199 |
else:
|
| 200 |
+
# Try to find a line that starts with ffmpeg
|
| 201 |
+
ffmpeg_lines = [line.strip() for line in content.split('\n') if line.strip().startswith('ffmpeg')]
|
| 202 |
+
if ffmpeg_lines:
|
| 203 |
+
command = ffmpeg_lines[0]
|
| 204 |
+
else:
|
| 205 |
+
command = content.replace("\n", "")
|
| 206 |
else:
|
| 207 |
+
# Try to find a line that starts with ffmpeg
|
| 208 |
+
ffmpeg_lines = [line.strip() for line in content.split('\n') if line.strip().startswith('ffmpeg')]
|
| 209 |
+
if ffmpeg_lines:
|
| 210 |
+
command = ffmpeg_lines[0]
|
| 211 |
+
else:
|
| 212 |
+
command = content.replace("\n", "")
|
| 213 |
|
| 214 |
# remove output.mp4 with the actual output file path
|
| 215 |
command = command.replace("output.mp4", "")
|
|
|
|
| 285 |
f"\n=== EXECUTING FFMPEG COMMAND ===\nffmpeg {' '.join(final_command[1:])}\n"
|
| 286 |
)
|
| 287 |
subprocess.run(final_command, cwd=temp_dir)
|
| 288 |
+
# Store the full model response for display
|
| 289 |
+
model_response = content.strip()
|
| 290 |
+
# Extract just the command for display
|
| 291 |
+
command_for_display = f"ffmpeg {' '.join(args[1:])} -y output.mp4"
|
| 292 |
+
generated_command = f"### Model Analysis\n{model_response}\n\n### Generated Command\n```bash\n{command_for_display}\n```"
|
| 293 |
return output_file_path, gr.update(value=generated_command)
|
| 294 |
except Exception as e:
|
| 295 |
attempts += 1
|