hashmedia.bat

Off-topic posts of interest to the "Everything" community.
Post Reply
therube
Posts: 4985
Joined: Thu Sep 03, 2009 6:48 pm

hashmedia.bat

Post by therube »

Batch files, grrrr ;-).

for %%i in (%*) do echo %%i | tee -a 0hashmedialist && ffmpeg -v 4 -i %%i -map 0 -c copy -f streamhash -hash md5 - | tee -a 0hashmedialist

So I'm wanting to take, generally 2 - could be more, files & throw them to ffmepg,
and get results to the screen & tee'd to a file.

If I drag & drop the files onto hashmedia.BAT, I seem to be OK (mainly, at least).
But if I
hashmedia.BAT file1.mp4 file2.mp4
, I run into issues with spaces in the file name (among other things).
(ffmpeg)
-v 4
, is probably wrong, but that's immaterial to the issue, & suffices for the moment.
I might not necessarily need
| tee -a
, as in perhaps
>> 0hashmedialist
might suffice (but I didn't test that).

My issue, I believe is again "quotes".

Ideas?

Code: Select all

:: hashmediastream.BAT
:: SjB 02-25-2023

:: drag&drop of *.mp4 works correctly, but hashvideo.bat *.mp4 doesn't - outputting all echo's, but only 1 hash - why?


@echo off

:: for %%i in (%*) do  echo "%%i"
:: for %%i in (%*) do  echo %%i
:: pause
:: exit



:: for %%i in (%*) do echo %%i >>0hashlist && ffmpeg -v 4 -i %%i  -map 0  -c copy  -f streamhash  -hash md5 - >>0hashlist
:: for %%i in (%*) do echo %%i | tee -a 0hashlist && ffmpeg -v 4 -i %%i  -map 0  -c copy  -f streamhash  -hash md5 - >>0hashlist


for %%i in (%*) do echo %%i | tee -a 0hashmedialist && ffmpeg -v 4 -i %%i  -map 0  -c copy  -f streamhash  -hash md5 - | tee -a 0hashmedialist


pause
goto end:


exit



This just gets the MD5 using stream copy mode. There is no decoding.
	ffmpeg -i input.mp4 -map 0:v -c copy -f md5 -

Show video and audio checksums separately
Using the streamhash muxer:
	ffmpeg -i input.mp4 -map 0   -c copy -f streamhash -hash md5 -

Per frame
Using the framehash muxer:
	ffmpeg -i input.mp4 -map 0   -f framehash -



:end
NotNull
Posts: 5461
Joined: Wed May 24, 2017 9:22 pm

Re: hashmedia.bat

Post by NotNull »

Warning: all untested. Willl test if I have some more time.

Don't think it is about the quotes this time.
When you drag/drop files on hashmedia.bat, it will contain the full path and filenames will be quoted when they contain spaces.

However, when you run hashmedia.cmd *.mpg, filenames will be unquoted and that will cause issues.

Furthermore, IIRC tee does only catch the standard output stream and not the error stream.
If you want to catch errors too in your "logfile", redirect error stream to standard uotput stream before feeding it to tee with 2>&1.

Try this (as said untested):

Code: Select all

@echo off
for %%x in (%*) do (echo "%%~fx" & echo ffmpeg.exe parms "%%~fx" more parms ) 2>&1 | tee -a logfile.txt

replace echo ffmpeg.exe with ffmpeg.exe if the ffmeg command-line looks OK
therube
Posts: 4985
Joined: Thu Sep 03, 2009 6:48 pm

Re: hashmedia.bat

Post by therube »

That's got it, thanks.


:: notnull 02-27-2023 (fixed) :-)

:notnull
for %%i in (%*) do (echo "%%~fi" & ffmpeg -v 4 -i %%i -map 0 -c copy -f streamhash -hash murmur3 -) 2>&1 | tee -a c:\out\logfile.txt
pause
NotNull
Posts: 5461
Joined: Wed May 24, 2017 9:22 pm

Re: hashmedia.bat

Post by NotNull »

:shock: I'm surprised there were no major flaws in the code.
(there were major flaws in the textual part; I sneakily removed those ;) )

'My' tee does indeed not support error streams. To check if the same goes for 'your' tee command, try the following command:
cdsfsf | tee-a out.txt

If error streams are not supported, out.txt will be empty/not exist.
If it is supported, it's contents will be something like " ... is not recognized as an internal or external command,
operable program or batch file."
therube
Posts: 4985
Joined: Thu Sep 03, 2009 6:48 pm

Re: hashmedia.bat

Post by therube »

C:\TMP\BRU>kjdkjfdf | tee 0dumytee
'kjdkjfdf' is not recognized as an internal or external command, operable program or batch file.

C:\TMP\BRU>dir 0dum*

Directory of C:\TMP\BRU

File Not Found

C:\TMP\BRU>
NotNull
Posts: 5461
Joined: Wed May 24, 2017 9:22 pm

Re: hashmedia.bat

Post by NotNull »

OK, then the 2>&1 can't be removed.
NotNull
Posts: 5461
Joined: Wed May 24, 2017 9:22 pm

Re: hashmedia.bat

Post by NotNull »

therube wrote: Tue Feb 28, 2023 7:38 pmC:\TMP\BRU>
As this is the off-topic forum anyway, let's make it even more so ;)


Long time ago I was registered on the Bulk Rename Utility forum too. Probably to help you with something, can't remember.

But I do remember a specific forumthread where I suggested a non-BRU solution while stating that this would be possible in BRU itself too (without ever using BRU myself ..).

Just rechecked this thread and it turns out a couple of people tried .. for a month! What have I done :o
Please tell those guys (M/F) I'm sorry.
therube
Posts: 4985
Joined: Thu Sep 03, 2009 6:48 pm

Re: hashmedia.bat

Post by therube »

My issue, I believe is again "quotes".
And if not "quotes", it is (parens). One, or the other, or both, doesn't matter.

if ( | ) | ( )
then drag&drop
fails
:-)

(And yet, the same file name, with parens, from the command line if fine.)


I'm pretty sure it's in the ffmpeg part of things, so I'll have to see what they say about passing variables...
therube
Posts: 4985
Joined: Thu Sep 03, 2009 6:48 pm

Re: hashmedia.bat

Post by therube »

And if not "quotes", it is (parens).
OH, it is where a paren is used where there has been no intervening <space> BEFORE the paren

OH, it is where a PAIR of paren are used, OR, a CLOSING paren is used, where there has been
no intervening <space> BEFORE the paren

OK:

Code: Select all

red (blue).mp3
red(blue.mp3
red blue).mp3
FAIL:

Code: Select all

red(blue).mp3
red blue).mp3
red )blue.mp3
no intervening <space>
isn't that deja vu ?
YES.
chapter01^ - Volume #2 Empire State Building.avi
chapter01^2.avi

if a file name has an '^'
& does NOT have a <sp> in the filename

then -> /SEND TO/ -> xxhash.bat (or MI_COMPARE.BAT), fail!
: xxhsum %*
: mediainfo %*

so a /SEND TO/ eats the ^ - if there are no spaces in the filename?
x^.txt
Error: Counld not open: 'x.txt': no such file or directory.
x^
Error: Counld not open: 'x': no such file or directory.

"x^x .txt", is fine
"x^ x", is fine

-

running the same from a /COMMAND LINE/ is not an issue, ie:
xxhash.bat c*

xxhsum.exe (xxhash) takes a FILE /SPEC/
xxh *, is valid
xxh c*, is valid
xxh *.wmv, is valid


MediaInfo_CL.exe takes a FILENAME (not a wildcard)
- so is diff in that respect, & my .bat passes it %1 & %2, which are FILENAME
mi.exe *, fails
mi.exe x^x, fails
mi.exe "x^x", WORKS


ONLY /SEND TO/ eats '^', & so fails, if no intervening <sp>
(& i /think/ it is <sp> that causes things to just "work" ?)

EH !!!
I'VE BEEN HERE BEFORE & already figured this out, BEFORE - but just *FORGOT* that i had !!!
therube
Posts: 4985
Joined: Thu Sep 03, 2009 6:48 pm

Re: hashmedia.bat

Post by therube »

Batch files, grrrr ;-).

And if not "quotes", it is (parens). One, or the other, or both, doesn't matter.

Don't think it is about the quotes this time.
It's always about the quotes (& batch files) ;-).

So you wrote:

Code: Select all

for %%x in (%*) do (echo "%%~fx" & echo ffmpeg.exe parms "%%~fx" more parms ) 2>&1 | tee -a logfile.txt
And what I wrote was:

Code: Select all

for %%i in (%*) do (echo "%%~fi" & ffmpeg -v 4 -i %%i -map 0 -c copy -f streamhash -hash murmur3 -) 2>&1 | tee -a c:\out\logfile.txt
And if you note, which I didn't at the time, you wrote, "%%~fx", & I wrote, '%%i'.

There is a difference there - quotes ;-).

There are also nuances there which I also wasn't particular keen on.

Using ~, like %%~i - removes quotes (there's that word again).
Where more simply using, %%i (so no ~), does not.


So what happens in different scenarios, differs.
Command line vs. drag & drop, & then also command line with the use of wildcards, or not, & or quoting on the command line, or not.

And with that, with my (old) version, I was still running into issues - sometimes.

Newest version:

Code: Select all

for %%i in (%*) do (echo "%%~fi"  &  ffmpeg -v 4  -i "%%~i"  -map 0  -c copy  -f streamhash  -hash murmur3 -)  2>&1  |  tee -a  c:\out\0hashmedialist
And look, I've added quotes in there ;-)
(more to come... at some point...)


(Heh, look at that, almost 1 year exactly.)
NotNull
Posts: 5461
Joined: Wed May 24, 2017 9:22 pm

Re: hashmedia.bat

Post by NotNull »

I'm a bit confused ..

Is it working now?
therube
Posts: 4985
Joined: Thu Sep 03, 2009 6:48 pm

Re: hashmedia.bat

Post by therube »

Yes.
In order for it to work (in all situations I've come up with - so far), I had to:

add quotes ;-)
& also that squiggly thing, ~

So, "%%~i" (or "%%~fi" is fine too) vs. %%i (no quotes nor ~).

Code: Select all

:: hashmediastream.BAT
:: 02-25-2023 SjB

@echo off
goto start:

:: 02-27-2023 notnull 02-27-2023 (fixed) :-)
:: 03-01-2023 quotes quotes quotes quotes, you need "quotes" in '-i %%i' if the name contains ( parens )
:: --- BUT, that only works from command-line, /STILL/ fails with drag&drop !!!
:: 02-29-2024 actually you do NOT want "quotes" in the '-i %%i' - if the name contains spaces [& spaces are the issue, not parens]
:: 	from command line "x.mp4" -> ""x.mp4"", doubling up the quotes
::      from  drag & drop "x.mp4" -> is always singularly quoted, "x.mp4", so if you add quotes to the -i, you end up with ""x.mp4"", again

:: %%~fi, always gives a full path, regardless of how the file name was gotten, so good from command line, good from d&d
::        so in that respect, to me seems that %%~fi should be used rather then simply %%i, more "fullproof"

:: x.mp4, .\x.mp4 C:x.mp4, C:\OUT\x.mp4
:: 'x (2).mp4', '.\x (2).mp4', 'C:x (2).mp4', 'C:\OUT\x (2).mp4'

:: & or &&, both, 1 or 2, seem to work, 3 or more, fail
:: - why one or the other? [don't recall? seems i used 2, notnull used 1, both seem ? OK]

:: i'm pretty sure murmur3 was a litte faster then md5 (& any of the other available hashes), so no reason not to use that

:: 02-29-2024 ACTUALLY, what you want is %~i & NOT %i, then on top of that, you want "%~i", NOT (simply) %~i
::            %~i   - STRIPS QUOTES !!!
:: so in order for a name containing a space to work, you need to physically enclose the %~1 in QUOTES
::           "%~i"  - in that regard, shouldn't matter if you use %~i or %~fi or ... - so long as they are in quotes

:: so... with "%~i" (or "%~fi" ...)
:: things should work from command-line, and from drag-drop
:: and, with command-line, with a filename, xxx.mp4, a quoted name, "x x.mp4", & also with WILDCARDS, x*.mp4 (or even, x*)



:start
@echo off
goto notnull:


:: for %%i in (%*) do  echo "%%~fi"
:: echo       ---------
:: for %%i in (%*) do  echo "%%i"
:: echo       ---------
:: for %%i in (%*) do  echo %%i

:: pause
:: goto end:


:: for %%i in (%*) do echo %%i >>0hashlist        && ffmpeg -v 4 -i %%i  -map 0  -c copy  -f streamhash  -hash md5 - >>0hashlist
:: for %%i in (%*) do echo %%i | tee -a 0hashlist && ffmpeg -v 4 -i %%i  -map 0  -c copy  -f streamhash  -hash md5 - >>0hashlist

:: drag&drop of *.mp4 works correctly, but hashvideo.bat *.mp4 doesn't - outputting all echo's, but only 1 hash - why?
:: for %%i in (%*) do echo %%i | tee -a 0hashmedialist && ffmpeg -v 4 -i %%i  -map 0  -c copy  -f streamhash  -hash md5 - | tee -a 0hashmedialist

:: note that with the ffmpeg command the closing '-' is NEEDED



:notnull
for %%i in (%*) do (echo "%%~fi"  &  ffmpeg -v 4  -i "%%~i"  -map 0  -c copy  -f streamhash  -hash murmur3 -)  2>&1  |  tee -a  c:\out\0hashmedialist

pause
goto end:


:: wonder if i ought not use '-i %%~fi', above?  possibly ? issue with that & LFN ? otherwise might be more fullproof?
:: 02-29-2024 yes !!, well i /would/ & seemingly /should/ - based on (see) quotes.bat, but it does not work correctly, all the time, if i do - WHY?
:: "%%~fi" is fine with the echo, but, presumably, farts on the ffmpeg.exe end - depending on the file name (containing <space> or not) ?

:: thinking i needed quotes with the -i, i ended up messing things up - i should have kept notnull's original, i think
:: for %%i in (%*) do (echo "%%~fi"  &  ffmpeg -v 4  -i "%%i"  -map 0  -c copy  -f streamhash  -hash murmur3 -)  2>&1  |  tee -a  c:\out\logfile.txt
:: "%%i" causes issues - at times, doubling the quotes in the name, so ""x.mp4"", which then may fail [ffmpeg]

:: so running into issue with my quoted version (up), i used this instead (below)
:: & that worked, though probably a bit less efficient ? then notnull's (two tee's instead of a single)
:: for %%i in (%*) do echo %%i | tee -a 0hashmedialist && ffmpeg -v 4 -i %%i  -map 0  -c copy  -f streamhash  -hash md5 - | tee -a 0hashmedialist




ffmpeg -i input.mp4 -map 0:v -c copy -f md5 -
	- This just gets the MD5 (murmur3) hash using stream copy mode. There is no decoding.

ffmpeg -i input.mp4 -map 0   -c copy -f streamhash -hash md5 -
	- Show video and audio checksums separately
	- Using the streamhash muxer:

ffmpeg -i input.mp4 -map 0   -f framehash -
	- Per frame
	- Using the framehash muxer:



:end
NotNull
Posts: 5461
Joined: Wed May 24, 2017 9:22 pm

Re: hashmedia.bat

Post by NotNull »

therube wrote: Fri Mar 01, 2024 4:39 pm add quotes ;-)
& also that squiggly thing, ~
The qquiggly thing is very usefule when not sure if something, like path, is quoted or not.
It removes the surrounding double quotes, so you can put the there yourself if needed.
This prevents paths like ""c:\windows"" (with double-double quotes) which breaks most code.
Post Reply