← Back to archive
Programming Notes

Batch Script FFMPEG Video Cutting

ffmpeg

I originally followed a method found online, but after cutting the videos, they would not play on H5. Further investigation revealed that each browser has requirements for which H.264 encodings it supports, and iOS Safari in particular is stricter—so I record it here as follows:

setlocal enabledelayedexpansion
set x=0
for %%i in (.azhi\*) ^
do (
      
        set /a x+=1
        set new_dir=G:\\course\\bazhi\\!x!
        mkdir !new_dir!
        ffmpeg -i %%i -vcodec h264_nvenc -f hls !new_dir!\\index.m3u8
)

In ffmpeg, the -vcodec parameter specifies the codec; after installing NVIDIA’s CUDA, h264_nvenc can use the GPU to accelerate encoding.

Batch scripting under Windows is rather cumbersome, not as easy to understand as Python-style syntax. A simple loop counter requires enabling delayed variable expansion, and when assigning values, the variables must be wrapped with ! rather than %.

If you want to understand this more thoroughly, it is quite complex—search for the article “The Meaning and Differences Between Various Forms of Variables: %0, %i, %%i, var, %var%, !var!”.

Written by Master Sanfu on March 7, 2022. Please credit the source if you share.

Translation Notice: This English version was translated with AI assistance. Specialized, historical, religious, or culturally sensitive terms may contain nuances, inaccuracies, or debatable wording. In case of ambiguity or discrepancy, the original Chinese text shall prevail.