How to extract MP3 images with Powershell V2

Started by nixda, February 17, 2013, 12:14:47 AM

Previous topic - Next topic

nixda

Hello forum,

I had a hard time to figure out the right syntax to extract an image/cover of an MP3 with Powershell (Version 2 is currently bundled in Windows 7 x64)

While my old batch script was working
D:\folder\exiftool -picture -binary D:\folder\input.mp3 > D:\folder\output.jpg

My shiny new Powershell script does not work.
& "D:\folder\exiftool.exe" -picture -binary "D:\folder\input.mp3" > "D:\folder\output.png"

The resulting image is not viewable. I tried different encodings without luck.
I was reading in the helpfile that exiftool uses UTF8 by default.

Could someone help me please :)

Just for the records, I also asked there. But I guess, my chances of a valuable answer are higher here.

Phil Harvey

Did you run ExifTool on the output image?  Could it just be that you have the wrong extension (.jpg in the first case, and .png in the second).  ExifTool does not care about the file extension.

If this isn't the problem, it may be helpful to know how large the output file is in both cases.

I don't see why this wouldn't work in PowerShell.  You suggest that special characters and encoding may be the problem, but there are no special characters in your command so this is not relevant.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

nixda

#2
The thing about the special characters and the non-viewable output.img are two separate problems. (Somehow related)

Assume a folder with "[1]" in its name. Powershell sees this as placeholder. This forces you to use methods which understand the parameter "literalpath" so the placeholder isn't recognized.
However, this limits you in the available output methods you can use. The standard methods like "out-file" can't handle such pathes because the literpath parameter is added in Powershell V3 but not V2 (the standart version).
So I'm forced to use the "set-content" method. This is a problem more on Powershell sides so I did'n post it here.

The second problem: The .png and .jpg extensions on my stackoverflow example was a copy & paste error by me. Silly me.
That was because I played around with both extensions (jpg and png). I thought that this may be the problem. However, I tried them and non are working.

Here is an example mp3 with an embedded image. (Don't bother, its free for personal use since its a non-commercial track)
I'm sure that it isn't a problem with the MP3 because the old batch command is working. And the problem still stands with all other MP3 files I've tested.

Here is my current non-working code for cases without special characters

& "D:\folder\exiftool.exe" -picture -binary "D:\folder\input.mp3"| Set-Content -literalPath "D:\folder\output.png" -encoding UTF8
& "D:\folder\exiftool.exe" -picture -binary "D:\folder\input.mp3" > "D:\folder\output.png"


And the even more complicated version which works with special characters in its path:


$ownpath = Split-Path $MyInvocation.MyCommand.Path
$exe = $ownpath + '\exiftool.exe'
$input = $ownpath + '\input.mp3'
$outimg = $ownpath + '\output.jpg'
& $exe -picture -binary $input| Set-Content -literalPath $outimg -encoding UTF8


None of them are producing a viewable image.

You can compare the output file with these two examples.

http://i.imgur.com/C0UJhh4.jpg  - The first one is created via the old batch method which is working.
http://ge.tt/6wrKIhY/v/0 - And the second one via Powershell and "set-content" method using UTF8 as encoding

Depending on the chosen encoding every output file has a different file size (of course).
But none of them are viewable. Here you see a list of available encodings

Please try it yourself with any example MP3 and let me know if you got it to work.  ???

Phil Harvey

It sounds to me like there is something fishy going on here.

The output image you get with the -binary option is binary data.  It should never be interpreted using any type of character encoding (because it is not encoded in the first place).

I fear that the problem is entirely powershell related, and as such I don't think that I will be able to help.

But perhaps you can avoid the problem by not using the powershell redirection, and save the output image directly to a file instead:

exiftool -picture -binary -w %d%f.png D:\folder

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

nixda

That was so promising until I found this limitation in your help  :-\

Quote2) It is not possible to specify a simple filename as an argument
         for -w. Instead, this simple case is accomplished using shell
         redirection:

             exiftool FILE > out.txt

So we are again at my initial problem because Shell redirection works different in Powershell

Phil Harvey

I think the documentation has confused you.

I didn't specify a simple file name.  I specified "%d%f.png", which will create a ".png" file with the same name as each source file, and in the same directory.

You only need to use shell redirection when you want to create a single output file from multiple input source files.  But this wouldn't make sense here because it doesn't make sense to write multiple binary images to a single file.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).