Skip to content

Instantly share code, notes, and snippets.

@greatghoul
Created July 16, 2025 15:04
Show Gist options
  • Save greatghoul/556c9f129efb25ac10eea38e97a0a42e to your computer and use it in GitHub Desktop.
Save greatghoul/556c9f129efb25ac10eea38e97a0a42e to your computer and use it in GitHub Desktop.
查找 Windows 下载文件夹中的 .webm 文件,选择后转换为 mp4
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
:: 防止窗口意外关闭
title WebM to MP4 Converter
echo Starting WebM to MP4 Converter...
echo.
:: 设置搜索目录
set "WATCH_DIR=C:\Users\greatghoul\Downloads"
:: 检查目录是否存在
if not exist "%WATCH_DIR%" (
echo.
echo ======================================
echo ERROR: Directory does not exist
echo ======================================
echo Directory: %WATCH_DIR%
echo.
echo Press any key to exit...
pause >nul
exit /b 1
)
:: 查找所有 .webm 文件(按创建时间倒序)
echo Searching for .webm files in: %WATCH_DIR%
set "FILE_COUNT=0"
:: 使用更简单的方法:直接使用 dir 命令按创建时间倒序
for /f "delims=" %%f in ('dir /b /tc /o-d "%WATCH_DIR%\*.webm" 2^>nul') do (
set /a FILE_COUNT+=1
set "FILE_!FILE_COUNT!=%WATCH_DIR%\%%f"
set "FILENAME_!FILE_COUNT!=%%f"
)
:: 检查是否找到文件
if %FILE_COUNT%==0 (
echo.
echo ======================================
echo No .webm files found
echo ======================================
echo Directory: %WATCH_DIR%
echo.
echo Press any key to exit...
pause >nul
exit /b 1
)
:: 显示文件列表
echo.
echo Found %FILE_COUNT% .webm file(s):
echo.
for /l %%i in (1,1,%FILE_COUNT%) do (
echo %%i. !FILENAME_%%i!
)
:: 让用户选择文件
echo.
set /p "CHOICE=Please select a file number (1-%FILE_COUNT%): "
:: 验证用户输入
if "%CHOICE%"=="" (
echo.
echo ======================================
echo Invalid selection - Empty input
echo ======================================
echo Press any key to exit...
pause >nul
exit /b 1
)
:: 检查选择是否在有效范围内
if %CHOICE% LSS 1 (
echo.
echo ======================================
echo Invalid selection - Number too small
echo ======================================
echo Valid range: 1-%FILE_COUNT%
echo Press any key to exit...
pause >nul
exit /b 1
)
if %CHOICE% GTR %FILE_COUNT% (
echo.
echo ======================================
echo Invalid selection - Number too large
echo ======================================
echo Valid range: 1-%FILE_COUNT%
echo Press any key to exit...
pause >nul
exit /b 1
)
:: 获取选择的文件
set "SELECTED_PATH=!FILE_%CHOICE%!"
set "SELECTED_FILE=!FILENAME_%CHOICE%!"
echo.
echo Selected file: %SELECTED_FILE%
echo File path: %SELECTED_PATH%
:: 生成输出文件路径(使用延迟变量展开处理空格和特殊字符)
set "OUTPUT_FILE=!SELECTED_PATH:.webm=.mp4!"
:: 检查 ffmpeg 是否可用
echo Checking ffmpeg availability...
set "FFMPEG_PATH=C:\Applications\ffmpeg\bin\ffmpeg.exe"
if exist "%FFMPEG_PATH%" (
set "FFMPEG_CMD=%FFMPEG_PATH%"
) else (
:: 尝试在系统路径中查找
where ffmpeg >nul 2>&1
if !errorlevel! equ 0 (
set "FFMPEG_CMD=ffmpeg"
) else (
echo.
echo ======================================
echo ERROR: ffmpeg not found
echo ======================================
echo ffmpeg.exe not found in system PATH or default location
echo Please ensure ffmpeg is installed and added to system PATH
echo.
echo Press any key to exit...
pause >nul
exit /b 1
)
)
:: 执行转换
echo.
echo Converting file: !SELECTED_PATH!
echo Output file: !OUTPUT_FILE!
echo.
"!FFMPEG_CMD!" -y -i "!SELECTED_PATH!" -c:v libx264 -c:a aac "!OUTPUT_FILE!"
:: 检查转换结果
if exist "!OUTPUT_FILE!" (
echo.
echo ======================================
echo Conversion completed successfully!
echo ======================================
echo Output file: !OUTPUT_FILE!
echo.
) else (
echo.
echo ======================================
echo Conversion failed!
echo ======================================
echo Please check the output above for error details.
echo.
)
echo.
echo ======================================
echo Script execution completed.
echo Press any key to close this window...
echo ======================================
pause >nul
echo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment