ffmpeg Batch Video Format Conversion

📢 This article was translated by gemini-2.5-flash

Intro

I stumbled upon YouTube-dl and found it super handy for downloading entire channels. The only catch was the webm format it outputted isn’t always compatible. So, I looked into ffmpeg commands for format conversion.

Config File

Later, I realized a simple config file tweak would do the trick. Just add this line to your config:

1
--merge-output-format mp4

Here’s my full config file:

1
-o 'C:/Users/yexca/Downloads/Video/%(uploader)s/%(upload_date)s%(title)s%(id)s.%(ext)s' --merge-output-format mp4

Single Command

You can quickly transcode with this command:

1
ffmpeg -i before.webm after.mp4

But doing it one by one is way too slow, which led me to think about batch processing.

Batch Processing

Create a new Notepad file and paste this code:

1
for %%a in (*.webm) do ffmpeg -i "%%~a" -vcodec copy -f mp4 "%%~na.mp4"

Here, (*.webm) is the original file type, and \"%%~na.mp4\" is the desired output file type.

Save it as run.bat, drop it into the target folder, and run it.

However, having to edit it every time for different files isn’t super practical.

DOS Script

So, I dove a bit into .bat files and quickly whipped up the following script (FYI: GB2312 encoding is needed for proper Chinese output in the command line).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
echo off
:0
echo Please enter a number to select an operation
echo 1. Transcode
echo 2. Delete
echo 3. Exit
set /p choice=Select an option:
goto %choice%
:1
set /p before=Enter original file format (e.g., webm):
set /p after=Enter target file format (e.g., mp4):
echo Starting transcode from %before% to %after%
for %%a in (*.%before%) do ffmpeg -i "%%~a" -vcodec copy -f %after% "%%~na.%after%"
echo Transcoding complete.
goto 0
:2
set /p delet=Enter file format to delete (e.g., webm):
echo Starting deletion of %delet% files
del *.%delet%
del %delet%
echo Deletion complete.
goto 0
:3
exit

Just drop this in the desired folder and run it.

References

Practical YouTube-dl Tips · eisen blog

20+ FFmpeg Command Examples for Beginners - Zhihu

FDM’s Down, youtube-dl’s Sluggish – Doomsday for Content Editors?! Comment by AnnMilne

Batch Transcoding FFmpeg with .bat Commands - Wuren’s Notes

Learning Bat Commands - Sunshine & Rain - Blog园

Detailed Explanation of bat set Command_Python Veteran Noob’s Blog - CSDN Blog

This post is licensed under CC BY-NC-SA 4.0 by the author.