Can Chat GPT really replace a coder?
When I heard that Chat GPT was capable of writing code, I was immediately sceptical. I have tried a couple of AI code pilots in the past, and I can liken them to an over-enthusiastic amateur leaning over your shoulder and shouting out whatever pops into their heads. But after trying Chat GPT out with a few funny stories, I was impressed and ready to give AI coding another try. I’d written a short script to extract audio from video files so that I could listen to them on my phone — here it is:
#!/usr/bin/php
<?php
$cwd = getcwd();
$target = $cwd.’/extractedaudio’;
if (!file_exists($target)) {
mkdir($target);
}
echo “n Extracting audio for all files in $cwd to $target”;
$dir = new RecursiveDirectoryIterator($cwd);
$iterator = new RecursiveIteratorIterator($dir);
$iterator->setMaxDepth(2);
$regexstr = ‘/^.+.(?:mov|mp4|avi|m4v|h265)$/i’;
$regexiterator = new RegexIterator($iterator, $regexstr, RecursiveRegexIterator::GET_MATCH);
foreach ($regexiterator as $info) {
$pathname = $info[0];
echo “n Processing $pathname”;
$finfo = pathinfo($pathname);
$outputmp3name = $finfo[‘filename’].’.mp3′;
$outputmp3path = “$target/$outputmp3name”;
$cmd = “ffmpeg -i $pathname -q:a 0 -map a $outputmp3path”;
passthru($cmd);
}
echo “nn!!!DONE!!! :-)”;
So the immediate issue that I had was transcoding the audio file to mp3 when there was already an mp3 audio source embedded within the video file. This is bad for three reasons: a) It takes longer and b) The quality degrades, and c) The file size can be larger than the original audio source.
I asked Chat GPT the following:
Improve this script to extract mp3 audio instead of transcoding if the source uses mp3 for audio.
Here’s the main change it made to the code:
$cmd = “ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 “$pathname””;
$codec_name = trim(shell_exec($cmd));
if ($codec_name != ‘mp3’) {
$cmd = “ffmpeg -i “$pathname” -q:a 0 -map a “$outputmp3path””;
passthru($cmd);
}
And here’s Chat GPT’s explanation of what it did:
The main changes are:The script uses ffprobe to get the audio codec of the input file. The command returns the codec name, which is then trimmed to remove any whitespace characters.The script checks whether the audio codec is “mp3”. If it is, the script skips extracting the audio and moves on to the next file. If it isn’t, the script uses ffmpeg to extract the audio and save it as an MP3 file.
So now it was extracting the codec type from the video file, which was good. However, it was now only processing files that didn’t use the mp3 codec, and this was wrong. Also, it was still transcoding the audio. Here was my response to chat GPT:
That’s good but if the source contains mp3 audio, can you extract that instead of transcoding. At the moment, the modifications you have made will do nothing with movie files that contain mp3 audio.
Chat GPT replied with:
You’re right, my previous solution still transcodes the audio even if the input file already has an MP3 audio codec. Here’s an updated version of the script that checks if the input file contains an MP3 audio stream and extracts it as an MP3 file without transcoding if it does:
Here’s the main change it made to the code:
$cmd = “ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 “$pathname””;
$codec_name = trim(shell_exec($cmd));
if ($codec_name == ‘mp3’) {
copy($pathname, $outputmp3path);
} else {
$cmd = “ffmpeg -i “$pathname” -q:a 0 -map a “$outputmp3path””;
passthru($cmd);
}
And here’s its explanation:
The main change is that the script now checks if the input file has an MP3 audio stream before extracting the audio. If it does, the script simply copies the input file to the output directory with an MP3 file extension. If it doesn’t, the script uses ffmpeg to extract the audio and save it as an MP3 file.I hope this updated script works better for you! Let me know if you have any further questions or issues.
This still didn’t do what I had asked — the problematic piece of code is below, where Chat GPT wasn’t clever enough to understand that just because the audio codec detected was an mp3 doesn’t mean that the entire file IS an mp3.
if ($codec_name == ‘mp3’) {
copy($pathname, $outputmp3path);
Here’s my response:
Simply copying the input file to the output directory isn’t good enough — you need to use ffmpeg to extract the mp3 audio from the source because the source is a video, not an mp3 file.
And here’s Chat GPT’s response:
You’re right, I apologize for the confusion. I misunderstood your previous comment.In that case, here’s an updated version of the script that extracts the audio from video files as MP3, but copies audio-only files without transcoding: $cmd = “ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 “$pathname””;
$codec_name = trim(shell_exec($cmd));
if ($codec_name == ‘mp3’) {
copy($pathname, $outputmp3path);
} else {
$cmd = “ffmpeg -i “$pathname” -vn -acodec libmp3lame -q:a 0 “$outputmp3path””;
passthru($cmd);
}
However, it has changed the wrong line of code! It still hasn’t understood that copying the file if the audio codec is mp3 is not going to work because the file itself is a video.
I went around in circles with it for a while but, in the end, resorted to fixing the script manually. Here is the finished script (fixed by hand):
#!/usr/bin/php
<?php
$cwd = getcwd();
$target = $cwd.’/extractedaudio’;
if (!file_exists($target)) {
mkdir($target);
}
echo “n Extracting audio for all files in $cwd to $target”;
$dir = new RecursiveDirectoryIterator($cwd);
$iterator = new RecursiveIteratorIterator($dir);
$iterator->setMaxDepth(2);
$regexstr = ‘/^.+.(?:mov|mp4|avi|m4v|h265)$/i’;
$regexiterator = new RegexIterator($iterator, $regexstr, RecursiveRegexIterator::GET_MATCH);
foreach ($regexiterator as $info) {
$pathname = $info[0];
echo “n Processing $pathname”;
$finfo = pathinfo($pathname);
$outputmp3name = $finfo[‘filename’].’.mp3′;
$outputmp3path = “$target/$outputmp3name”;
if (file_exists($outputmp3path)) {
echo “n Skipping existing mp3 file $outputmp3name”;
continue;
}
$cmd = “ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 “$pathname””;
$codec_name = trim(shell_exec($cmd));
if ($codec_name == ‘mp3’) {
// Audio codec is already mp3, no need to convert, just extract.
$cmd = “ffmpeg -i “$pathname” -vn -acodec copy “$outputmp3path””;
passthru($cmd);
} else {
// Audio codec is not mp3, convert to mp3
$cmd = “ffmpeg -i “$pathname” -vn -acodec libmp3lame -q:a 0 “$outputmp3path””;
passthru($cmd);
}
}
echo “nn!!!DONE!!! :-)”;
In conclusion, Chat GPT is not going to replace developers soon. However, it is certainly a useful tool to help out a coder. I am absolutely blown away by how well it managed to handle my instructions. And I could have ended up with similar misinterpretations had I instructed another human developer. When it got things wrong, it got me thinking about my code and helped me come to the best solution, even if I did have to manually craft the optimization I was looking for. Bravo, Chat GPT!
Source: https://brudinie.medium.com/feed
- PHP 8.0 Model Classes with constructor promotion – 24th November 2024
- Moodle jobs: Program Administrator (Moodle LMS) – Full-time – Herndon, VA, USA – 21st November 2024
- Moodle Educator Certificate course starts 1st October – 8th September 2024