1.

Solve : Detecting Filetype of Drag & Drop file?

Answer»

So i made a simple, little AAC to MP3 converter including the adding of MP3 Tags.
For this i use Faad (A part of FAAC), Lame & Tag

http://sourceforge.net/projects/faac/
http://www.rarewares.org/aac-decoders.php#faad2-win
http://lame.sourceforge.net/
http://www.synthetic-soul.co.uk/tag/

After writing the batch i however discovered that Faad2 is also capable of extracting the AAC from certain types of containers like MP4 files,  completely eliminating the need for me to extract those manually before feeding them to my batch.
Unless i don't have the MP4 to begin with but just the AAC file.

Sadly however this does cause an issue with my already written batch if i try to Drag & Drop a MP4 file on to the batch.

The batch i wrote renames the file created by Faad, the wav file, since it keeps the file extension of the AAC in the filename of the freshly created WAV file.
So for example; "Artist - Song.AAC" becomes "Artist - Song.AAC.WAV"

And Lame does the same with the freshly created MP3 file, that is then named "Artist - Song.WAV.MP3"

So to combat that i let it perform a rename after each process, after which Tag.exe adds tags to the MP3 and my MP3 file is done.

However when a MP4 file is dragged & dropped onto the batch instead of an AAC file.. the rename code, of course, fails.

Now the easy solution would be to created a seperate batch for MP4 files as well and use it along side the batch for the AAC files, but i'd vastly prefer to have all the code in 1 batch file instead.
So to ACCOMPLISH this i thought of letting the batch file detect what the filetype is of the file dropped onto it, and then to let it perform a GOTO to the part of the code for the AAC file and the MP4 file respectively.
I tried messing around with the IF command but to no avail.. I also tried the FOR command as well but i understand that command's capabilities even less.

So my QUESTION is simple; how do i make a batch file detect the filetype of whatever file is dragged & dropped onto it ?

Here's my code so far, please don't laugh as i'm no expert in how to utilize batch commands efficiently.

Code: [Select]ECHO OFF
color 81
:Original version by NiTeCr4Lr
:title AAC to MP3 CLI
faad -b 2 -l 2 -o %1.wav %1
title AAC to MP3 CLI
move /-Y %1.wav "C:\Program Files\AAC to MP3 CLI"
cls
ren *.aac.wav *.
ren *.aac *.wav
del %1
cls
lame -cbr -b320 -m j -q0 *.wav
del *.wav
ren *.wav.mp3 *.
ren *.wav *.mp3
Tag --auto --scheme "A - T" *.mp3
move /-Y *.mp3 "C:\Documents and Settings\NiTeCr4Lr\Bureaublad"
exit

Note:

Title after Faad since Faad hijackes the title of the Batch window and the title resets after Faad closes.

In addition, i realize the moving of the files seems inefficient but i let it do so since the shortcut to the batch file resides on my desktop, where more often than not a lot more AAC, MP4 & MP3 files reside as well. And i don't want my code touching any of those at all, especially not the "Del" part of the code
(Ok, ok.. It's also because somewhere in the code "%1" wouldn't work properly due to some path problem.. I'm very much a noob at using things like "%1" correctly.)

P.S. If anybody would be crazy enough to want to use my inefficient code to convert MP4/AAC files to MP3 files in a fairly cumbersome way, feel free to do so.
A thank you and a reference if posted elsewhere would however be appreciated. Quote

Drag & Drop a MP4 file on to the batch.
What does that mean?    Quote from: Geek-9pm on March 25, 2015, 11:56:55 PM
What does that mean?   

It means that if you have a batch file on your desktop, you then drag a MP4 or AAC file with your mouse and let the mouse button go (drop) it when it's on the batch file icon.
So the batch then takes the file and processes it.This modification should process the files - inside the same folder where the original files are and handle any supported extension.

If your executables are not on the path, then they will need the FULL location SPECIFIED to the files
- so if they are all located on the desktop then it should work with "%userprofile%\desktop\"


"%userprofile%\desktop\faad.exe" rest of the line
"%userprofile%\desktop\lame.exe" rest of the line
"%userprofile%\desktop\Tag.exe" rest of the line


and if they are elsewhere and you need a guide, then let us know where they are located.

This should also process more than one dragged file - one after another.
Test it with some sample files.

Code: [Select]echo off
:: Original version by NiTeCr4Lr
:: title AAC to MP3 CLI
title AAC to MP3 CLI
color 81
cls
:begin
pushd "%~dp1" || (echo error - the target folder "%~dp1" seems to be missing & pause & goto :EOF)
faad -b 2 -l 2 -o "%~n1.wav" "%~1"
lame -cbr -b320 -m j -q0 "%~n1.wav"
del "%~n1.wav"
Tag --auto --scheme "A - T" "%~n1.mp3"
move /-Y "%~n1.mp3" "C:\Documents and Settings\NiTeCr4Lr\Bureaublad"
popd
shift
if not "%~1"=="" goto :begin
Quote from: NiTeCr4Lr on March 25, 2015, 11:42:19 PM
The batch i wrote renames the file created by Faad, the wav file, since it keeps the file extension of the AAC in the filename of the freshly created WAV file.
So for example; "Artist - Song.AAC" becomes "Artist - Song.AAC.WAV"

And Lame does the same with the freshly created MP3 file, that is then named "Artist - Song.WAV.MP3"
No they do not.  As Foxidrive has pointed out in his code, you coded it that way! Quote from: Squashman on March 26, 2015, 08:05:45 AM
No they do not.  As Foxidrive has pointed out in his code, you coded it that way!

I figured i'd test what you said by dragging and dropping files on to Faad.exe & Lame.exe without the batch.
You're right about Faad, no file extension from the previous filetype in the new file's name.
With Lame however, you were right yet wrong :p

Since i've been using just the Lame part of the batch (no pun intended, that was bound to happen :p) for a couple of years now, my version of Lame is "3.98.2".
The version you probably tested (or know of by experience that it doesn't add the previous file extension ) is i'm guessing "3.99.5"
Since when i dropped a WAV file on the 3.99.5 version, it like Faad didn't include the previous extension anymore, yay!

However, after testing the difference between 3.98.2 and 3.99.5 it became clear i'd prefer to keep using the 3.98.2 version..
Under the same conditions with the same high quality parameters 3.98.2 took 36 seconds to encode a MP3 where 3.99.5 took 1 Minute & 15 Seconds.... Quote from: foxidrive on March 26, 2015, 07:14:54 AM
This modification should process the files - inside the same folder where the original files are and handle any supported extension.

If your executables are not on the path, then they will need the full location specified to the files
- so if they are all located on the desktop then it should work with "%userprofile%\desktop\"


"%userprofile%\desktop\faad.exe" rest of the line
"%userprofile%\desktop\lame.exe" rest of the line
"%userprofile%\desktop\Tag.exe" rest of the line


and if they are elsewhere and you need a guide, then let us know where they are located.

Thank you so much for your effort and time !
I indeed encounter a path issue.
The shortcut to the batch file is on my desktop but it links to the batch, and all the other files like faad, lame & tag, in the following path
"C:\Program Files\AAC to MP3 CLI"
(I like to keep all finished projects there :p)

I figured a simple chdir command at the beginning could fix that very easily..
Though, i saw the "%~dp1" you used at the pushd command, and since i'm not familiar with pushd
(it seems like a.. pipe? thing.. Something i've been wanting to use since before i started adding the AAC/Faad & Tag parts to the original code, which was only Lame encoding, but didn't have the know how for :p)

With some logical thinking i'm guessing the "%~dp1" take the d & p of "%1" right ?
So that's why it's stuck on my desktop ("Bureaublad" in Dutch) since "%1" is on there :p?

I'll wait for your reply before i start to mess around with the code of which those few new things i don't fully understand ..

I'm trying to confirm and learn from your code, though :p.
Also i'm guessing that "popd" is an 'off-switch' for "pushd" if you will  ?

I didn't even at all know about the Shift command and how it worked :p! I did a Shift/? seems handy ! Quote
Substitution of batch parameters (%n) has been enhanced.  You can
 now use the following optional syntax:

     %~1         - expands %1 removing any surrounding quotes (")
     %~f1        - expands %1 to a fully qualified path name
     %~d1        - expands %1 to a drive letter only
     %~p1        - expands %1 to a path only
     %~n1        - expands %1 to a file name only
     %~x1        - expands %1 to a file extension only
     %~s1        - expanded path contains short names only
     %~a1        - expands %1 to file attributes
     %~t1        - expands %1 to date/time of file
     %~z1        - expands %1 to size of file
     %~$PATH:1   - searches the directories listed in the PATH
                    environment variable and expands %1 to the fully
                    qualified name of the first one found.  If the
                    environment variable name is not defined or the
                    file is not found by the search, then this
                    modifier expands to the empty string

 The modifiers can be combined to get compound results:

     %~dp1       - expands %1 to a drive letter and path only
     %~nx1       - expands %1 to a file name and extension only
     %~dp$PATH:1 - searches the directories listed in the PATH
                    environment variable for %1 and expands to the
                    drive letter and path of the first one found.
From what you have said, all it needs is the change in the pushd command

It pushes a directory onto a stack, changes to it, and the popd pops it back off again as you gathered.
Pushd can have many levels but cd /d "C:\Program Files\AAC to MP3 CLI" will work here just as well
above the :begin label - and remove the pushd/popd lines.

Squashman has pointed out the useful help in the for /?

Code: [Select]echo off
:: Original version by NiTeCr4Lr
:: title AAC to MP3 CLI
title AAC to MP3 CLI
color 81
cls
:begin
pushd "C:\Program Files\AAC to MP3 CLI"
faad -b 2 -l 2 -o "%~n1.wav" "%~1"
lame -cbr -b320 -m j -q0 "%~n1.wav"
del "%~n1.wav"
Tag --auto --scheme "A - T" "%~n1.mp3"
move /-Y "%~n1.mp3" "C:\Documents and Settings\NiTeCr4Lr\Bureaublad"
popd
shift
if not "%~1"=="" goto :begin
Quote from: Squashman on March 26, 2015, 12:54:31 PM

Using the code supplied by foxidrive i don't get how to adjust that even with the info in Squashman's post in order to get it to get it to run for the path "C:\Program Files\AAC to MP3 CLI" instead of the desktop ..

Maybe i'm having a senior moment right now, very likely in fact Quote from: NiTeCr4Lr on March 26, 2015, 09:46:27 PM
i don't get how to adjust that to get it to run for the path "C:\Program Files\AAC to MP3 CLI" instead of the desktop ..

The most recent post runs from that folder - the pushd command changes the folder to "C:\Program Files\AAC to MP3 CLI"

Did it do something odd when you tried it? Quote from: foxidrive on March 26, 2015, 06:55:13 PM
From what you have said, all it needs is the change in the pushd command

It pushes a directory onto a stack, changes to it, and the popd pops it back off again as you gathered.
Pushd can have many levels but cd /d "C:\Program Files\AAC to MP3 CLI" will work here just as well
above the :begin label - and remove the pushd/popd lines.

Squashman has pointed out the useful help in the for /?

Code: [Select]

I don't know what a stack is in this form of terminology but if it works i'm not complaining :p
 
Quote from: foxidrive on March 27, 2015, 03:48:54 AM
The most recent post runs from that folder - the pushd command changes the folder to "C:\Program Files\AAC to MP3 CLI"

Did it do something odd when you tried it?

Sorry, i had posted that post before i saw that you had posted ! My bad.

I tried the new code and apart from the title being "FAAD" after Faad was already done and Lame had it's turn, it stopped at copying the mp3 back to "Bureaublad" because it couldn't find the MP3 due to it still having the WAV extension in it's title after Lame was done.
(So the whole before mentioned in another post "Artist - Song.WAV.MP3" ..)

The title is easily fixed by adding that after the Faad line again, but the filetype extension in new file name issue i do not know how to address efficiently while keeping your code night and tight :p..
So apart from using that old bit of code of mine "ren *.wav.mp3 *." and then "ren *.wav *.mp3" how could i address that ?Can you test this version and mark/copy/paste the text on the screen into a reply, when it pauses?
The messages on the screen help to follow what is going wrong.

Code: [Select]echo off
:: Original version by NiTeCr4Lr
:: title AAC to MP3 CLI
title AAC to MP3 CLI
color 81
cls
:begin
pushd "C:\Program Files\AAC to MP3 CLI"
faad -b 2 -l 2 -o "%~n1.wav" "%~1"
lame -cbr -b320 -m j -q0 "%~n1.wav"
del "%~n1.wav"
Tag --auto --scheme "A - T" "%~n1.mp3"
cls
move /-Y "%~n1.mp3" "C:\Documents and Settings\NiTeCr4Lr\Bureaublad"
dir /b "%~n1*"
pause
popd
shift
if not "%~1"=="" goto :beginHere's the marked, copied & pasted text displayed

"Het systeem kan het opgegeven bestand NIET vinden.
Test.wav.mp3
Druk op een toets om door te gaan. . ."

Which translated says:
"System can't find the ..given.. file.
Test.wav.mp3
Press a key to continue..."

So like i said in the post before, it's that whole "filetype extension in new file name issue"

Lame still puts out "Artist - Song.WAV.MP3" instead of "Artist - Song.MP3" because i use the 3.98.2 version of Lame which takes less than 50% of the time than the "3.99.5" version does without losing any noticeable quality, but does put out with the wav extension in the new file new sadly..

Quote from: NiTeCr4Lr on March 27, 2015, 02:02:47 PM
The filetype extension in new file name issue i do not know how to address efficiently while keeping your code night and tight :p..
So apart from using that old bit of code of mine "ren *.wav.mp3 *." and then "ren *.wav *.mp3" ...
Before I load that release of lame and test it - try this and see what it shows.

BTW, where are the LAME binary files for Windows?  The link you posted looks to only be for the source code.

Code: [Select]echo off
:: Original version by NiTeCr4Lr
:: title AAC to MP3 CLI
title AAC to MP3 CLI
color 81
cls
:begin
pushd "C:\Program Files\AAC to MP3 CLI"
faad -b 2 -l 2 -o "%~n1" "%~1"
lame -cbr -b320 -m j -q0 "%~n1"
del "%~n1"
Tag --auto --scheme "A - T" "%~n1.mp3"
move /-Y "%~n1.mp3" "C:\Documents and Settings\NiTeCr4Lr\Bureaublad"
popd
shift
if not "%~1"=="" goto :begin


Discussion

No Comment Found