Code: Select all
00:27:34.45 1616 kbs mpeg4 708x480 334 Mb --- Alexander the Great.avi
00:03:03.00 264 kbs wmv3 320x240 6 Mb --- Apples
00:57:31.68 605 kbs h264 640x360 261 Mb --- Europe.mp4
00:00:01.00 9213 kbs msvid 648x520 1 Mb --- H21 similarity.avi
00:08:49.67 128 kbs h264 1000x562 9 Mb --- Relational model.mp4
00:04:45.50 304 kbs flv1 320x240 11 Mb --- Robot Chicken.flv
But in fact this sample output demonstrates the one bug I found in my batch file. The file name "Apples" at the end of the 2nd line is really supposed to be "Apples & Oranges.avi", although the first part of that line correctly specifies the video duration and other parameters. I think the cause of this bug is the fact that sed treats the ampersand as a special character which is used to represent the last matched pattern. Although to add to my confusion the dos box also prints this error message:
Code: Select all
'Oranges.wmv' is not recognized as an internal or external command, operable program or batch file.
Since I'm just piping the output to a text file I don't see how this text gets treated as a command unless dos's special treatment of ampersand is also involved. With files that don't contain ampersnad characters this problem never happens.
My attempt to fix this bug was to replace the & in the file name with the word "and".
If that worked (which it didn't), I would instead replace it with \& may also work since that is the way sed escapes this character.
To do that, I pulled the main work out of the loop into a subroutine (Foo) which is called with three arguments (Full file name with path, file name without path, file size)
Then as you can see I attempted to replace the & in the file name (2nd argument) using a set command in the fourth line of the Foo routine. However this string replacement does not work. When I change the string replacement so that it changes some other string to the word "and" then it does work. This indicates that dos's special use of & is also a problem here. So I tried adding the "setlocal enableDelayedEpansion command and using exclamation marks instead of percent signs as well as adding the quotes resulting in the code below. I also tried prefixing the & with 1,2,3,4,or 5 ^ characters which is a way to escape the special meaning of &, but to no avail. In fact I tried many dozen variations of this set command, but none of them would work. My batch file is shown below.
Code: Select all
for /R %%G IN (*.mp4 *.avi *.mkv *.flv *.wmv *.mpg *.mov) do call :Foo "%%G" "%%~nxG" %%~zG
goto End
:Foo
set z=%2
set z=%z:~1,-1%
setlocal enableDelayedExpansion
set "z=!z:&= and !"
set /A sz = (%3 + 500000) / 1000000
set sz= %sz%
set sz=%sz:~-5%
ffprobe -i %1 2>&1 | sed -n -r^
"/Duration:/{s/.*tion: ([^,]*).*(.... kb).*/\1 \2sQQ/;h};/Video:/{H;x;s/QQ\n.*eo: (.....)[^,]*,[^,]*.*(....x....).*/ \1 \2 %sz% Mb --- %z%/p}" >> vid.txt
goto :eof
:End
REM - Remove commas
sed -r "s/,(.*)--/ \1--/; s/,(.*)--/ \1--/" vid.txt > vid2.txt
REM - sort alphabetically by file name (which starts in column 47)
sort /+47 vid2.txt > vidtimer.txt
type vidtimer.txt
Thanks in advance for any suggestions you may think of.
~Paul