2017-06-22 213 views
0

我一直试图使用%% f来操作目录中每个文件上的脚本,但是当通过脚本引用它时,%% f和%%〜nf不会似乎工作。我限制了编程经验,我试图制作更有用的this video formatting tutorial版本。批量存储%% f作为变量

所以我想存储%% f和%%〜nf作为变量来引用脚本的其余部分,尽管我无法解决这个问题。

@echo off 
set /p FORMAT="Enter File Format: " 
FOR %%f IN (*.%FORMAT%) DO echo %%f 
set TEST=%%f 
echo %TEST% 
cmd/k 

如果我可以存储这些将解决我的问题,但是这是我想要做的更长的形式,这个脚本工作,如果我有用户输入文件手动到一个变量(% VIDEO%= %% f和%〜nf)。虽然这远非理想。

@echo off 
set /p FORMAT="Enter File Format: " 

FOR %%f IN (*.%FORMAT%) DO (
::IDFILE 
for /F "delims=" %%I in ('C:\ffmpeg\bin\ffprobe.exe -v error -show_entries format^=filename -of default^=noprint_wrappers^=1:nokey^=1 "%%f"') do set "FILENAME=%%I" 

for /F "delims=" %%I in ('C:\ffmpeg\bin\ffprobe.exe -v error -select_streams v:0 -show_entries stream^=codec_name -of default^=noprint_wrappers^=1:nokey^=1 "%%f"') do set "Vcodec=%%I" 

for /F "delims=" %%I in ('C:\ffmpeg\bin\ffprobe.exe -v error -select_streams a:0 -show_entries stream^=codec_name -of default^=noprint_wrappers^=1:nokey^=1 "%%f"') do set "Acodec=%%I" 

echo %FILENAME% is using %Vcodec% and %Acodec% codecs 

if %Vcodec% == h264 (echo DO NOT CONVERT VIDEO) else (echo CONVERT VIDEO) 
if %Acodec% == ac3 (echo DO NOT CONVERT AUDIO) else (echo CONVERT AUDIO) 
timeout /t 5 

:: COPY V FIX A 
if %Vcodec% == h264 if not %Acodec% == ac3 (echo Copying Video, Converting Audio 
timeout /t 5 
C:\ffmpeg\bin\ffmpeg.exe -i "%%f" -map 0 -vcodec copy -scodec copy -acodec ac3 -b:a 640K "%%~nf"-AC3.mkv) 

:: FIX V COPY A 
if not %Vcodec% == h264 if %Acodec% == ac3 (echo Converting Video, Copying Audio 
timeout /t 5 
C:\ffmpeg\bin\ffmpeg.exe -i "%%f" -map 0 -vcodec libx264 -scodec copy -acodec copy "%%~nf-"h264.mkv) 

:: FIX V FIX A 
if not %Vcodec% == h264 if not %Acodec% == ac3 (echo Converting Video, Converting Audio 
timeout /t 5 
C:\ffmpeg\bin\ffmpeg.exe -i "%%f" -map 0 -vcodec libx264 -scodec copy -acodec ac3 -b:a 640K "%%~nf"-h264-AC3.mkv) 

:: COPY V COPY A 
if %Vcodec% == h264 if %Acodec% == ac3 (echo "Doesn't require any Conversion") 
) 
pause 
cmd/k 

回答

2

通过想念它!

@echo off 
set /p FORMAT="Enter File Format: " 

FOR %%f IN (*.%FORMAT%) DO (
set "video=%%f" 
set "namepart=%%~nf" 
call :idfile 
) 
pause 
goto :eof 

.. then the remainder of your code, except Modify the comment `::IDfile` to a 
    label `:idfile` (1 fewer colon) remove the `)` before the `pause` 
    and replace the `cmd /k` with `goto :eof` and replace `%%f`, `%%~nf` with 
    `%video%`, `%namepart%` respectively. 

两个主要问题与您的代码:delayed expansion(约合其中有这么多,很多文献 - 只是用在最上面一栏的search设施)和UE的`:: - 风格注释(这是在事实上已损坏的标签)在代码块(括号内的一系列行)中不允许使用标签。

我建议的更改只是将您的代码的整个肉变成一个子程序。

您还可以(并且可能优选地)的代码变更为

@echo off 
set /p FORMAT="Enter File Format: " 

FOR %%f IN (*.%FORMAT%) DO call :idfile "%%f" 

pause 
goto :eof 

然后,代替在子程序使用videonamepart,使用%~1%%~n1(因为%1是子程序的第一个参数; ~删除引号(这里确保正确处理,如果%%f包含空格))

+0

非常感谢帮助我现在更接近了,但是当试图使用%%〜n1作为实际转换名称部分I已经尝试使用%%〜n1,尽管它似乎没有正确读取。 ':: COPY V修复 如果%了vcodec%== H264若不%Acodec%== AC3(回声复制视频,转换音频 回声 “%%〜N1”(也试过没有 “”) 暂停 C:\ ffmpeg \ bin \ ffmpeg.exe -i“%〜1”-map 0 -vcodec copy -scodec copy -acodec ac3 -b:a 640K“%%〜nf”-AC3.mkv)' and it读回''%%〜n1''或'%%〜n1'并产生称为%%〜n1的文件 – RyanMe321

0

尝试这样的:

@echo off 
set /p FORMAT="Enter File Format: " 
FOR %%f IN (*.%FORMAT%) DO ( 
    echo %%f 
    set TEST=%%f 
) 
echo %TEST% 

@echo off 
setlocal enableDelayedExpansion 
set /p FORMAT="Enter File Format: " 
FOR %%f IN (*.%FORMAT%) DO ( 
    echo %%f 
    set TEST=%%f 
    echo !TEST! 
)