topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday March 29, 2024, 6:31 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: regex default value if nothing entered  (Read 2935 times)

jinxx

  • Supporting Member
  • Joined in 2014
  • **
  • default avatar
  • Posts: 10
    • View Profile
    • Donate to Member
regex default value if nothing entered
« on: September 18, 2020, 07:49 PM »
for example if I have this farr alias with respective value (some stuff redacted and formatted for readability):

slicevideo "(.*)" (.*) (.*) (.*)

shellexec /k "source file"=$$1 - cut time=$$2 - duration=$$3 - output name=$$4 |
   ffmpeg.exe -i "$$1" -ss $$2 -c copy -t $$3 -vf "\ffmpeg output\$$4.mp4"

Say I wanted to sometimes omit the last value "$$4", and if I omitted it then "$$4" would go to the default value of "untitled" so i'd end up spitting out a file "untitled.mp4"

Is this possible? also for multiple trailing values

Obviously as-is it only works for the trailing $$'s

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,169
    • View Profile
    • Donate to Member
Re: regex default value if nothing entered
« Reply #1 on: September 19, 2020, 11:23 AM »
I don't think the FARR alias syntax has any feature for that kind of conditionality.

Not sure if there is a way to do it in the windows cmd part (left side of the | pipe).

My own solution in these kinds of cases is to write a small AutoHotkey script and have FARR run it with the parameters and do the conditional steps in the script. If you go for this option then consider doing only
slicevideo (.*)

and separate out parameters from that one long string once in the script.

edit: Something like this

FARR alias
alias name: slicevideo
alias regex: slicevideo (.*)
alias result: C:\folder\FARR_slicevideo_helper.exe "$$1"

AutoHotkey script
Saved as FARR_slicevideo_helper.ahk and then compiled to FARR_slicevideo_helper.exe

Code: Autohotkey [Select]
  1. SetWorkingDir %A_ScriptDir%
  2.  
  3. ; FARR alias slicevideo helper script
  4. ; 2020-09-19
  5.  
  6. ; FARR alias syntax:
  7. ; slicevideo "(.*)"
  8. ; where the regex sub pattern contains one of these string formats:
  9. ; - "<filepath>" <start-time-in-seconds> <duration-time-in-seconds> <out-file-name-no-ext>
  10. ; - "<filepath>" <start-time-in-seconds> <duration-time-in-seconds>
  11. ; - "<filepath>" <start-time-in-seconds>
  12. ; rule: only <filepath> can include spaces
  13.  
  14. ; process command line input
  15. ; split at quote marks to array
  16. ; -> array item 1 = blank
  17. ; -> array item 2 = input file path
  18. ; -> array item 3 = all remaining params, space separated
  19. ArrayA := StrSplit(A_Args[1], """")
  20. ; split above array item 3 at spaces to new array
  21. ArrayB := StrSplit(ArrayA[3], " ")
  22.  
  23. File      := ArrayA[2]
  24. StartTime := ArrayB[2]
  25. Duration  := ArrayB[3]
  26. OutFile   := ArrayB[4]
  27.  
  28. ; input file must exist and starttime must be set
  29. if !FileExist(File) or !StartTime
  30.  
  31. ; fallback output filename
  32. if !OutFile
  33.   OutFile := "untitled"
  34.  
  35. ; fallback duration in seconds
  36. if !Duration
  37.   Duration := 60
  38.  
  39. ; run cmd window that stays open and starts ffmpeg cut
  40. Run %comspec% /k " ffmpeg.exe -i "%File%" -ss %StartTime% -c copy -t %Duration% -vf "\ffmpeg output\%OutFile%.mp4" "
« Last Edit: September 20, 2020, 03:19 AM by Nod5 »

wjamoe

  • Supporting Member
  • Joined in 2010
  • **
  • Posts: 99
    • View Profile
    • Donate to Member
Re: regex default value if nothing entered
« Reply #2 on: September 25, 2020, 01:25 AM »
or you can make all 4 fields mandatory like:

slicevideo\s+"([^"]+)"\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)
 
\s+      = at least one space or tab (white space)
[^"]+  = at least one character until a quote
[^\s]+ = at least one character not being a white space
()        = remember contents for use in $$1 $$2 $$3 $$4

Maybe you want  to limit the characters for $$2 $$3 $$4 to make sure valid time format and filename

slicevideo\s+"([^"]+)"\s+([\d:]+)\s+([\d:]\w+)\s+(\w+)$
 
\w+     = at lease one word character 0-9 a-z A-Z and the underscore (word characters)
[\d:]+  =0-9 and ':' (in random order!)
[0-9:]  =0-9 and ':' (in random order)

if you want to extend the word characterset with '+' and '-'
replace
   \w+
by
   [\w+\-]+
please note you need a backslash just before the minus sign

-----
or a simple alternative
add a prefix (e.g. vid_) to each $$4 in the alias this will keep working when $$4 is empty like so:

shellexec /k "source file"=$$1 - cut time=$$2 - duration=$$3 - output name=vid_$$4 |
   ffmpeg.exe -i "$$1" -ss $$2 -c copy -t $$3 -vf "\ffmpeg output\vid_$$4.mp4"
« Last Edit: October 16, 2020, 12:31 PM by wjamoe, Reason: explain characterset and explained vid_$$4 in more detail »

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,169
    • View Profile
    • Donate to Member
Re: regex default value if nothing entered
« Reply #3 on: October 12, 2020, 03:24 AM »
Discussion in another thread https://www.donation...ex.php?topic=50563.0 got me thinking...

An alternative way to do what OP here wants withouth using an external AutoHotkey script is to set up multiple aliases, one for each possible use case.

case 1 would be your original example where all four parameters are typed. Let us call that alias 1. We could there use the regex wjamoe suggested which requires all four parameters to be typed.
regex: slicevideo\s+"([^"]+)"\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)
results:
shellexec /k "source file"=$$1 - cut time=$$2 - duration=$$3 - output name=$$4 | ffmpeg.exe -i "$$1" -ss $$2 -c copy -t $$3 -vf "\ffmpeg output\$$4.mp4"

case2 
Say I wanted to sometimes omit the last value "$$4", and if I omitted it then "$$4" would go to the default value of "untitled" so i'd end up spitting out a file "untitled.mp4"
covered by alias 2:
regex: slicevideo\s+"([^"]+)"\s+([^\s]+)\s+([^\s]+)
results:
shellexec /k "source file"=$$1 - cut time=$$2 - duration=$$3 - output name=untitled | ffmpeg.exe -i "$$1" -ss $$2 -c copy -t $$3 -vf "\ffmpeg output\untitled.mp4"

You can create additional sibling aliases for the other cases you want to cover.
The drawback with this approach is that it is unwieldy to overview and if you want to later on e.g. change the ffmpeg path you need to edit multiple aliases.