Drag-and-drop a file to script to run ffmpeg passing the file as parameter Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Ross
Posts: 100
Joined: 13 Mar 2016, 00:27

Drag-and-drop a file to script to run ffmpeg passing the file as parameter

10 May 2021, 03:33

Hi, it's been a long while since I last had drag-and-drop work with AutoHotKey; I believe it has something to do with that option upon installation (enable drag-and-drop) which is now disabled by default. So I don't even know whether it's still possible; I install AutoHotKey via Chocolatey.

So say I have a mp3 file on my desktop, or any other audio file, and want to run the following:

Code: Select all

Run ffmpeg -i "in.mp3" -filter:a "atempo=1.7" -vn "C:\Users\Leo\Desktop\out.mp3" -y
...where the "in.mp3" is the dragged file onto the ahk script's icon. How to make it work so that the dragged file's path will take the "in.mp3" parameter in the line above?
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: Drag-and-drop a file to script to run ffmpeg passing the file as parameter

10 May 2021, 07:48

another example , drag&drop a videofile to a GUI ( video to MP3 )

Code: Select all

;-------------------- VIDEO 2 MP3  [ drag&drop GUI ] ---------------------
;- 20210510
#Warn
#NoEnv
SetWorkingDir %A_ScriptDir%
Gui,2: -DPIScale
Gui,2:Font,s12 CYellow,Lucida Console
Gui,2: Color,Black, Black
;--------------------------
PR=%a_scriptdir%\ffmpeg.exe
AA=%a_desktop%                 ; <<< Browse search fileselectfile from here
;---------------------------
edcol1=white
btcol =Gray
Gui,2:Add, Edit, x5     y10  w680 h90  c%edcol1% vF1,
;-- 2 - buttons
Gui,2:Add,Progress,            x700  y10   w150   h27  Disabled Background%btcol%
Gui,2:Add,Text,                xp    yp     wp    hp   cYellow  BackgroundTrans center 0x200 gBrowse   ,Browse
;--------------
Gui,2:Add,Progress,            x700  y60   w150   h27  Disabled Background%btcol%
Gui,2:Add,Text,                xp    yp     wp    hp   cYellow  BackgroundTrans center 0x200 gConvert2 ,Convert
;--------------
GUI,2:show, NA x20 y20 W920 h160, Drag & Drop or Browse for Video-files / Video2MP3
return
;----------------------------------------------------------
2GuiDropFiles:
Loop, parse, A_GuiEvent, `n
   GuiControl,2:,F1,%A_LoopField%
return
;----------------------------------------------------------
2GuiClose:
ExitApp
;----------------------------------------------------------
Browse:
FileSelectFile, filename,1,%aa%,Select your file,*.flv;*.mpg;*.webm;*.3gp;*avi;*.vob;*.mov;*.wmv;*.mp4;*.m4a
if filename =
   return
GuiControl,2:,F1,%filename%
return
;-------------------------------
Convert2:
Gui,2:submit,nohide
SplitPath,F1,name, dir, ext, name_no_ext, drive
Filedest=%a_desktop%\%name_no_ext%.mp3
if (ext="mp4" or ext="flv" or ext="mpg" or ext="webm" or ext="3gp" or ext="avi" or ext="vob" or ext="m4a" or ext="mov" or ext="wmv")
   {
   runwait,%comspec% /k %pr% -i "%f1%" -ab 192k "%Filedest%"
   run,%filedest%
   }
else
  msgbox, 262208, ,Not a desired video-extension  found
return
;============== END SCRIPT ==============================================================================================
User avatar
Ross
Posts: 100
Joined: 13 Mar 2016, 00:27

Re: Drag-and-drop a file to script to run ffmpeg passing the file as parameter

10 May 2021, 12:07

Thanks guys, I was able to use one example in the aforementioned topic and adapted my script to:

Code: Select all

Loop, %0%  ; For each parameter:
{
	param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
	Run ffmpeg -i %param% -filter:a "atempo=1.7" -vn "C:\Users\me\Desktop\out.mp3" -y
	; MsgBox, 4,, Parameter number %A_Index% is %param%.  Continue?
}
The GUI approach pointed by Garry was also useful:
garry wrote:
10 May 2021, 07:48
another example , drag&drop a videofile to a GUI ( video to MP3 )[...]
Logonius
Posts: 31
Joined: 19 Aug 2022, 08:20

Re: Drag-and-drop a file to script to run ffmpeg passing the file as parameter

03 Mar 2023, 02:14

Hello.
Do not tell me, the code on a non-compiled AHK file works, but as soon as I compile it into an exe

Code: Select all

Loop, %0%
Stops working, how to fix it?

Here is what part of my code looks like:

Code: Select all

Loop, %0%
{
	if FileExist("TI.exe"){
	RunWait, TI.exe cmd.exe ;starts services
	sleep 1000
	param := %A_Index%
	RunWait, powershell.exe -Command "Get-ChildItem %param% | ForEach-Object {Add-AppxPackage -Path $_.FullName}"
}
else{
	UrlDownloadToFile, ;downloads the file if TI. exe is not nearby and the command will be executed again
	RunWait, %A_Temp%\TI.exe ;starts the service
	sleep 1000
	param := %A_Index%
	RunWait, powershell.exe -Command "Get-ChildItem %param% | ForEach-Object {Add-AppxPackage -Path $_.FullName}"
}
}
I throw in a compiled exe file to install appx and bundle files, but they stop installing as soon as AHK turns into EXE
How to fix the code?
User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Drag-and-drop a file to script to run ffmpeg passing the file as parameter

03 Mar 2023, 06:41

If the loop is not executing, then you can use the script to display the value of its parameter.

If you pass no command-line parameters to your compiled script, then the number of parameters is zero.

Code: Select all

#Requires AutoHotkey v1.1.33
MsgBox Number=%0%
Loop %0%
 MsgBox 123
Explained: Legacy command line

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, mikeyww and 314 guests