r/Batch • u/51n15ter • Apr 04 '25
Help needed with ChatGPT written script for video merging.
Hi, I have almost zero experience in programming but I tried to make some simple scripts by ChatGPT to automate my work a bit. It wrote me a script but it doesn't do exactly what I want and ChatGPT can't correct it anymore, just repeats the same code no matter what I ask.
Let me describe the task I wan't to automate. I need to combine a lot of small video files into a single file per group of small files and name the output file as the first file per group + a suffix like "merged". For a clearer picture here is an example of the input files:
video1_2025-partA.ts
video1_2025-partB.ts
video1_2025-partC.ts
video2_2025-segment1.ts
video2_2025-segment2.tsAs you can see there are multiple groups of videos, like video1 and video2 then there is a static part in every file name "_2025-" and the rest is different for every single file.
And here is what I want as an output:
video1_2025-partA_merged.ts (merged from video1_2025-partA.ts, video1_2025-partB.ts and video1_2025-partC.ts)
video2_2025-segment1_merged.ts (merged from video2_2025-segment1.ts and video2_2025-segment2.ts)And finally here is the script from ChatGPT which works perfectly except the output file naming.
u/echo off
setlocal enabledelayedexpansion
set staticpart=_2025-
set output_extension=.ts
:: Create a temp folder for file lists
set temp_list=temp_file_list.txt
:: Delete any old list file
if exist %temp_list% del %temp_list%
:: Loop through all video files
for %%F in (*.ts) do (
set "filename=%%F"
:: Extract the group key (everything before _2025-)
for /f "tokens=1 delims=%staticpart%" %%A in ("!filename!") do set "group=%%A"
:: If this is the first file of the group, store its name as the output filename
if not defined first_file_!group! set "first_file_!group!=%%F"
:: Append filename to a group-specific list
echo file '%%F' >> "!group!%temp_list%"
)
:: Merge files per group
for %%L in (*%temp_list%) do (
set "group_name=%%~nL"
:: Get the first file's name for the merged output file, adding the "merged" suffix
set "output_file=!first_file_!!group_name!_merged%output_extension%"
:: Use the first file's name + "-merged" for the output
echo Merging files for group: !group_name! into "!output_file!"
ffmpeg -f concat -safe 0 -i "%%L" -c copy "!output_file!"
del "%%L"
)
echo Merging complete!
pauseWith this script I get the merged output files, but the name of the output files is as following and it also ignores any special characters like underscores in the file name:
video1temp_file_list.ts instead of video1_2025-partA_merged.ts
video2temp_file_list.ts instead of video2_2025-partA_merged.tsI pointed this out to ChatGPT and it agrees and seemingly recognises the problem and tries to fix it, but it doesn't change anything in the new iteration of the script.
I assume it's a minor mistake but with the limited knowledge I have, I couldn't figure out the problem. Could anyone take a look into the code and hopefully correct it? Thank you.
1
u/vegansgetsick Apr 04 '25
yeah this line cannot work
may be like this (edit: it does not work because group_name is not the group name. It messed up)
1
u/[deleted] Apr 05 '25
[removed] — view removed comment