Log in

View Full Version : Simple programming request


kolak
13th September 2012, 17:02
Just need a bat (or whatever file), which would automatically create simple avisynth script from dragged file- either avisource or qtinput (based on extension).

So when I drag on it avi file I got:

avisynth file with the same name (can be same location as avi file), but avs extension with just:

avisource("path to my avi file", audio=false)

In case of mov it should use:

qtinput("path to mov", audio=false)

Thanks a lot for help :thanks:

manolito
13th September 2012, 18:31
That's an easy one...:)

@echo off
SET name=%~dpn1
SET ext=%~x1
SET filter=qtinput
IF %ext%==.avi SET filter=avisource
ECHO %filter%("%1", audio=false) >"%name%.avs

You can 'Drag & Drop' or 'Copy & Paste' source files on this batch file.


Cheers
manolito

von_Runkel
13th September 2012, 18:37
A quick two-liner:
if %~x1==.avi echo avisource("%~f1", audio=false)>"%~n1".avs
if %~x1==.mov echo qtinput("%~f1", audio=false)>"%~n1".avs

EDIT: Sometimes the extension is .AVI and not .avi (or .MOV and not .mov). To address this, the if statement needs the /i switch (ignore case):
if /i %~x1==.avi echo avisource("%~f1", audio=false)>"%~n1".avs
if /i %~x1==.mov echo qtinput("%~f1", audio=false)>"%~n1".avs

von_Runkel
13th September 2012, 19:17
That's an easy one...:)

@echo off
SET name=%~dpn1
SET ext=%~x1
SET filter=qtinput
IF %ext%==.avi SET filter=avisource
ECHO %filter%("%1", audio=false) >"%name%.avs

You can 'Drag & Drop' or 'Copy & Paste' source files on this batch file.


Cheers
manolito
If I drop a file with spaces in the name on this batch file it produces a .avs file with doubled quotation marks:

avisource(""C:\source\Film 1.avi"", audio=false)

If I drop a file without spaces in the file name, the .avs file is correct:

avisource("C:\source\Film1.avi", audio=false)

My solution is correct with or without spaces in the file name.

kolak
13th September 2012, 20:36
Thanks guys- you are simply amazing :) !!!!

Can we do the same with whole folder with mov and avis inside or when I drag few files at the same time?
Also- does this method have any limitation in terms of file naming (special characters etc) and how deeply in folder they are?

update:

Played with it myself:

for %%a IN (*.avi) DO echo avisource("%%~fa", audio=false)>"%%~na".avs
for %%a IN (*.mov) DO echo qtinput("%%~fa", audio=false)>"%%~na".avs

when you drag one file (can be any) it will create avisynth scripts for all .avi and .mov files in this folder :)

manolito
14th September 2012, 04:05
Check out this one:
@echo off
REM Test if parameter is a folder
SET dummy=%~x1
IF !%dummy%==! GOTO :folder

FOR %%a IN (%*) DO CALL :exec %%a
GOTO :EOF

:folder
SET folder=%~1
FOR %%a IN ("%folder%\*.avi") DO CALL :exec "%%a"
FOR %%a IN ("%folder%\*.mov") DO CALL :exec "%%a"
GOTO :EOF

:exec
SET name=%~dpn1
SET ext=%~x1
SET filter=qtinput
IF /i %ext%==.avi SET filter=avisource
ECHO %filter%("%~1", audio=false) >"%name%.avs"

It can take either single or multiple files as its input, but you can also drop a folder on it.



Cheers
manolito

Mug Funky
14th September 2012, 05:51
sometimes it's good to be able to drop a bunch of files at once, in which case we have this:



:batch
if "%~1"=="" goto end
if /i %~x1==.avi echo avisource("%~f1", audio=false)>"%~n1".avs
if /i %~x1==.mov echo qtinput("%~f1", audio=false)>"%~n1".avs
shift
goto batch
:end

Chetwood
14th September 2012, 07:27
You guys rock. Interested in one more? I'd need a batch to mux all external VobSubs into the corresponding mkvs from one folder to another with MKVmerge. Folder looks like this:

tvshow1x01.idx
tvshow1x01.mkv
tvshow1x01.sub
tvshow1x01-eng.idx
tvshow1x01-eng.sub


tvshow1x02.idx
tvshow1x02.mkv
tvshow1x02.sub
tvshow1x02-eng.idx
tvshow1x02-eng.sub
...

whereas the first sub should be muxed as subtitle track 1 (named "deu") and -eng track 2 (named "eng") inside the new MKV and both default to off. It's also important to note that mkvmerge only imports the idx file and automatically grabs the sub. Thanks.

kolak
14th September 2012, 09:53
sometimes it's good to be able to drop a bunch of files at once, in which case we have this:



:batch
if "%~1"=="" goto end
if /i %~x1==.avi echo avisource("%~f1", audio=false)>"%~n1".avs
if /i %~x1==.mov echo qtinput("%~f1", audio=false)>"%~n1".avs
shift
goto batch
:end


My script does the same, but you drag only one file (can be actually any file in given folder, even not avi or mov ).

It has been a funny exercise- thanks a lot for all suggestions!

:thanks:

von_Runkel
14th September 2012, 17:39
for %%a IN (*.avi) DO echo avisource("%%~fa", audio=false)>"%%~na".avs
for %%a IN (*.mov) DO echo qtinput("%%~fa", audio=false)>"%%~na".avs

TIP:
If you add /r to the for statement (for /r %%a IN etc) , your script loops recursively through all subfolders. Can be handy sometimes.

kolak
14th September 2012, 17:51
Thanks- I don't really know bat commands- made it by analysing other scripts :)

von_Runkel
14th September 2012, 17:58
Thanks- I don't really know bat commands- made it by analysing other scripts :)
Well, that is how we learn things isn't it? :)

kolak
15th September 2012, 00:25
Yep- funny enough I've done my degree without looking into almost single book- rather by analysing and testing- maybe it takes longer, but stays way longer in your head :)

manolito
15th September 2012, 01:36
@Chetwood

The batch part of your request should not be so hard. But I need a working mkvmerge command line.

Could you please use mkvmergeGUI and make all the necessary settings for a single file. I.E. add the source MKV, add both subtitle streams, specify the track numbers and languages, set them to OFF by default. Specify the output folder and then under the "Mux" menu entry get the corresponding command line and publish it here...

Another question:
Do you always want to save the resulting MKV in a constant output folder or do you need to specify different output folders every time? And am I right that the resulting MKV should have the same name as the source MKV, just in a different folder?


Cheers
manolito

Chetwood
15th September 2012, 07:12
Nice one, wasn't aware that the command-line could be extracted!

"D:\Programme\MKVtoolnix\mkvmerge.exe" -o "E:\\done\\My.TV.show.1x01.mkv" "--track-name" "0:" "--default-track" "0:yes" "--forced-track" "0:no" "--display-dimensions" "0:1280x720" "--compression" "0:none" "--language" "1:ger" "--track-name" "1:deu " "--default-track" "1:yes" "--forced-track" "1:no" "--compression" "1:none" "--language" "2:eng" "--track-name" "2:en" "--default-track" "2:no" "--forced-track" "2:no" "--compression" "2:none" "-a" "1,2" "-d" "0" "-S" "-T" "--no-global-tags" "(" "D:\\2 mux\\My.TV.show.1x01.mkv" ")" "--language" "0:ger" "--track-name" "0:deu" "--default-track" "0:yes" "--forced-track" "0:yes" "-s" "0" "-D" "-A" "-T" "--no-global-tags" "--no-chapters" "(" "D:\\2 mux\\My.TV.show.1x01-forced.idx" ")" "--language" "0:ger" "--track-name" "0:deu" "--default-track" "0:no" "--forced-track" "0:no" "-s" "0" "-D" "-A" "-T" "--no-global-tags" "--no-chapters" "(" "D:\\2 mux\\My.TV.show.1x01.idx" ")" "--language" "0:eng" "--track-name" "0:en" "--default-track" "0:no" "--forced-track" "0:no" "-s" "0" "-D" "-A" "-T" "--no-global-tags" "--no-chapters" "(" "D:\\2 mux\\My.TV.show.1x01-eng.idx" ")" "--track-order" "0:0,0:1,0:2,1:0,2:0,3:0" "--title" "\\My.TV.show.1x01"

And yes, the resulting mkv should have the same name as the input mkv, in the above case My.TV.show.1x01.mkv, and the output folder would be defined inside the batch, like this:


REM ### options ###

set mkvmerge_exe=
REM - full path to mkvmerge.exe, e.g. D:\Programme\MKVToolnix\mkvmerge.exe

set in=
REM - source dir containing all MKVs to be remuxed

set out=
REM - destination dir, files already present will be overwritten!


Please keep in mind that paths/filenames could contain whitespace.

EDIT: I forgot some of those mks have an extra forced track that if present, should be first, set to deu and flagged as default = yes, forced = yes.

manolito
15th September 2012, 23:35
Try this one:

REM ### options ###

set mkvmerge_exe=
REM - full path to mkvmerge.exe, e.g. D:\Programme\MKVToolnix\mkvmerge.exe

set in=
REM - source dir containing all MKVs to be remuxed

set out=
REM - destination dir, files already present will be overwritten!


REM - No quotes or trailing backslash, please...




SET in=%in:\=\\%
SET out=%out:\=\\%
REM - Replace single backslash with double backslash


FOR %%a IN ("%~dp1%\*.mkv") DO "%mkvmerge_exe%" -o "%out%\\%%~nxa" "--track-name" "0:" "--default-track" "0:yes" "--forced-track" "0:no" "--display-dimensions" "0:1280x720" "--compression" "0:none" "--language" "1:ger" "--track-name" "1:deu " "--default-track" "1:yes" "--forced-track" "1:no" "--compression" "1:none" "--language" "2:eng" "--track-name" "2:en" "--default-track" "2:no" "--forced-track" "2:no" "--compression" "2:none" "-a" "1,2" "-d" "0" "-S" "-T" "--no-global-tags" "(" "%in%\\%%~nxa" ")" "--language" "0:ger" "--track-name" "0:deu" "--default-track" "0:yes" "--forced-track" "0:yes" "-s" "0" "-D" "-A" "-T" "--no-global-tags" "--no-chapters" "(" "%in%\\%%~na-forced.idx" ")" "--language" "0:ger" "--track-name" "0:deu" "--default-track" "0:no" "--forced-track" "0:no" "-s" "0" "-D" "-A" "-T" "--no-global-tags" "--no-chapters" "(" "%in%\\%%~na.idx" ")" "--language" "0:eng" "--track-name" "0:en" "--default-track" "0:no" "--forced-track" "0:no" "-s" "0" "-D" "-A" "-T" "--no-global-tags" "--no-chapters" "(" "%in%\\%%~na-eng.idx" ")" "--track-order" "0:0,0:1,0:2,1:0,2:0,3:0" "--title" "\\%%~na"

I could not really test it because I don't have any subtitle files. :sly:

Good luck
manolito


//Edit
I noticed that the mkvmerge command line contains the settings for the extra forced subtitles track. I have no idea how mkvmerge behaves if these settings are applied to an MKV without this extra track.

If mkvmerge chokes on these files, the batch file must probably be rewritten. Then two separate command lines must be used. The batch file has to determine for each MKV if the extra track is present and then branch to the approriate command line accordingly.

sneaker_ger
16th September 2012, 07:14
I noticed that the mkvmerge command line contains the settings for the extra forced subtitles track. I have no idea how mkvmerge behaves if these settings are applied to an MKV without this extra track.

If mkvmerge chokes on these files, the batch file must probably be rewritten. Then two separate command lines must be used. The batch file has to determine for each MKV if the extra track is present and then branch to the approriate command line accordingly.

Mkvmerge will throw an error and not do anything if any of the input files doesn't exist.

manolito
17th September 2012, 01:51
@Chetwood
Update:

Alright, I wanted to get something done before hitting the sack...:devil:

So I decided to edit the mkvmerge command line to adapt it for sources without a forced subs stream. Works on my machine, might just work on your computer, too...

@echo off
REM ### options ###

set mkvmerge_exe=
REM - full path to mkvmerge.exe, e.g. D:\Programme\MKVToolnix\mkvmerge.exe

set in=
REM - source dir containing all MKVs to be remuxed

set out=
REM - destination dir, files already present will be overwritten!

REM - No quotes or trailing backslash, please...


SET in=%in:\=\\%
SET out=%out:\=\\%
REM - Replace single backslash with double backslash


FOR %%a IN ("%in:\\=\%\*.mkv") DO IF EXIST "%%~dpna-forced.*" (
CALL :forced "%%a"
) ELSE (
CALL :no_forced "%%a"
)

GOTO :EOF


:forced
"%mkvmerge_exe%" -o "%out%\\%~nx1" "--track-name" "0:" "--default-track" "0:yes" "--forced-track" "0:no" "--display-dimensions" "0:1280x720" "--compression" "0:none" "--language" "1:ger" "--track-name" "1:deu " "--default-track" "1:yes" "--forced-track" "1:no" "--compression" "1:none" "--language" "2:eng" "--track-name" "2:en" "--default-track" "2:no" "--forced-track" "2:no" "--compression" "2:none" "-a" "1,2" "-d" "0" "-S" "-T" "--no-global-tags" "(" "%in%\\%~nx1" ")" "--language" "0:ger" "--track-name" "0:deu" "--default-track" "0:yes" "--forced-track" "0:yes" "-s" "0" "-D" "-A" "-T" "--no-global-tags" "--no-chapters" "(" "%in%\\%~n1-forced.idx" ")" "--language" "0:ger" "--track-name" "0:deu" "--default-track" "0:no" "--forced-track" "0:no" "-s" "0" "-D" "-A" "-T" "--no-global-tags" "--no-chapters" "(" "%in%\\%~n1.idx" ")" "--language" "0:eng" "--track-name" "0:en" "--default-track" "0:no" "--forced-track" "0:no" "-s" "0" "-D" "-A" "-T" "--no-global-tags" "--no-chapters" "(" "%in%\\%~n1-eng.idx" ")" "--track-order" "0:0,0:1,0:2,1:0,2:0,3:0" "--title" "\\%~n1"
GOTO:EOF


:no_forced
"%mkvmerge_exe%" -o "%out%\\%~nx1" "--track-name" "0:" "--default-track" "0:yes" "--forced-track" "0:no" "--display-dimensions" "0:1280x720" "--compression" "0:none" "--language" "1:ger" "--track-name" "1:deu " "--default-track" "1:yes" "--forced-track" "1:no" "--compression" "1:none" "--language" "2:eng" "--track-name" "2:en" "--default-track" "2:no" "--forced-track" "2:no" "--compression" "2:none" "-a" "1,2" "-d" "0" "-S" "-T" "--no-global-tags" "(" "%in%\\%~nx1" ")" "--language" "0:ger" "--track-name" "0:deu" "--default-track" "0:no" "--forced-track" "0:no" "-s" "0" "-D" "-A" "-T" "--no-global-tags" "--no-chapters" "(" "%in%\\%~n1.idx" ")" "--language" "0:eng" "--track-name" "0:en" "--default-track" "0:no" "--forced-track" "0:no" "-s" "0" "-D" "-A" "-T" "--no-global-tags" "--no-chapters" "(" "%in%\\%~n1-eng.idx" ")" "--track-order" "0:0,0:1,0:2,1:0,2:0" "--title" "\\%~n1"


Good luck
manolito

Reino
23rd September 2012, 15:06
test.bat:
:batch
if "%~1"=="" goto end
echo video=FFVideoSource("%~f1")>"%~f1".avs
echo audio=BassAudioSource("%~f1")>>"%~f1".avs
echo AudioDub(video,audio.TimeStretch(pitch=432.0/4.4))>>"%~f1".avs
shift
goto batch
:end
This bat-file works great (test.bat file and drag&drop), except when there's a comma in the directory path. test.bat file does still work then, but drag&drop is totally screwed. 2 Files are even created in C:\Documents and Settings\Admin.
I've read (http://www.robvanderwoude.com/parameters.php) about (http://www.robvanderwoude.com/escapechars.php) the comma being an "escape character", but how can I modify the bat-file to actually include the comma in the file-path upon drag&drop.

Btw, can anyone tell me why, unlike the bat-file above, the bat-file below doesn't work for multiple echo lines?
:batch
if "%~1"=="" goto end
(
echo video=FFVideoSource("%~f1")
echo audio=BassAudioSource("%~f1")
echo AudioDub(video,audio.TimeStretch(pitch=432.0/4.4))
) >"%~f1".avs
shift
goto batch
:end

sneaker_ger
24th September 2012, 18:06
Because every ")" would end the if loop?

von_Runkel
24th September 2012, 18:18
Btw, can anyone tell me why, unlike the bat-file above, the bat-file below doesn't work for multiple echo lines?
:batch
if "%~1"=="" goto end
(
echo video=FFVideoSource("%~f1")
echo audio=BassAudioSource("%~f1")
echo AudioDub(video,audio.TimeStretch(pitch=432.0/4.4))
) >"%~f1".avs
shift
goto batch
:end
This one works for me:

:batch

if "%~1"=="" goto end
echo video=FFVideoSource("%~f1")>"%~f1".avs
echo audio=BassAudioSource("%~f1")>>"%~f1".avs
echo AudioDub(video,audio.TimeStretch(pitch=432.0/4.4))>>"%~f1".avs
shift
goto batch

:end

As other stated, it is probably the "(" and ")" in your example.

Reino
24th September 2012, 21:57
Weird. http://stackoverflow.com/questions/5181212/windows-in-batch-file-write-multiple-lines-to-text-file (http://stackoverflow.com/questions/5181212/windows-in-batch-file-write-multiple-lines-to-text-file) gave me the hint, but apparently it's not working.

Run from cmd, that one works for me too, von_Runkel, but does it work for you when you drag&drop a file which directory-name contains a comma (,)?

von_Runkel
27th September 2012, 19:07
Run from cmd, that one works for me too, von_Runkel, but does it work for you when you drag&drop a file which directory-name contains a comma (,)?

No, it does not, and that is weird. It puts the .avs file in the same location as the batch file.

It seems that a comma in the folder name divides the %1 into several arguments.

I will have to look into this and see if there is something that I have missed or if it is normal bahaviour (or a bug?).

von_Runkel
28th September 2012, 18:34
The problem is that comma (,) and several other delimiters (space, equal sign etc.) are treated as they are; delimiters.

This affects multi input parameters to the cmd process. The parameters to cmd.exe is sent "as is", and cmd.exe is then trying to interpret the string. For example, when cmd.exe reads a comma, it terminates further reading and comes to the conclusion that this is parameter 1 (%1). And the %1 variable is of course not a valid path, because it is truncated.

However, there is a way to tell cmd.exe to treat the parameter string natively, using the special variable %*. This variable is the full string sent to cmd.exe. The problem is, alas, that it works for parameters with commas (and other delimiters), but not for paths that includes spaces. Here, the path with spaces are quoted, and if you use "%*" the paths with spaces will be double quoted, which means that the commas will be exposed to cmd.exe! (which is not what we want).

Catch 22 syndrome.

Does this make sense to you?

I can write a batch file for you that works with commas (and other delimiters), but that will NOT work with spaces in the path. And vice versa, I can write a batch file for you that works with spaces (the one that I showed you), but not with delimiters.

If someone else could come up with a solution, I'd be more than happy.

If you are running Windows 7, then I think Powershell is the solution.

Let me know what you think.

Reino
30th September 2012, 12:56
I thought there would be an easy way to create a batch-script to literally interpret paths (including delimiters). Although it's not that urgent at all, I'd be interested in what you come up with.
I'm running WinXP btw.

Reino
21st August 2013, 19:27
I had time to look up some old stuff like this for instance and in the end the issue was caused by the directory name, which had brackets and multiple dots in it (a relic from years ago). Once I gave it a normal name, the issue was gone and the batch-script works as it should.

Blue_MiSfit
22nd August 2013, 08:10
Templates / macros in AvsP are a good solution too.