Making simple scripts with chatGPT

A bit of knowledge about programming can make you a lot more productive and save you lots of time that you can spend doing something else, but getting into programming is not always that easy.

For Python you can find a lot of good courses online and you should do some training to understand the basic concept of programming. More and more companies will also provide access to Python courses.

In IT we use scripts to automate repetitive and boring task, like making inventories, changing config on multiple devices, generating reports... Once you have these scripts it is easy to adapt them for the required task, but making them from scratch the first time can take some time and plenty of googling and reading forums.

But these days there are AI's to help you out. When you do a lot of programming you should take a look at CodePilot, but even the free version of ChatGPT can generate some good scripts.

For example, I have a backup of a TV show where all seasons have their own folder and in every season there is a subs-folder. In this folder there are again folders with the same name as the episode of the TV show. the name of the subtitle-file is 2_eng.srt, but sometimes this file is not available, but the first subtitle-file will be 3_eng.srt, in that case we need to copy that file.

When the subtitle is in the same location as the episode then it will automatically be shown when being played, so I want to move all the subtitles from the subtitle-folder to the episode folder and I would like to do that for all seasons.

I asked ChatGPT to do the following: can you make me a python script that copies a file called 2_eng.srt from a sub-folder with the same name as the mp4-file on the root directory located in the Subs folder.

the first attempt

Even with that short request I got working script, but I forgot to asked to copy the next subtitle-file when the one starting with 2 did not exist, and the script was using a variable to point it to the folder to scan. So lets start fix those issues by asking: "if the file 2_eng.srt does not exist then copy the file 3_eng.srt".

There is also no loop to check multiple folders for mp4 files, so I asked to add that loop with: "can you add a loop so it will check multiple folders with mp4 files in the same directory?" It is nice that after generating the script chatGPT will shortly explain what it is doing now in three steps and reminds me to set the correct path to scan, that reminds me to ask to change the script to that will become a command line option.

after only 3 request we already have this script

So the last thing I asked was "can you change the script so it will read the root directory from a command line option?" to generate the final script. Until this point I only checked the python script but did not test it yet on the target directory, but from what I could tell there were no obvious issues, but before testing, I made a backup just to be sure. 😄

the final script!

After copying the script to a file I read over it an noticed that the destination of the subtitle file was not exactly the same as the episode name, the the script appended the name of the subtitle file. to fix this I just changed those lines manually. For example I have changed:

dest_file = os.path.join(full_path, f"{mp4_base}_2_eng.srt")
dest_file = os.path.join(full_path, f"{mp4_base}_3_eng.srt")

to:

dest_file = os.path.join(full_path, f"{mp4_base}.srt")

After that small change I am happy to say that the script worked flawlessly and using chatGPT I was able to make it a lot quicker. I have also used it for other Python and Bash-scripts with similar results. You have to get used to writing detailed enough prompts and I prefer to add small changes after and initial more complex prompt.

AI will not replace real programmers, but it is a great tool to speed up the work and to make programming more accessible for other people.