Author Topic: Simple batch convert with .bat file.  (Read 1498 times)

dosdan

  • Full Member
  • ***
  • Posts: 134
Re: Simple batch convert with .bat file.
« Reply #15 on: April 12, 2019, 12:52:57 AM »
This version continues after an error occurs instead of aborting. It keeps a list of the files which generated processing errors and so were not converted. Since you could be processing hundreds of files, the err msgs may otherwise scroll off the screen.

Convert_MTS_to_MP4_v3.bat
Code: [Select]
@echo off
setlocal EnableDelayedExpansion
cls

if exist conversion_error.txt del /q conversion_error.txt

:FILES_LOOP
for %%F  in (%*) do (
    echo Converting    "%%~fF"    to    "%%~dpFconverted\%%~nF.mp4"
if not exist "%%~dpFconverted\" md "%%~dpFconverted\"
ffmpeg.exe -i "%%~fF" -c:v copy -c:a aac -y -hide_banner -loglevel error -map_metadata 0 "%%~dpFconverted\%%~nF.mp4"
if errorlevel 1 echo error occurred: %%~fF  >> conversion_error.txt

        rem Next line sets the MP4's file date/time to the same as the original MP4 file.
if exist "%%~dpFconverted\%%~nF.mp4" touch -r "%%~fF" "%%~dpFconverted\%%~nF.mp4"

        rem Next line shows the MP4 after it has been created, so with multiple MP4s it acts as a sort of progress indicator.
rem if exist "%%~dpFconverted\%%~nF.mp4" dir "%%~dpFconverted\%%~nF.mp4" | find "/"

        rem Remove REM from the start of the next line if you wish to delete the original file if no error occurred in the conversion.
rem del "%%~fF"

        echo.
shift
)
if not "%1"=="" goto FILES_LOOP
goto FINISHED

:FINISHED
@echo off
if exist conversion_error.txt start /i conversion_error.txt
echo.
echo.
pause

I found using -i "%%F" problematic with files with inverted commas around them. The most consistent results seem to be from using -i "%%~fF".

The file test set consisted of 6 .MTS files, two of which are invalid, and one with a space in its filename. (I also had a space in the current directoryname):
Code: [Select]
09/07/2017  08:43 PM        64,296,960 test 1.MTS
09/07/2017  08:43 PM        64,296,960 test2.MTS
01/04/2019  06:50 AM               325 test3.mts


09/07/2017  08:43 PM        64,296,960 test4.MTS
01/04/2019  06:50 AM               325 test5.mts
09/07/2017  08:43 PM        64,296,960 test6.mts

C:\Filename_test\Directory A>Convert_MTS_to_MP4_v3.bat test*.mts




start /i conversion_error.txt runs the associated program for text files, typically Notepad, and loads the file.

Dan.

sark

  • Newbie
  • *
  • Posts: 11
Re: Simple batch convert with .bat file.
« Reply #16 on: April 12, 2019, 09:07:17 AM »
Quote
Don't know if this is the cause of your problem, but this version works with spaces in the filenames:

Will give it a go tonight. Thanks.

Quote
What is encoding format of the audio stream in the .m2ts files (same as .mts) used for BD?  The Mediainfo program (https://mediaarea.net/en/MediaInfo) will tell you.

I'm pretty certain it's H264. I can use MultiAVCHD to create output from mp4's, but it should be quicker to use m2ts files for BD output and save MultiAVCHD much of the work (That's my thinking).

Quote
From memory, for DVD, the video stream is mpeg-2 (M2V) and the audio stream is either pcm16le (.wav) or mp2, so both steams will need to be converted when converting from MP4 to M2TS/MTS.

Yes DVD is mpeg 2 but I intend creating HD compatible DVDR's (to play in Blu-ray players and HD compatible DVD players) which I believe are the same m2ts as BD output? Standard DVD format is very low resolution compared to HD (about a quarter I believe) although I've produced surprisingly reasonable results with DVDStyler.

sark

dosdan

  • Full Member
  • ***
  • Posts: 134
Re: Simple batch convert with .bat file.
« Reply #17 on: April 12, 2019, 09:44:43 AM »

Quote
What is encoding format of the audio stream in the .m2ts files (same as .mts) used for BD?

I'm pretty certain it's H264.


Sank, H.264 is a video codec, but I was asking about the audio stream. So what audio codec is expected for the audio stream in the .m2ts containers destined for BD?

Dan.


sark

  • Newbie
  • *
  • Posts: 11
Re: Simple batch convert with .bat file.
« Reply #18 on: April 15, 2019, 10:34:58 AM »

Quote
What is encoding format of the audio stream in the .m2ts files (same as .mts) used for BD?

I'm pretty certain it's H264.


Sank, H.264 is a video codec, but I was asking about the audio stream. So what audio codec is expected for the audio stream in the .m2ts containers destined for BD?

Dan.

Firstly, I jumped ahead of myself after reading "What is encoding format"  and missed "audio stream" . Should have said AC3. However, my earlier post was also not as clear as it could have been regarding the DVD output also being HD, which is m2ts-h264-ac3, for playback on compatible players.

Don't know if this is the cause of your problem, but this version Convert_MTS_to_MP4_v2.bat works with spaces in the filenames:

The spaces are not an issue. It always processed one file, regardless of its naming structure. This example (v2.bat) failed to run, creating an error due to not recognising the file path. See below.


 
This version continues after an error occurs instead of aborting. It keeps a list of the files which generated processing errors and so were not converted. Since you could be processing hundreds of files, the err msgs may otherwise scroll off the screen.

Convert_MTS_to_MP4_v3.bat

I haven’t seen any indication (in the cmd window) of an error occuring with the first version. Just the failure to encode more than one file. However, there may well have been an error as this version (Convert_MTS_to_MP4_v3.bat) works perfectly in XP, converting all the files from m2ts-ac3 to mp4-aac (mediainfo confirmed), with no errors reported. See below. So it appears you have solved it as far as XP is concerned. I suspect the first version will run just fine in Win 7. This version as well probably.
 



I found using -i "%%F" problematic with files with inverted commas around them. The most consistent results seem to be from using -i "%%~fF".

I don’t generally use inverted commas for file names. Don’t know if this helped solve the issue. More likely something to do with error reporting perhaps ?

This has been a very useful learning experience. Your help is greatly appreciated. Can’t say I understand the cmd code as it’s less descriptive than some of the simpler coding languages I’ve dabbled with in the past, but I intend delving a bit deeper so I can maybe make some adjustments if required in the future. Where did you source the cmd script info to get you this far ?

Thanks again

sark 

dosdan

  • Full Member
  • ***
  • Posts: 134
Re: Simple batch convert with .bat file.
« Reply #19 on: April 15, 2019, 12:18:46 PM »
Where did you source the cmd script info to get you this far ?


Yes ago I used to write a few batchfiles using the 4DOS/4OS2 command processors. The Windows version of these (4NT) is now available for free as TCC/LE: https://jpsoft.com/products/tcc-le.html

However, cmd.exe, the command proccessor in Windows has got more powerful over the years and you an do a fair bit in it now.   A good reference for the commands is found at https://ss64.com/nt/

Dan.


dosdan

  • Full Member
  • ***
  • Posts: 134
Re: Simple batch convert with .bat file.
« Reply #20 on: April 15, 2019, 12:57:22 PM »

I don’t generally use inverted commas for file names. Don’t know if this helped solve the issue.

It's not just the filename, but the full pathname.  Your directory name My Videos has a space in it. This is not a problem if working within a directory and just using filenames, but I am using full pathnames as I was working towards drag&dropping multiple directories, i.e. working with directory names, not selecting files within a directory. I think there's a difference in how Windows passes the filenames/pathnames with spaces in them in drag&drop vs via the command line as batch parameters.  That's perhaps why "%%~fF" or just "%%~F" seems a better way than either %%F or "%%F", which can end up with none/too many double-inverted commas when passing a mixture of files with full pathnames with and without spaces.  See "Parameter Extensions" at https://ss64.com/nt/syntax-args.html


BTW, I forget to remove the REM from the start of:
if exist "%%~dpFconverted\%%~nF.mp4" dir "%%~dpFconverted\%%~nF.mp4" | find "/"

So it's inactivated at the moment. If you remove the REM it will add date/time and size details e.g:

Code: [Select]
Converting    "C:\Filename_test\Directory A\test 1.MTS"    to    "C:\Filename_test\Directory A\converted\test 1.mp4"
09/07/2017  08:43 PM        60,840,130 test 1.mp4

Converting    "C:\Filename_test\Directory A\test2.MTS"    to    "C:\Filename_test\Directory A\converted\test2.mp4"
09/07/2017  08:43 PM        60,840,130 test2.mp4


The piping ("|") of the DIR's screen output of the details of a single file though the Window's FIND.EXE filter which only keeps the line that contains a date because it contains "/" characters i.e. 09/07/2017

Dan.

« Last Edit: April 15, 2019, 01:27:28 PM by dosdan »

sark

  • Newbie
  • *
  • Posts: 11
Re: Simple batch convert with .bat file.
« Reply #21 on: April 16, 2019, 10:47:21 AM »
Where did you source the cmd script info to get you this far ?


Yes ago I used to write a few batchfiles using the 4DOS/4OS2 command processors. The Windows version of these (4NT) is now available for free as TCC/LE: https://jpsoft.com/products/tcc-le.html

However, cmd.exe, the command proccessor in Windows has got more powerful over the years and you an do a fair bit in it now.   A good reference for the commands is found at https://ss64.com/nt/

Dan.

Will take a look at both, thanks.


I don’t generally use inverted commas for file names. Don’t know if this helped solve the issue.

It's not just the filename, but the full pathname.  Your directory name My Videos has a space in it. This is not a problem if working within a directory and just using filenames, but I am using full pathnames as I was working towards drag&dropping multiple directories, i.e. working with directory names, not selecting files within a directory. I think there's a difference in how Windows passes the filenames/pathnames with spaces in them in drag&drop vs via the command line as batch parameters.  That's perhaps why "%%~fF" or just "%%~F" seems a better way than either %%F or "%%F", which can end up with none/too many double-inverted commas when passing a mixture of files with full pathnames with and without spaces.  See "Parameter Extensions" at https://ss64.com/nt/syntax-args.html


Interestingly DVDStyler fails to encode if it's installed in "Program Files" because of the space in the path name. It has to be installed in C:, or Windows. DVDStyler calls an ffmpeg.bat to encode. Something to do with double quotes not being used on the called path name causing issues.


BTW, I forget to remove the REM from the start of:
if exist "%%~dpFconverted\%%~nF.mp4" dir "%%~dpFconverted\%%~nF.mp4" | find "/"

Dan.

Yes, I noticed that. I thought I had done it whilst disabling/enabling touch  to check it functioned correctly. When touch is enabled the Modified date in Properties remains the same as the original, when remarked out, the current date displays in Modified. In both cases the Created date is the current date. This is fine, but wasn't sure if that's how it was intended?

sark
« Last Edit: April 17, 2019, 09:09:39 AM by sark »

dosdan

  • Full Member
  • ***
  • Posts: 134
Re: Simple batch convert with .bat file.
« Reply #22 on: April 16, 2019, 01:14:46 PM »
When touch is enabled the Modified date in Properties remains the same as the original, when remarked out, the current date displays in Modified. In both cases the Created date in the current date. This is fine, but wasn't sure if that's how it was intended?

There's an amount of flexibility with both TOUCH & DIR. The reason I added TOUCH to the batchfile was, when converting my camcorder files from AVCHD to MP4, I wanted the file dates to stay the same, as it gives me an external indication of when the video was shot.

Both TOUCH and DIR can work with Created, Modified or Last Accessed date/times so you can set the batchfile up to suit yourself:

TOUCH
Code: [Select]
Changes files access, modification and creation times.

Usage: touch [-acmpsRvx] [-r REFFILE | -t TIME | -d DATETIME] FILE...

A FILE argument that does not exist is created empty, unless -c or --no-create
is supplied.

  -a, --access-time        Change file access time.
  -c, --no-create          Do not create any new files.
  -m, --modif-time         Change file modification time.
  -p, --pause-exit         Pause on exit.
  -s, -R, --recursive      Recursively touch files in specified directory and
                           all subdirectories.
  -v, --verbose            Output the result of every file processed.
  -x, --creation-time      Change file creation time.

  -r, --reference REFFILE  Use this file's times instead of current time.
  -t, --time TIME          Use [[CC]YY]MMDDhhmm[.ss] instead of current time.
  -d, --date DATETIME      Use YYYY-MM-DDThh:mm:SS[.frac] instead of
                           current time.

  -h, --help               Display this help and exit.
  --version                Display version information and exit.


With DIR, the /TC/TA/TW switches control its date/time display & sorting:
Code: [Select]
/T          Controls which time field displayed or used for sorting timefield
              C  Creation
              A  Last Access
              W  Last Written

Dan.
« Last Edit: April 16, 2019, 01:23:48 PM by dosdan »

dosdan

  • Full Member
  • ***
  • Posts: 134
Re: Simple batch convert with .bat file.
« Reply #23 on: April 16, 2019, 01:33:35 PM »
Interestingly DVDStyler fails to encode if it's installed in "Program Files" because of the space in the path name. It has to be installed in C:, or Windows. DVDStyler calls an ffmpeg.bat to encode. Something to do with double quotes not being used on the called path name causing issues.

You could probably place it elsewhere as long as the pathname has no space in it e.g. C:\Video_Utils\DVDStyler.

As well as spaces, you should test for directory or file names containing an inverted coma e.g. Dan's_Holiday_Video.mts and Dan's Holiday Video.mts.

Dan.

sark

  • Newbie
  • *
  • Posts: 11
Re: Simple batch convert with .bat file.
« Reply #24 on: April 17, 2019, 09:23:58 AM »
Interestingly DVDStyler fails to encode if it's installed in "Program Files" because of the space in the path name. It has to be installed in C:, or Windows. DVDStyler calls an ffmpeg.bat to encode. Something to do with double quotes not being used on the called path name causing issues.

As well as spaces, you should test for directory or file names containing an inverted coma e.g. Dan's_Holiday_Video.mts and Dan's Holiday Video.mts.

Dan.

Will bare that in mind.

When touch is enabled the Modified date in Properties remains the same as the original, when remarked out, the current date displays in Modified. In both cases the Created date in the current date. This is fine, but wasn't sure if that's how it was intended?

There's an amount of flexibility with both TOUCH & DIR. The reason I added TOUCH to the batchfile was, when converting my camcorder files from AVCHD to MP4, I wanted the file dates to stay the same, as it gives me an external indication of when the video was shot.

Both TOUCH and DIR can work with Created, Modified or Last Accessed date/times so you can set the batchfile up to suit yourself:

TOUCH
Code: [Select]
Changes files access, modification and creation times.

Usage: touch [-acmpsRvx] [-r REFFILE | -t TIME | -d DATETIME] FILE...

A FILE argument that does not exist is created empty, unless -c or --no-create
is supplied.

  -a, --access-time        Change file access time.
  -c, --no-create          Do not create any new files.
  -m, --modif-time         Change file modification time.
  -p, --pause-exit         Pause on exit.
  -s, -R, --recursive      Recursively touch files in specified directory and
                           all subdirectories.
  -v, --verbose            Output the result of every file processed.
  -x, --creation-time      Change file creation time.

  -r, --reference REFFILE  Use this file's times instead of current time.
  -t, --time TIME          Use [[CC]YY]MMDDhhmm[.ss] instead of current time.
  -d, --date DATETIME      Use YYYY-MM-DDThh:mm:SS[.frac] instead of
                           current time.

  -h, --help               Display this help and exit.
  --version                Display version information and exit.


With DIR, the /TC/TA/TW switches control its date/time display & sorting:
Code: [Select]
/T          Controls which time field displayed or used for sorting timefield
              C  Creation
              A  Last Access
              W  Last Written

Dan.

Will save this for future reference. As long as I have an indicator of the original time, regardless of what it falls under, I'm happy for now.

sark

Sark