Automatic output file extension and muxer selection

Microsoft Windows specific usage questions
Forum rules
Please post only Windows specific questions in this forum category. If you don't know where to post, please read the different forums' rules. Thanks.
VLC22
New Cone
New Cone
Posts: 7
Joined: 09 Nov 2010 04:08

Automatic output file extension and muxer selection

Postby VLC22 » 02 Sep 2012 17:47

I've made a one-line batch file that takes any file sent to it and puts the audio stream into a .MP4 file including the original name. It works great for files with MP4 audio in them, but fails if the audio is MP3, etc.

I'd like to alter the command line so that VLC will automatically choose the extension and muxer to use, based on whatever the input audio track is. Is this possible?

The current line is:

"C:\Program Files (x86)\VideoLAN\VLC\vlc" %1 --no-sout-video --sout "#std{dst=%~n1_audio.mp4}" vlc://quit

Also, it fails if the file has a comma or apostrophe in it. (And maybe other characters too?) Is there a way round that?

Thanks!

Arite
Big Cone-huna
Big Cone-huna
Posts: 2478
Joined: 26 Jun 2007 20:40
VLC version: 3.0.20
Operating System: Debian Testing|Win10

Re: Automatic output file extension and muxer selection

Postby Arite » 03 Sep 2012 02:46

I've made a one-line batch file that takes any file sent to it and puts the audio stream into a .MP4 file including the original name. It works great for files with MP4 audio in them, but fails if the audio is MP3, etc.

I'd like to alter the command line so that VLC will automatically choose the extension and muxer to use, based on whatever the input audio track is. Is this possible?
You'd need to transcode the audio to something compatible with MP4 (like AAC) if the codec isn't already compatible.

If you make the assumption that ".aac" and ".mp4" both have MP4-compatible audio codecs, and any other file needs converting to MP4 then you can do it in a few lines of batch script.

You can get the file extensions by using this modifier:

Code: Select all

%~x1
^ Similar to what you did with %~n1. For a list of other ones see here:
http://www.microsoft.com/resources/docu ... x?mfr=true

Then you could do something like this:

Code: Select all

@echo off set vlc="C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" set sout_copy="#std{dst=%~n1_audio.mp4}" set sout_convert="#transcode{vcodec=none,acodec=mp4a,ab=128,channels=2}:file{dst=%~n1_audio.mp4}" if "%~x1"==".mp4" goto copy if "%~x1"==".aac" goto copy :convert echo Going to convert... set sout_vars=%sout_convert% goto run :copy echo Going to copy... set sout_vars=%sout_copy% goto run :run %vlc% %1 --no-sout-video --sout %sout_vars% vlc://quit  
I appreciate that it's quite long (there are probably more efficient ways to write in in batch), but what I came up with. All it does is look at the file extension, and change the sout string accordingly.

The first few lines just set up where vlc.exe is, what the "copy" sout string looks like and what the "convert" sout string looks like.

The convert sout string converts the source audio to 2 channel mp4a (AAC) audio with a bitrate of 128kbps. You can change this of course. The file name is the as what you put (original file name, exentionless, with "_audio.mp4" afterwards).

The next couple of lines just run if statements, and if true, jumps to the "copy" section. This just copys the "sout_copy" to "sout_vars". If both if statements are false the ":convert" section will run. The "goto" jumps to ":run", and runs VLC.
Also, it fails if the file has a comma or apostrophe in it. (And maybe other characters too?) Is there a way round that?
If you put the dst in single quotes it should help. So:

Code: Select all

dst='dst='%~n1_audio.mp4'
Special charaters should be escaped using a \ etc., not sure whether that can be done in batch. Ideally avoid commas and single quotes in filenames anyway.

Cheers, Arite.
Don't use PMs for support questions.

VLC22
New Cone
New Cone
Posts: 7
Joined: 09 Nov 2010 04:08

Re: Automatic output file extension and muxer selection

Postby VLC22 » 04 Sep 2012 17:26

Thanks... I learnt a bit about how to make my batch files look neater, but I'm afraid you missed the point of my question:
so that VLC will automatically choose the extension and muxer to use, based on whatever the input audio track is.
I want to avoid transcoding, and only want the output to be .mp4 format if that's what the audio track is. If the audio is MP3, I want VLC to automatically choose the .mp3 extension and correct container, if it is Ogg Vorbis, then choose Ogg container and .ogg extension, if WAV then choose .wav, etc.

I can't really avoid apostrophes and commas because they are both popular in the titles of songs / music videos, films, YouTube videos, etc. Perhaps single quotes will help with commas, but apostrophes are the same character, so that will surely confuse them. I had already tried enclosing the filename in double quotes, but it seems to be VLC itself that has problems with commas and apostrophes.

I actually put some more time into it and found my own solution to problem characters. VLC only has a problem with the output file names (not the input), so I set the output as a temporary filename, then automatically rename it afterwards.

My current .bat batch file is below. This works for dropping several files on it at once, or using the Windows "Send To" function. [b
However, I still can't get it to automatically choose the correct container and extension for the output file.[/b]
(It also doesn't work if folders are sent to it rather than individual files.) It seems daft that VLC doesn't seem to have this option, because obviously VLC itself already knows what extension the track should have, and it seems difficult to automate it externally.

Code: Select all

FOR %%A IN (%*) DO (CALL :SUB_VLC %%A) GOTO :eof :SUB_VLC REM VLC cannot handle a comma or apostrophe in output filename, so use temporary name then rename. REM Note: _TEMP_A.mp4 will be overwritten by VLC if it exists "C:\Program Files (x86)\VideoLAN\VLC\vlc" -I dummy %1 --no-sout-video --sout=#file{dst="_TEMP_A.mp4"} vlc://quit SET _outfile=%~n1_audio.mp4 REM Add number to filename if file already exists. set n=0 :loop set /a n+=1 REM Two separate "if exist" lines are used to avoid problems with brackets in filenames. if exist "%~dp1%_outfile%" set _outfile=%~n1_audio %n%.mp4 if exist "%~dp1%_outfile%" goto loop REN "%~dp1_TEMP_A.mp4" "%_outfile%" GOTO :eof


Return to “VLC media player for Windows Troubleshooting”

Who is online

Users browsing this forum: No registered users and 103 guests