0

I'm using the following command in a batch file that I double-click in the folder containing the resp. webm video and m4a audio files:

for %%a in ("*.webm") do "C:\AVConvert\ffmpeg.exe" -i "%%~fa" -i "%%~na.m4a" -c:v copy -c:a copy -map 0:0 -map 1:0 "%%~na.mkv"

It works great and muxes webm with m4a audio into one mkv file. It even automatically batch-converts multiple webm/m4a files present in the same folder. Now, I wanna make the batch file available as one of multiple conversion options in the right-click context menu of media files in Windows Explorer.

Background: I have created a collection of ffmpeg batch files similar to the one above to convert between various media file types or losslessly extract audio streams from videos (among other features). For convenience, I organized the access to the batch files in cascaded context menu entries in the context menu of media files.

For now, all the batch files which are triggered when a user clicks on a context menu entry are of the following format: "C:\AVConvert\ffmpeg.exe" -i "%~1" -map_metadata 0 -codec:a pcm_s24le -write_bext 1 "%~d1\%~p1\%~n1.wav". The batch files work in whatever directory the media files are right-clicked and with as many files as the user had selected prior to triggering the right-click conversion option. Output folder is always the same as the input folder.

All batch files reside in C:\AVConvert\Batches, ffmpeg.exe is located in C:\AVConvert\. Various registry entries make the resp. batch files available in media file context menues. Problem is that the batch file containing the for loop above doesn't work when triggered via the context menu entry.

3
  • Please specify what did you mean by another location ? Another PC or another folder ? Or what exactly ?
    – Hackoo
    Commented Nov 3, 2021 at 19:43
  • Consider adding the line you use to the windows registry you are using to invoke your bat. And I have suspicions that you need to enter the drive/folder passed as an argument for the loop to work... cd /d "%~dp1"
    – Io-oI
    Commented Nov 4, 2021 at 18:47
  • in order to make the context menu show a conversion option that in turn triggers a batch file, the registry entry is: [HKEY_CLASSES_ROOT\SystemFileAssociations\.webm\Shell\4AVConvertMuxVA\Shell\01_M4A\command] @="C:\\AVConvert\\Batches\\WEBMV+M4AA.bat \"%V\"". All other batch files I'm using don't have a for loop but rely on the user's file selection which I'd also prefer for the muxing script but "C:\AVConvert\ffmpeg.exe" -i "%~1" -i "%~n1.m4a" -c:v copy -c:a copy -map 0:0 -map 1:0 "%~n1.mkv" does nothing at all.
    – Sonic
    Commented Nov 4, 2021 at 20:14

2 Answers 2

0

Your request is still unclear for me but here is what you should begin to start with :


@echo off
Title Convert *.webm to *.mkv using Batch and ffmpeg
Set "MyFile=%~1"
If "%MyFile%"=="" ( 
    MODE 95,4 & Color 0C & echo(
    echo(    You should drag and drop any "file.webm" over this batch to be converted into "file.mkv"
    Timeout /T 10 /nobreak>nul & exit
)

Set "ffmpeg=C:\ffmpeg\ffmpeg.exe"
Set "MKV_Folder_OutPut=%~dp0MKV_Folder_OutPut"
If Not Exist "%ffmpeg%" color 0C & echo( & echo( "%ffmpeg%" dosen't exists Please check location of this program ! & Timeout /T 10 /Nobreak>nul & EXIT
If Not Exist "%MKV_Folder_OutPut%" MD "%MKV_Folder_OutPut%"
@for %%a in ("*.webm") do "%ffmpeg%" -i "%MyFile%" -c:v copy -c:a copy -map 0:0 "%MKV_Folder_OutPut%\%%~na.mkv"
pause & exit
1
  • reworded my initial request for better comprehension.
    – Sonic
    Commented Nov 4, 2021 at 15:47
0

enter image description here

The windows registry will not manage its queue of objects (files) and connect them already managing the delivery to a bat file (cmd.exe), and at the same time monitoring the evolution of the events involved one by one during execution.

On the other hand, the cmd.exe (command interpreter), you will only receive a string with a complete path to your file, and in a single argument... which will point to a single file, the first in the queue, one line, that's all.

In the way you have been searching, it would be better to consider using Send To, where several items of the selection will be passed in another way (with multiplicity), as arguments...

  • To run only on the selected file, 1 per run, use:
@echo off 

setlocal EnableExtensions
2>nul cd /d "%~dp1"|| goto :eOf

color 0a & mode 110,3 & title <nul
title ..\FFmpeg 4AVConvertMuxVA ..\%~nx0 & echo\
set "_flags=-c:v copy -c:a copy -map 0:0 -map 1:0 -hide_banner -v error -stats"

if not "%~1" == "" for /f usebackq^tokens^=*delims^=  %%i in (`echo\ ^& set /p "'=%1"^<con: ^<nul
`)do if exist "%%~dpni.m4a" "C:\AVConvert\ffmpeg.exe" -i "%%~i" -i "%%~ni.m4a" %_flags% "%%~ni.mkv"

endlocal
  • To run on all your mp4 files in the selected file folder, use:
@echo off

setlocal EnableExtensions
2>nul cd /d "%~dp1"|| goto :eOf

color 0a & mode 110,3 & title <nul
title ..\FFmpeg 4AVConvertMuxVA ..\%~nx0 & echo\
set "_flags=-c:v copy -c:a copy -map 0:0 -map 1:0 -hide_banner -v error -stats"

for /f tokens^=*^delims^= %%i in ('^<con: 2^>nul "%__AppDir__%where.exe" .:*.webm
    ')do if exist "%%~dpni.m4a" "C:\AVConvert\ffmpeg.exe" -i "%%~i" -i "%%~ni.m4a" %_flags% "%%~ni.mkv"
    
endlocal

  • Windows registry entry for Menus with Submenu's in ContexMenuWsubMenu.Reg:
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\SystemFileAssociations\.webm\Shell\4AVConvertMuxVA]
"MUIVerb"="4AVConvertMuxVA"
"icon"="wmploc.DLL,-1110"
"SubCommands"="Windows.01_M4A;Windows.02_M4A"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.01_M4A]
"MUIVerb"="01_M4A"
"icon"="wmploc.DLL,-1110"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.02_M4A]
"MUIVerb"="02_M4A"
"icon"="wmploc.DLL,-1110"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.01_M4A\command]
@="\"C:\\Windows\\System32\\cmd.exe\" /c >con: cd /d \"%W\" && \"C:\\AVConvert\\Batches\\WEBMV+M4AA.bat\" \"%V\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.02_M4A\command]
@="\"C:\\Windows\\System32\\cmd.exe\" /c >con: cd /d \"%W\" && \"C:\\AVConvert\\Batches\\WEBMV+M4AA.bat\" \"%V\""

This %~n1 does not work in Windows registry
Pipe multiple files into a single batch file using explorer-highlight

6
  • this solution seems to work, thanks! Could you elaborate on what the code in the color line and in the for line means (until do)?
    – Sonic
    Commented Nov 4, 2021 at 20:30
  • this works well for muxing all video files of the rigth-clicked file type in a folder. I'd prefer to do the muxing only on selected video files (the ones highlighted by the user).
    – Sonic
    Commented Nov 4, 2021 at 21:30
  • thanks a lot for your effort - much appreciated! I think that for the time being I will stick to your all-files-in-a-folder solution. I'm way too inexperienced to understand why a conversion from wav to mp3 like this "C:\AVConvert\ffmpeg.exe" -i "%~1" -map_metadata 0 -write_id3v1 1 -write_id3v2 1 -codec:a libmp3lame -q:a 0 "%~d1\%~p1\%~n1.mp3" works when invoked via context menu but for %%a in ("*.webm") do "C:\AVConvert\ffmpeg.exe" -i "%%~na.webm" -i "%%~na.opus" -vcodec copy -acodec copy "%%~na.mkv" only works when the batch file is double-clicked in the video file's folder..
    – Sonic
    Commented Nov 8, 2021 at 2:46
  • is the same: "%~d1\%~p1\%~n1.mp3" => %~dpn.mp3... and so sorry, but your for loop comment (my fault)/bad English here) i not understand.... you need another
    – Io-oI
    Commented Nov 8, 2021 at 2:56
  • no hurry. Here we are at 5.40 a.m., LOL!
    – Sonic
    Commented Nov 8, 2021 at 4:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .