AI-Assisted Programming in Practice: Key Considerations for Building a Batch Audio Extraction Tool
This article walks through building a small desktop tool that extracts audio from video files in batch and exports it in common audio formats. It explains how to turn AI-generated requirements into verifiable acceptance criteria, keep the GUI responsive during long conversions, handle errors and cancellation, and test generated code beyond a successful build.
1. Background: Verifying AI-Generated Code
More developers are using large language models to generate code. Generation is fast, but whether the code runs correctly and meets real requirements still needs independent verification. Three criteria matter: whether each requirement is covered, whether the program runs stably, and whether exceptional cases are handled.
This article uses a typical small desktop tool as an example. The tool extracts audio from video files in batches and exports it in common audio formats. It includes a graphical interface, a file list, format settings, progress feedback, and error messages, so it covers several of the most common problems in desktop application development.
2. Requirement Breakdown: Turning Descriptions into a Verifiable Checklist
The more detailed the requirements, the more complete the generated result is likely to be. However, every detail must be testable. It is recommended to organize requirements as a table of features and acceptance criteria.
| Feature | Acceptance Criteria |
|---|---|
| Video input | Supports common video container formats; file extension detection is case-insensitive |
| Audio export | Common audio formats can be selected as output |
| File list | Supports adding a single file or an entire directory, with an option to scan subdirectories recursively |
| Format settings | Input format filtering, output format, and output directory are all configurable |
| Conversion | Runs in batch; a failure in one file does not affect the others |
| Progress and status | Shows an overall progress bar; the status bar shows the current file and its result |
| Error messages | Shows a clear message on failure, including the file name and the reason for the failure |
| UI responsiveness | The window can be moved and clicked during conversion, and the conversion can be canceled |
Each acceptance criterion corresponds to a result that can be operated and observed. Without this correspondence, it is hard to tell whether the generated code actually meets the requirements.
3. Core Challenge: Preventing UI Freezes
Desktop GUIs are driven by an event loop. Window redrawing, button clicks, and control updates are processed one after another on the main thread. If conversion runs directly on the main thread, each file may take anywhere from a few seconds to several minutes. During that time the interface stops responding, so the window cannot be dragged, or the system displays a “Not Responding” message.
A common solution is to run the time-consuming task on a worker thread and pass progress back through a thread-safe message queue. The interface layer does not wait for conversion results. Instead, it checks the queue at fixed intervals and updates the progress bar and status bar.
The following pseudocode shows this structure. It is independent of any specific language:
function start_conversion(file_list, params):create cancel_flagcreate message_queuestart worker_thread(run_batch_conversion, file_list, params, message_queue, cancel_flag)function run_batch_conversion(file_list, params, message_queue, cancel_flag):for each file f (index i):if cancel_flag is set: exit loopsend_message(message_queue, "start", f)result = call_external_transcoder(f, params)if result failed: send_message(message_queue, "error", f, failure_reason)else: send_message(message_queue, "done", f)send_message(message_queue, "progress", i + 1, total)send_message(message_queue, "end")function refresh_ui_periodically():take all messages from message_queue:update progress bar, status bar, or error list based on message typewait 100 ms, then call refresh_ui_periodically() again
This structure has two rules that must be followed. First, only the main thread may modify interface controls. Worker threads should pass data only through the queue. Most GUI frameworks enforce this constraint. Violating it often causes intermittent crashes or a garbled display. Second, cancellation must act on both the conversion loop and the running external process. Discarding the results alone does not release resources, because the external process would still use CPU and disk.
When calling the external transcoder, there are two more details to handle. First, read standard output line by line to parse progress, and read standard error at the same time. Otherwise, the child process will block once the pipe buffer fills up. Second, when canceling, terminate the child process actively and wait for it to exit.
4. Engineering Details: Fault Tolerance and User Experience
1. Prevent output file overwrites. If a file with the same name already exists, append a sequence number automatically. For example, if clip.out already exists, the new file is named clip(1).out. Overwriting should happen only when the user explicitly chooses it, never by default.
2. Dependency detection. Before conversion, check whether the external transcoder can run correctly. This can be done by searching the system path and trying to run a version query command. If the transcoder is not found, show the user how to install it and how to configure the path, instead of exposing the low-level exception.
3. Error isolation. When a single file fails, record the reason and continue with the remaining files. After the batch finishes, summarize the number of successes, the number of failures, and the list of failed files so the user can retry.
4. Exit protection. If the user clicks Exit while a task is running, show a confirmation dialog. After confirmation, request cancellation of the worker thread, wait for the child process to end, and then close the window.
5. Directory scanning. During recursive scanning, if a directory cannot be read due to missing permissions, skip it and log the skip. A single permission issue should not stop the entire task.
6. Path and parameter safety. File names may contain spaces, non-Latin characters, or special symbols. When calling an external program, pass paths as an argument list. Do not concatenate them into a single command string. String concatenation can cause parsing errors and creates a risk of command injection.
5. Verification Methods: A Successful Build Does Not Mean Correct Behavior
After AI generates code, verify it in four stages.
Level 1: Static checks. These include syntax compilation, code style checks, and checks for undefined names and unused variables. This level catches obvious errors but cannot confirm that the business logic is correct.
Level 2: Environment checks. Install the dependencies in a clean environment and confirm that the message shown when the external component is missing is clear. Also check whether the layout works at different window sizes.
Level 3: Feature checks against requirements. Test each item in the table from Section 2. For example, import a folder that contains subdirectories and confirm that the recursive scan switch works. Select a file whose output name already exists and confirm that the numbering rule applies.
Level 4: Exception and stress tests. Use very large files, batch tasks with hundreds of files, corrupted video files, directories without read permission, and files with special characters in their names. During conversion, repeatedly click Cancel and Exit, then check that the interface keeps responding and that child processes are cleaned up correctly.
6. Prompt Writing Tips
- Specify the inputs, outputs, and constraints, for example, “conversion must not block the interface.”
- Specify the expected behavior in error cases, for example, “a failure in one file must not affect other files.”
- Ask the model to list its assumptions and explain the choices it made for anything unspecified.
- Ask the model to provide run instructions and a dependency list so the project can be reproduced in a clean environment.
- You can ask the model to check its own requirement coverage, but its self-check does not replace manual testing.
