Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
adee
Femeie
24 ani
Mures
cauta Barbat
24 - 59 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [VIDEO] Quickly Split Multiple TS Files By Given Time Intervals Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
Mrrrr
AdMiN

Inregistrat: acum 18 ani
Postari: 2247
I use a .BAT file that ChatGPT wrote to split several video files in a desired folder.

First things first, the code will define the desired paths. All my videos are under a path and then subfolders that are named with the first part of the videos they contain, up until a dash followed by a year then a minus character, like this: _YEAR-

Example of video file naming pattern (wildcard represents any characters allowed by Windows in file naming):
*_2025-01-24_21-23-22_105.ts

So instead of the wildcard, it could be:
ab1234_2025-01-24_21-23-22_105.ts

As I said above, all my videos are under subfolders in a path:
D:\My Videos

The subfolder names are colored in aqua:
ab1234_2025-01-24_21-23-22_105.ts
_ac264as_2025-02-01_22-01-02_257.ts
12abcd____2025-02-03_21-27-26_161.ts
and so on


The lines contained in the videos.txt file (lines created using Excel's =TEXTJOIN() function, separating intervals with commas.

E.g., in A2 I add the following function:
=TEXTJOIN(",";1;B2:H2)

In B2:B4 I add the names of the video files:
ab1234_2025-01-24_21-23-22_105.ts
_ac264as_2025-02-01_22-01-02_257.ts
12abcd____2025-02-03_21-27-26_161.ts

In C2:H4 I add the time intervals, 1 per cell, as many as 6 per video (that's why it goes to H2, but any number of intervals can be completed except none). So the example is as follows:
C2: 00:04:06.000-00:12:17.000
C3: 00:18:30.000-00:27:26.000
D3: 00:37:10.000-00:48:23.000
C4: 00:07:43.000-00:22:44.000


So this is an example of content of the videos.txt file:
ab1234_2025-01-24_21-23-22_105.ts,00:04:06.000-00:12:17.000
_ac264as_2025-02-01_22-01-02_257.ts,00:18:30.000-00:27:26.000,00:37:10.000-00:48:23.000
12abcd____2025-02-03_21-27-26_161.ts,00:07:43.000-00:22:44.000


Error handling:
If no time interval is mentioned for a file, a "Skip" message will be displayed.
If a video file does not exist, a "Skip" message will be displayed.


Updated version:
The code will display the name of the .ts video file, eg. 12abcd____2025-02-03_21-27-26_161.ts
After you press a key, it will display the name of the extracted folder.
If you agree with the folder name, just press Enter, but if not (let's say extracted folder name would be 12abcd instead of 12abcd___):
- type sX - s stands for start, and X for 1 character or more to add before the folder name (in this example we don't need characters before 12abcd)
- type eX - e stands for end, and X for 1 character or more to add after the folder name (in our example, you could type e___ to add the proper suffix)
- type nX - n stands for new, and X for the desired folder name, an entire folder name to replace the previous (eg completely change 12abcd to DCBA21 with nDCBA21)


The code below can still be improved, I know.
(this code is filled with troubleshooting echoes)[/color]



@echo off
setlocal enabledelayedexpansion

REM Input file containing video names and time ranges
set "input_file=D:\Videos\videos.txt"

REM Path to ffmpeg (adjust if needed)
set "ffmpeg=D:\Videos\ffmpeg.exe"

REM Root path for all video folders (updated to "My Videos")
set "root_path=D:\Videos\My Videos"

REM Path to the videos file (adjust if needed)
set "videos_file=%input_file%"

REM echo Input File: %input_file%
REM echo FFMPEG Path: %ffmpeg%
REM echo Root Path: %root_path%
REM echo Videos File: %videos_file%
REM pause


REM Process each line in the input file

for /f "usebackq tokens=* delims=" %%A in ("%videos_file%") do (
    set "line=%%A"

cls
   
    echo Processing Line: !line!
    pause

    REM Initialize flag to skip to next file if needed
    set "skip_file=false"

    REM Split the line into video file and ranges
    for /f "tokens=1* delims=," %%B in ("!line!") do (
        set "video=%%B"
        set "ranges=%%C"

       REM echo Video: !video!
        REM pause


REM Extract the folder name (everything before the first _YEAR- sequence)
set "folder="
for /f "tokens=1* delims=_" %%X in ("!video!") do (
    set "folder=%%X"
   
REM echo Folder: !folder!
REM pause   

   
    REM Process the remaining part of the filename for the year-based pattern
    for /f "tokens=1 delims=-" %%Z in ("%%Y") do (
        set "folder=!folder!_%%Z"
    )
)

REM Remove the last 5 characters (_YEAR)
set "folder=!folder:~0,-5!"

REM Echo folder name to debug
REM echo Folder: !folder!
REM pause


set /p modify="Enter 'sX' to prefix with 'X', or 'eX' to suffix with 'X', or 'nX' to change entire name to 'X' (or Enter to keep it as is) [!folder!]: "

REM Check if input starts with "s" or "e" or "n" and add characters or replace entirely
if /i "!modify:~0,1!"=="s" set "folder=!modify:~1!!folder!"
if /i "!modify:~0,1!"=="e" set "folder=!folder!!modify:~1!"
if /i "!modify:~0,1!"=="n" set "folder=!modify:~1!"

REM Echo final folder name to debug
REM echo Final folder name: !folder!
REM pause


        REM Build the full path to the original video
        set "video_path=%root_path%\!folder!\!video!"

        rem Check if the video file exists
        if not exist "!video_path!" (
            echo File !video_path! not found. Going to the next file.
            echo.
            set "skip_file=true"
        )

        REM Set the output folder path (same as the subfolder containing the original video)
        set "output_folder=%root_path%\!folder!"

        REM Check if no time intervals are provided
        if not defined ranges (
            echo No splits mentioned, no splits to be done. Skipping!
            set "skip_file=true"
        )

        REM If skip_file is true, go to the next file
        if "!skip_file!"=="true" (
            echo Skipping to next file...
            echo.
            rem Reset the flag for the next iteration
            set "skip_file=false"
            goto :next_file
        )

        REM Process the splits
        for %%D in (!ranges!) do (
            for /f "tokens=1,2 delims=-" %%E in ("%%D") do (
                set "start=%%E"
                set "end=%%F"

                REM Remove any leading/trailing spaces
                set "start=!start: =!"
                set "end=!end: =!"

                REM Replace colons with dots for filenames
                set "start_clean=!start::=.!"
                set "end_clean=!end::=.!"

                if defined start if defined end (
                    echo Processing !video_path!: !start! to !end!
                    "%ffmpeg%" -i "!video_path!" -ss !start! -to !end! -c copy "!output_folder!\!video!-!start_clean!-!end_clean!.ts"
                )
            )
        )
    )

)

    :next_file

echo Done.
pause


_______________________________________


pus acum 3 saptamani
   
Mrrrr
AdMiN

Inregistrat: acum 18 ani
Postari: 2247
updated code

_______________________________________


pus acum 2 saptamani
   
Pagini: 1  

Mergi la