0

I am using a batch file to access my portable VLC executable to convert an mp4 to an mp3:

set arg1=%1   REM -> arg1={my_mp4_full_path}
set arg2=%2   REM -> arg2={my_mp3_full_path}
echo %arg1%
echo %arg2%
REM batch file is in the same directory as "VLCPlayer" folder
"%~dp0\VLCPlayer\VLCPortable.exe" -I dummy %arg1% --sout=#transcode{acodec=mp3,ab=128,vcodec=dummy}:std{access="file",mux="raw",dst=%arg2%} vlc://quit

When I run this script the first time, vlc crashes and I get an unplayable mp3 file, however when I run the script again the script works and I get a playable mp3. Is there a way to remedy this, or make it consistent? I don't see why running it twice would yield different outcomes.

No I don't have ffmpeg on my computer it is unrecognizable internal or external command.

Note that I face the same problem when using powershell to perform the same task, when I import my function from a .psm1 script:

function ConvertToMp3(
    [switch] $inputObject,
    [string] $vlc = '{PAth_TO_PORTABLE_VLC}\VLCPortable.exe')
{
    PROCESS {
        $codec = 'mp3';
        $oldFile = $_;
        $newFile = $oldFile.FullName.Replace($oldFile.Extension, ".$codec").Replace("'","");
        &"$vlc" -I dummy "$oldFile" ":sout=#transcode{acodec=$codec,
        vcodec=dummy}:standard{access=file,mux=raw,dst=`'$newFile`'}" vlc://quit | out-null;

        # delete the original file
        Remove-Item $oldFile;
        }
}

I get the same random output that sometimes works, sometimes crashes.

Update: I feel like I should add more info of how I use the batch file: I have a python script Convert.py and I call my batch file inside using os.system():

mp4_to_convert = arguments.file
full_path_mp4 = os.path.join(outdir,mp4_to_convert)
mp3_to_convert_to = mp4_to_convert.replace(".mp4",".mp3")
full_path_mp3 = os.path.join(outdir,mp3_to_convert_to)
command_string = """Convert_Script.bat \"{}\" \"{}\"""".format(full_path_mp4, full_path_mp3)
os.system(command_string)

This is the documentation of os.system():

os.system(command)

Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command. If command generates any output, it will be sent to the interpreter standard output stream.

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after running command. The shell is given by the Windows environment variable COMSPEC: it is usually cmd.exe, which returns the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

Any pointers or suggestions would be helpful, thank you in advance for your help.

Community
  • 1
  • 1
Hadi Farah
  • 1,091
  • 2
  • 9
  • 27
  • 1
    No need to assigne the batch file arguments to variables, just use `%1`, `%2` immediately; however, try using them with proper quotation, like `"%~1"` and `"%~2"` to avoid trouble with spaces or other special characters... – aschipfl Dec 10 '18 at 19:08
  • 4
    the behaviour you describe very much sounds like a typical [delayed expansion problem](https://stackoverflow.com/a/30284028/2152082) - which would mean, you don't show us all your (relevant) code. – Stephan Dec 10 '18 at 19:12
  • @aschipfl the arguments provided are already string with surrounding quotations so when I call a path with white spaces it would work. – Hadi Farah Dec 12 '18 at 17:05
  • @Stephan it seems adding `call` before the last line in my batch file bumps the success rate to 90% or so. Thank you. – Hadi Farah Dec 12 '18 at 17:08
  • @Mofi you are right I removed the additional backslash. – Hadi Farah Dec 12 '18 at 17:10

0 Answers0