; rough youtube clipper. ; usage: ; right click on a youtube video to copy video URL at current time ; ; then run this script. ; depends on ffmpeg and youtube-dl being installed in your %PATH% ; ; greetz to ferryman #NoEnv #SingleInstance, Force ; you may want to change these variables: ; path to mpv.exe (do not enclose in quotes) mpv=mpv.exe ; temp/output folder tmp=%A_Temp% url := clipboard, ts := A_Now SetWorkingDir, % tmp if (!RegExMatch(url, "^https://")) return if (RegExMatch(url, "[\&\?]t=(\d+)", m)) startTime := m1, startTime += 0 InputBox, endTime, End Time, Enter duration in seconds (be generous) downloadURLs := StrSplit(StdoutToVar_CreateProcess("youtube-dl.exe -g " url), "`n", "`r") downloadURL1 := downloadURLs[1], downloadURL2 := downloadURLs[2] exec = ffmpeg.exe -y -ss %startTime% -i "%downloadURL1%" -ss %startTime% -i "%downloadURL2%" -t %endTime% -map 0:v -map 1:a -vcodec copy -acodec copy clip.mp4 StdoutToVar_CreateProcess(exec) Run, % tmp Run, "%mpv%" clip.mp4 ; See https://www.autohotkey.com/boards/viewtopic.php?t=791 for StdoutToVar_CreateProcess to include in this script ; ---------------------------------------------------------------------------------------------------------------------- ; Function .....: StdoutToVar_CreateProcess ; Description ..: Runs a command line program and returns its output. ; Parameters ...: sCmd - Commandline to execute. ; ..............: sEncoding - Encoding used by the target process. Look at StrGet() for possible values. ; ..............: sDir - Working directory. ; ..............: nExitCode - Process exit code, receive it as a byref parameter. ; Return .......: Command output as a string on success, empty string on error. ; AHK Version ..: AHK_L x32/64 Unicode/ANSI ; Author .......: Sean (http://goo.gl/o3VCO8), modified by nfl and by Cyruz ; License ......: WTFPL - http://www.wtfpl.net/txt/copying/ ; Changelog ....: Feb. 20, 2007 - Sean version. ; ..............: Sep. 21, 2011 - nfl version. ; ..............: Nov. 27, 2013 - Cyruz version (code refactored and exit code). ; ..............: Mar. 09, 2014 - Removed input, doesn't seem reliable. Some code improvements. ; ..............: Mar. 16, 2014 - Added encoding parameter as pointed out by lexikos. ; ..............: Jun. 02, 2014 - Corrected exit code error. ; ..............: Nov. 02, 2016 - Fixed blocking behavior due to ReadFile thanks to PeekNamedPipe. ; ---------------------------------------------------------------------------------------------------------------------- StdoutToVar_CreateProcess(sCmd, sEncoding:="CP0", sDir:="", ByRef nExitCode:=0) { DllCall( "CreatePipe", PtrP,hStdOutRd, PtrP,hStdOutWr, Ptr,0, UInt,0 ) DllCall( "SetHandleInformation", Ptr,hStdOutWr, UInt,1, UInt,1 ) VarSetCapacity( pi, (A_PtrSize == 4) ? 16 : 24, 0 ) siSz := VarSetCapacity( si, (A_PtrSize == 4) ? 68 : 104, 0 ) NumPut( siSz, si, 0, "UInt" ) NumPut( 0x100, si, (A_PtrSize == 4) ? 44 : 60, "UInt" ) NumPut( hStdOutWr, si, (A_PtrSize == 4) ? 60 : 88, "Ptr" ) NumPut( hStdOutWr, si, (A_PtrSize == 4) ? 64 : 96, "Ptr" ) If ( !DllCall( "CreateProcess", Ptr,0, Ptr,&sCmd, Ptr,0, Ptr,0, Int,True, UInt,0x08000000 , Ptr,0, Ptr,sDir?&sDir:0, Ptr,&si, Ptr,&pi ) ) Return "" , DllCall( "CloseHandle", Ptr,hStdOutWr ) , DllCall( "CloseHandle", Ptr,hStdOutRd ) DllCall( "CloseHandle", Ptr,hStdOutWr ) ; The write pipe must be closed before reading the stdout. While ( 1 ) { ; Before reading, we check if the pipe has been written to, so we avoid freezings. If ( !DllCall( "PeekNamedPipe", Ptr,hStdOutRd, Ptr,0, UInt,0, Ptr,0, UIntP,nTot, Ptr,0 ) ) Break If ( !nTot ) { ; If the pipe buffer is empty, sleep and continue checking. Sleep, 100 Continue } ; Pipe buffer is not empty, so we can read it. VarSetCapacity(sTemp, nTot+1) DllCall( "ReadFile", Ptr,hStdOutRd, Ptr,&sTemp, UInt,nTot, PtrP,nSize, Ptr,0 ) sOutput .= StrGet(&sTemp, nSize, sEncoding) } ; * SKAN has managed the exit code through SetLastError. DllCall( "GetExitCodeProcess", Ptr,NumGet(pi,0), UIntP,nExitCode ) DllCall( "CloseHandle", Ptr,NumGet(pi,0) ) DllCall( "CloseHandle", Ptr,NumGet(pi,A_PtrSize) ) DllCall( "CloseHandle", Ptr,hStdOutRd ) Return sOutput }