Hi
into the cats, but
if possible I wonder how to catch this condition:
the sequence ofCode:LSMASHVideoSource("C:\Users\Administrator\Desktop\crop resize\drone 50P movimento veloceDJI_0044.MP4") #TurnRight() #Turn180() #TurnLeft() #robocrop() ConvertToYUY2(interlaced=false) ConverttoYV12(interlaced=false) emask = mt_convolution(vertical="-1 2 -1").ColorYUV(off_y=-64).ColorYUV(gain_y=128).mt_expand(chroma="-128").Blur(0.8) Overlay(last, Blur(0.6, 0.7), mask=emask) ConvertToYUY2(interlaced=false) assumeFPS(50) Prefetch(8)
ConvertToYUY2(interlaced=false)
ConverttoYV12(interlaced=false)
in an .avs script
via batch commandline
thanks
+ Reply to Thread
Results 1 to 19 of 19
-
-
you are falling from hacked workflows into hacking windows batch codes, which as it is, windows batch is already a one hack if one wants to really do a work
in this case we are hacking a problem not to be able to put an line break into a string or variable for a search:
Code:rem https://stackoverflow.com/questions/54401186/find-a-string-included-carriage-return-in-a-file-windows-batch @echo off setlocal set "script=test_avs.avs" set "text1=ConvertToYUY2(interlaced=false)" set "text2=ConverttoYV12(interlaced=false)" set LF=^ rem NOTE! the above 2 blank lines are critical - do not remove for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a" setlocal enableDelayedExpansion findstr /n /r /c:"!text1!!CR!*!LF!!text2!" %script% >nul && echo Found || echo Not found
-
I understand you cannot change whole lot of your scripts, but you can install python and start to do some utilities in a readable language:
like calling python script from a batch script and return some value back to batch script, like your example:
your batch script, which is calling a python utility:
Code:@echo off set "script=test_avs.avs" set "text=ConvertToYUY2(interlaced=false)ConverttoYV12(interlaced=false)" start /wait python find_python.py %script% %text% if %errorlevel%==0 (echo found it) else (echo did not find it)
Code:import sys def main(script, text): loaded_text = "" with open(script, "r") as f: for line in f: loaded_text += line.strip() if text in loaded_text: sys.exit(0) #found, returning 0 to batch script which appears in errorlevel variable else: sys.exit(1) #not found, returning 1 to batch script which appears in errorlevel variable if __name__ == "__main__": """ usage in cmd: python this_script.py test_avs.avs ConvertToYUY2(interlaced=false)ConverttoYV12(interlaced=false) """ script = sys.argv[1] text = sys.argv[2] main(script, text)
Last edited by _Al_; 13th Aug 2023 at 01:37.
-
mmm thanks but I'm already in a batch procedure: maybe I think I cannot use the setlocal enableDelayedExpansion (or I don't know exactly)
I have already defined the .avs scripts as
Code:%cd%\AllSingleRemuxed\%~nx1.avs
Code:ConvertToYUY2(interlaced=false) convertToYV12(interlaced=false)
if this how can I do it? -
Code:
find /I "ConvertToYUY2(interlaced=false)" filename.ext
find /h:
Code:Searches for a text string in a file or files. FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]] /V Displays all lines NOT containing the specified string. /C Displays only the count of lines containing the string. /N Displays line numbers with the displayed lines. /I Ignores the case of characters when searching for the string. /OFF[LINE] Do not skip files with offline attribute set. "string" Specifies the text string to find. [drive:][path]filename Specifies a file or files to search. If a path is not specified, FIND searches the text typed at the prompt or piped from another command.
Code:ConvertToYUY2( interlaced=false) ConvertToYUY2( interlaced=false ) ConvertToYUY2(interlaced = false) ConvertToYUY2(interlaced=false )
Last edited by jagabo; 13th Aug 2023 at 19:02.
-
My understanding is that the OP wishes to capture the occurrence of these two lines
ConvertToYUY2(interlaced=false)
ConverttoYV12(interlaced=false)
my suggestion is to use this code sequence
grep -A1 "ConvertToYUY2" | tail -n 1 | grep "ConverttoYV12"
grep -A1 "ConvertToYUY2" will capture the occurrence of the string ConvertToYUY2 plus the following line
tail -n 1 will capture the second line while
grep "ConverttoYV12" will check for the occurrence of the second string. -
That what I thought also, that it must search two same lines only one after each other,
findstr should be a windows equivalent for grep in linux, it allows spaces in string if using /c argument.
Not sure what to do, how to deal with a particular case if those two lines in Avisynth script have trailing spaces for example. In that case even findstr would not find that string. Maybe there is a way, perhaps another hack.
how can I do it?
Code:set "script=%cd%\AllSingleRemuxed\%~nx1.avs"
Last edited by _Al_; 14th Aug 2023 at 00:38.
-
-
Code:
echo Frame_RateScript assume %Frame_RateScript% if "%Frame_RateScript%"=="50/1" set ScriptAVS50P=1 if "%Frame_RateScript%"=="50/1" if exist tempDicituraVFinterlace.txt del tempDicituraVFinterlace.txt if "%Frame_RateScript%"=="50/1" echo -vf interlace=lowpass=0:scan=tff>tempDicituraVFinterlace.txt if "%Frame_RateScript%"=="50/1" set /p EventualeDicitura_X_FFMPEG_VFinterlaceSe50FPS=<tempDicituraVFinterlace.txt if "%Frame_RateScript%"=="50/1" If exist tempDicituraVFinterlace.txt del tempDicituraVFinterlace.txt if "%Frame_RateScript%"=="50/1" echo EventualeDicitura_X_FFMPEG_VFinterlaceSe50FPS assume %EventualeDicitura_X_FFMPEG_VFinterlaceSe50FPS% if "%Frame_RateScript%"=="25/1" set ScriptAVS25i=1 if "%Frame_RateScript%"=="25/1" set "EventualeDicitura_X_FFMPEG_VFinterlaceSe50FPS=" echo EventualeDicitura_X_FFMPEG_VFinterlaceSe50FPS assume %EventualeDicitura_X_FFMPEG_VFinterlaceSe50FPS% rem ********************************************************************* if "%dummy%"=="0" echo ### sorgente PARI>>"%cd%\AllSingleRemuxed\%~nx1.avs" if NOT "%dummy%"=="0" echo ### sorgente DISPARI>>"%cd%\AllSingleRemuxed\%~nx1.avs" if "%Frame_RateScript%"=="50/1" echo ### in questo punto viene rilevato automaticamente il framerate dello script che qui risulta a 50P>>"%cd%\AllSingleRemuxed\%~nx1.avs" if "%Frame_RateScript%"=="25/1" echo ### in questo punto viene rilevato automaticamente il framerate dello script che qui risulta a 25>>"%cd%\AllSingleRemuxed\%~nx1.avs" if NOT "%dummy%"=="0" if "%Frame_RateScript%"=="50/1" if not "%isMXF%"=="1" echo AssumeTFF().SeparateFields().SelectEvery(4, 0, 3).Weave()>>"%cd%\AllSingleRemuxed\%~nx1.avs" if NOT "%dummy%"=="0" if "%Frame_RateScript%"=="50/1" if "%isMXF%"=="1" echo AssumeTFF().SeparateFields().SelectEvery(4, 0, 3).Weave()>>"%cd%\AllSingleRemuxed\%~nx1.avs" if "%dummy%"=="0" if "%Frame_RateScript%"=="50/1" if "%isMXF%"=="1" echo AssumeTFF().SeparateFields().SelectEvery(4, 0, 3).Weave()>>"%cd%\AllSingleRemuxed\%~nx1.avs" echo Prefetch(8)>>"%cd%\AllSingleRemuxed\%~nx1.avs" rem ********************************** rilevamento del doppio inserimento di ConvertToYUY2 e ConvertToYV12 attenzione non cancellare le 2 linne bianche tra LF e rem ***************** set LF=^ rem NOTE! le 2 linee bianche sopra sono critiche NON CANCELLARLE for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a" setlocal enableDelayedExpansion findstr /n /r /c:"ConvertToYUY2(interlaced=false)!CR!*!LF!ConvertToYV12(interlaced=false)" "%cd%\AllSingleRemuxed\%~nx1.avs" >nul && echo Found || echo Not found rem ******************************************************************************************************************************************************************* pause rem *** occorre fare una specie di test dell avs per far si che comprenda se usando framerateconverter va in errore e va sostituito con smoothfps2 **** echo scanProgressivo assume %scanProgressivo% echo scanInterlace assume %scanInterlace% echo fps50 assume %fps50% echo RealFPS2 assume %RealFPS2% echo pixel_fmt assume %pixel_fmt% echo AmbiguoFrameRate assume %AmbiguoFrameRate% rem ***** rilevamento del framerate dello script avs **************** for /f "tokens=1,2 delims=^=" %%g in ('v:\automazioneclip\core\ffprobe64bit333.exe -hide_banner -loglevel fatal -pretty -select_streams v:0 -show_streams -show_entries stream^=codec_type^,r_frame_rate^,width^,height^,display_aspect_ratio^,duration_ts^,sample_aspect_ratio:stream_disposition^=:format_tags^=timecode^,company_name^,product_name^,product_version^,com.android.capture.fps "%cd%\AllSingleRemuxed\%~nx1.avs" 2^>^&1') DO ( if "%%~g"=="r_frame_rate" set /a "FinalFrameRate=%%~h" )
Now that I have found if an .avs have the 2 occurence is it possible delete the first one?
so that from
Code:ConvertToYUY2(interlaced=false) ConvertToYV12(interlaced=false)
Code:ConvertToYV12(interlaced=false)
[Attachment 73179 - Click to enlarge]
thanks -
grep -A1 "ConvertToYUY2" your_file.txt | head -n 2 | tail -n 1 | grep -c "ConverttoYV12"
this will output 1 if the sequence is found
this will output 0 if the sequence is not found -
-
It would be difficult to do it in batch script if you want these features: multi-line text or case sensitive or not, perhaps multiple occurrences or trailing spaces in text file.
I made it using python, following those features, so you can use it or compiled Replace_Lines.exe, it is in that zip file.
How did I do it:
made a python script, called replace_lines.py:
Code:def write_buffer(f, buffer): for buffer_line in buffer: f.write(f"{buffer_line}\n") def replace(filepath:str, search_lines:list, replace_lines:list, case_sensitive=True): """ replaces multiple lines in a text file where lines are loaded as a list with other multiple lines (also passed as a list) """ print("lines_replacement (Case Sensitive!):" if case_sensitive else "lines_replacement (case insensitive):") with open(filepath, 'r+') as f: lines=[] loaded_text="" for line in f: line = line.strip() lines.append(line) loaded_text += line if case_sensitive: occurance_count = loaded_text.count("".join(search_lines)) else: occurance_count = loaded_text.lower().count("".join(search_lines).lower()) if not occurance_count: print(f' no search pattern found in "{filepath}"') else: print(f' found {occurance_count} occurance(s) of search pattern in "{filepath}"') f.seek(0) #move cursor to the beggining f.truncate() #delete everything in file lines = iter(lines) running = True last = None search_lines = search_lines if case_sensitive else [i.lower() for i in search_lines] while running: if last is not None: buffer = [last] else: try: line = next(lines) except StopIteration: break buffer = [line] for i, search_line in enumerate(search_lines): line_c = line if case_sensitive else line.lower() if not line_c == search_line: if last: last_c = last if case_sensitive else last.lower() else: last_c = None if i and last_c == search_lines[0]: write_buffer(f, buffer[:-1]) last = line else: write_buffer(f, buffer) last = None break buffer_c = buffer if case_sensitive else [i.lower() for i in buffer] if buffer_c == search_lines: #pattern found! write_buffer(f, replace_lines) print(f" replace pattern written") last = None break try: line = next(lines) except StopIteration: running = False break last = line buffer.append(line) if __name__ == "__main__": """ usage: python replace_lines.py --file "test_avs.avs" --search "ConvertToYUY2(interlaced=false)\nConverttoYV12(interlaced=false)" --replace "ConverttoYV12(interlaced=false)" """ import argparse import os parser = argparse.ArgumentParser() parser.add_argument("--file", type=str, help="filepath of searched file") parser.add_argument("--search", type=str, help="searched lines, separate lines by \\n in a string") parser.add_argument("--replace", type=str, help="replace lines, separate lines by \\n in a string") parser.add_argument("-cs", "--casesensitive", action="store_true", help="flag will cause comparing strings as case sensitive") args = parser.parse_args() if not args.file: raise ValueError("file argument needed") if not os.path.isfile(args.file): raise ValueError(f"filepath not found: {args.file}") if not args.search: raise ValueError("search argument needed") search_lines = args.search.split(r'\n') if not args.replace: raise ValueError("replace argument needed") replace_lines = args.replace.split(r'\n') replace(args.file, search_lines, replace_lines, args.casesensitive)
Code:"C:\Users\***user***\AppData\Roaming\Python\Python38\Scripts\pyinstaller.exe" replace_lines.py --name "Replace_Lines" -–onefile
Usage for python file or that exe file:
Code:@echo off set "script=test_avs.avs" set "search=ConvertToYUY2(interlaced=false)\nConverttoYV12(interlaced=false)" set "replace=ConverttoYV12(interlaced=false)" Replace_Lines --file "%script%" --search "%search%" --replace "%replace%" rem Replace_Lines --help rem python replace_lines.py --file "%script%" --search "%search%" --replace "%replace%" rem python replace_lines.py --help
-when searched lines are not found, nothing happens to searched file
-before you start testing, BACK UP that text fileLast edited by _Al_; 15th Aug 2023 at 02:11.
-
[Attachment 73187 - Click to enlarge]
Code:C:\>replace_lines.exe --file "c:\test_avs.avs" --search "ConvertToYUY2(interlaced=false)\nConverttoYV12(interlaced=false)" --replace "ConverttoYV12(interlaced=false)" lines_replacement (case insensitive): found 1 occurance(s) of search pattern in "c:\test_avs.avs" replace pattern written C:\>
cheers to the pyton
very useful thanks !!!! -
more safer approach,
-it rewrites file actually only after there is no possible error while creating new file,
-also backs up original file , just adding .bak
Code:import shutil from pathlib import Path def cs_string(string, case_sensitive): if case_sensitive: return string else: return string.lower() if string is not None else None def cs_list(lst, case_sensitive): if case_sensitive: return lst else: return [i.lower() for i in lst] def replace(filepath:str, search_lines:list, replace_lines:list, case_sensitive=True): """ replaces multiple lines in a text file where lines are loaded as a list with other multiple lines (also passed as a list) """ print("lines_replacement (Case Sensitive!):" if case_sensitive else "lines_replacement (case insensitive):") new_lines = [] with open(filepath, 'r') as f: lines=[] loaded_text="" for line in f: line = line.strip() lines.append(line) loaded_text += line if case_sensitive: occurance_count = loaded_text.count("".join(search_lines)) else: occurance_count = loaded_text.lower().count("".join(search_lines).lower()) if not occurance_count: print(f' no search pattern found in "{filepath}"') else: print(f' found {occurance_count} occurance(s) of search pattern in "{filepath}"') lines = iter(lines) running = True last = None search_lines = cs_list(search_lines, case_sensitive) while running: if last is not None: buffer = [last] else: try: line = next(lines) except StopIteration: break buffer = [line] for i, search_line in enumerate(search_lines): if not cs_string(line, case_sensitive) == search_line: if i and cs_string(last, case_sensitive) == search_lines[0]: new_lines += buffer[:-1] last = line else: new_lines += buffer last = None break if cs_list(buffer, case_sensitive) == search_lines: #pattern found! new_lines += replace_lines print(f" pattern replaced for writting") last = None break try: line = next(lines) except StopIteration: running = False break last = line buffer.append(line) if new_lines: #backing up file before rewriting it path = Path(filepath) shutil.copy2(filepath, str(path.parent / f"{path.name}.bak")) #rewriting file print(f' Rewriting file. Old file backed up as "{path.name}.bak"') with open(filepath, "r+") as f: f.seek(0) #move cursor to the beggining f.truncate() #delete everything in file for line in new_lines: f.write(f"{line}\n") if __name__ == "__main__": """ usage: python replace_lines.py --file "test_avs.avs" --search "ConvertToYUY2(interlaced=false)\nConverttoYV12(interlaced=false)" --replace "ConverttoYV12(interlaced=false)" """ import argparse import os parser = argparse.ArgumentParser() parser.add_argument("--file", type=str, help="filepath of searched file") parser.add_argument("--search", type=str, help="searched lines, separate lines by \\n in a string") parser.add_argument("--replace", type=str, help="replace lines, separate lines by \\n in a string") parser.add_argument("-cs", "--casesensitive", action="store_true", help="flag will cause comparing strings as case sensitive") args = parser.parse_args() if not args.file: raise ValueError("file argument needed") if not os.path.isfile(args.file): raise ValueError(f"filepath not found: {args.file}") if not args.search: raise ValueError("search argument needed") search_lines = args.search.split(r'\n') if not args.replace: raise ValueError("replace argument needed") replace_lines = args.replace.split(r'\n') replace(args.file, search_lines, replace_lines, args.casesensitive)
Last edited by _Al_; 15th Aug 2023 at 12:47.
-
Ah wow
it works well:
Code:%COMSPEC% /c V:\Replace_Lines.exe --file "%cd%\AllSingleRemuxed\%~nx1.avs" --search "ConvertToYUY2(interlaced=false)\nConverttoYV12(interlaced=false)" --replace "ConverttoYV12(interlaced=false)" %COMSPEC% /c V:\Replace_Lines.exe --file "%cd%\AllSingleRemuxed\%~nx1.avs" --search "ConvertToYUY2(interlaced=false)\nConvertToYUY2(interlaced=false)" --replace "ConvertToYUY2(interlaced=false)" %COMSPEC% /c V:\Replace_Lines.exe --file "%cd%\AllSingleRemuxed\%~nx1.avs" --search "ConvertToYUY2(interlaced=true)\nConvertToYUY2(interlaced=true)" --replace "ConvertToYUY2(interlaced=true)"
-
Al, for kindness:
but if I need to search this example:
Code:LoadPlugin("V:\automazioneclip\AviSynth\MaskTool2\X64\masktools2.dll") LWLibavVideoSource("C:\Users\Administrator\Desktop\crop resize\24fps OsmoX3 DJI_0001.MP4") #TurnRight() #Turn180() #TurnLeft() #robocrop() ConvertToYUY2(interlaced=false) converttoYV12(interlaced=false).FrameRateConverter(50000,1000, preset = "faster") ConvertToYUY2(interlaced=false) assumeFPS(50)
Code:ConvertToYUY2(interlaced=false) converttoYV12(interlaced=false).FrameRateConverter(50000,1000, preset = "faster") ConvertToYUY2(interlaced=false)
using:
Code:Replace_Lines.exe --file "%cd%\AllSingleRemuxed\%~nx1.avs" --search "converttoYV12(interlaced=false).FrameRateConverter(50000,1000, preset = "faster")\ConvertToYV12(interlaced=false)" --replace "converttoYV12(interlaced=false).FrameRateConverter(50000,1000, preset = "faster")"
is there a way to search also in string that contain double quote to do a new replacement?
my target is to find:
Code:ConvertToYUY2(interlaced=false) converttoYV12(interlaced=false).FrameRateConverter(50000,1000, preset = "faster") ConvertToYUY2(interlaced=false)
Code:converttoYV12(interlaced=false).FrameRateConverter(50000,1000, preset = "faster")
-
It looks like when there are quotes in strings, it does not get to python properly, the shell takes them away by arg parser or something, not sure how to deal with it besides escaping those quotes now.
So when having quotes in search or replace variable, escape them in batch script:
Code:set "search=ConvertToYUY2(interlaced=false)\nconverttoYV12(interlaced=false).FrameRateConverter(50000,1000, preset = \"faster\")\nConvertToYUY2(interlaced=false)" set "replace=converttoYV12(interlaced=false).FrameRateConverter(50000,1000, preset = \"faster\")"
Similar Threads
-
How to catch string in a .txt?
By marcorocchini in forum Newbie / General discussionsReplies: 5Last Post: 5th Feb 2023, 21:50 -
how to insulate a string inside a .txt via batch
By marcorocchini in forum Newbie / General discussionsReplies: 8Last Post: 15th Dec 2022, 14:22 -
How to replace character (string) in batch?
By m0ck in forum Newbie / General discussionsReplies: 1Last Post: 29th Mar 2021, 07:26 -
batch to remove a particular string in a .txt file
By marcorocchini in forum Newbie / General discussionsReplies: 8Last Post: 17th Oct 2019, 19:09 -
how to find and replace string that contain % ?!?
By marcorocchini in forum Newbie / General discussionsReplies: 11Last Post: 18th Feb 2019, 14:08