News:

--

Main Menu

Simple batch convert with .bat file.

Started by sark, April 03, 2019, 09:43:34 AM

Previous topic - Next topic

sark

Hi all.

I'm trying to batch convert .m2ts files to .mp4, but I can't get the .bat script below to set the container to .mp4. At the moment although it adds an .mp4 extension, avidemux leaves the container to the default .mkv during processing and the files remain m2ts. They do display as .mp4, but a mediainfo check confirms they're matroska .mkv files.
Anyone know how I set the container to .mp4.

Cheers

sark

set avidemux="C:\Program Files\Avidemux 2.7\avidemux.exe"
set videocodec=Copy
set audiocodec=Copy
for %%f in (*.m2ts) do %avidemux% --video-codec %videocodec% --audio-codec %audiocodec% --force-alt-h264 --load "%%f" --save "%%f.mp4" --quit

eumagga0x2a

--output-format MP4

is missing in the command line.

dosdan

#2
I'd do this in FFMPEG, not ADM. What is the audio stream inside the m2ts container?

Dan.

dosdan

#3
Here's a batchfile I wrote some time ago to batch convert M2TS/MTS AVCHD files from my camcorder to MP4s. 

These AVCHD files contain AC-3 audio streams. These are incompatible with the MP4 container, so the audio stream is converted to AAC.   I also use touch.exe (https://www.binarez.com/touch_dot_exe/) to set the date/time on the new MP4 to be the same as the original M2TS/MTS.

You place the shortcut icon for this batchfile on your Windows desktop and then drag-and-drag one or more MTS/M2TS files onto it. It creates a "converted" subdirectory, if it doesn't already exist, beneath the location of the file(s) you wish to be converted. If FFMPEG.EXE and TOUCH.EXE are not situated in a directory mentioned in your Path list, specify their full pathname in the batchfile e.g. c:\utils\ffmpeg.exe


Convert_MTS_to_MP4.bat
@echo off

cls
set filename=""

:FILES_LOOP
for %%F in (%1) do (
set filename=%%F 
echo Converting    %%F    to    %%~dpFconverted\%%~nF.mp4
if not exist "%%~dpFconverted\" md "%%~dpFconverted\"
ffmpeg.exe -i %%F -c:v copy -c:a aac -y -hide_banner -loglevel error -map_metadata 0 "%%~dpFconverted\%%~nF.mp4"
if errorlevel 1 goto ERROR_OCCURRED
rem Next line sets the MP4's file date/time to the same as the original MP4 file.
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.
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 %%F
echo.
shift
)
if not "%1"=="" goto FILES_LOOP
goto FINISHED

:ERROR_OCCURRED
echo.
echo An error occurred when trying to process %filename%
echo Aborting...

:FINISHED
echo.
echo.
pause


Dan.

sark

Quote from: eumagga0x2a on April 03, 2019, 11:28:07 AM
--output-format MP4

is missing in the command line.

Does that go in the last line. (before --save "%%f.mp4)? Will give it a go tonight.

Cheers.

Quote from: dosdan on April 03, 2019, 07:57:23 PM
I'd so this in FFMPEG, not ADM. What is the audio stream inside the m2ts container?

Dan.

H264... Avidemux should just change the container, though I will need it to convert from AC3 to AAC, but that shouldn't be an issue.

Quote from: dosdan on April 03, 2019, 08:13:19 PM
Here's a batchfile I wrote some time ago to batch convert M2TS/MTS AVCHD files from my camcorder to MP4s. 


That's really appreciated. Will give that a go tonight. Already have ffmpeg installed and maintaining correct dates would be useful. If, for some reason I wanted to create current dates, would it be a simple matter of removing that one line (beginning touch).

Thanks for the replies. They're really appreciated.

sark

dosdan

Quote from: sark on April 04, 2019, 10:18:20 AM
If, for some reason I wanted to create current dates, would it be a simple matter of removing that one line (beginning touch).

Yes. You can also leave the line in the batchfile, but inactivate it by placing an REM (remark) command at the start of that line.

Dan.

sark

Quote
Yes. You can also leave the line in the batchfile, but inactivate it by placing an REM (remark) command at the start of that line.

Of course, didn't think of that.
Haven't had it running yet, I had an error. I think there is an issue with my ffmpeg, which is actually the "ffmpeg for Audacity" version. I'm going to download a standalone version today and install that tonight. Just one other question in case I still hit a problem.

Quote
If FFMPEG.EXE and TOUCH.EXE are not situated in a directory mentioned in your Path list, specify their full pathname in the batchfile e.g. c:\utils\ffmpeg.exe

What exactly do you mean by Path list, and where in your code would the pathname go. I can see nothing in your code that indicates pointing to ffmpeg.exe or touch.exe. Probly missing something obvious, but would be grateful for some clarification to ensure it's not an issue with my installations.

Cheers

sark

eumagga0x2a

PATH is an environment variable holding a list of directories which get searched for executables with given name so that you may skip specifying the full path to a particular executable.

dosdan

#8
Go to the command prompt, (Type CMD in the Windows Search box and then press Enter). The prompt ending in ">" shows your current drive and the directory you are situated in e.g.
C:\Windows\System32>

When you go to execute a program, e.g. ffmpeg.exe which, let's say, is stored in the C:\Utilities directory, there are 3 ways to do this:


  • Specify the full path in front of the command:
    c:\utilities\ffmpeg.exe
    This is probably the best way if you invoke the program from within a batch file.

  • First change to the directory it's located in and then execute it (two separate commands):
    CD \utilities (no need to specify C: if you are already situated on that drive), then ffmpeg.exe (or just ffmpeg)

  • Either copy ffmpeg.exe to a directory that is already in the PATH list of directories, or add its location (C:\Utilities) to the list of directories in your PATH list. When you try to run a executable (a file with .COM, .EXE or .BAT file extension), and you don't specify its location, the operating system looks first in your current location and then in the locations in the PATH list. If it's in one of these locations you can use just: ffmpeg
    while situated in any location.

For example, you might have lot of little utility programs and batch files which you want to be able to run from anywhere. Type PATH at the command line. Mine shows:

PATH=C:\Mingw\local64\bin-video;C:\Mingw\local64\bin-audio;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86);C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\GNU\GnuPG\pub;C:\Program Files (x86)\GNU\GnuPG;C:\Program Files (x86)\WinMerge;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\QuickTime\QTSystem\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Common Files\Acronis\VirtualFile\;C:\Program Files (x86)\Common Files\Acronis\VirtualFile64\;C:\Program Files (x86)\Common Files\Acronis\SnapAPI\;C:\Program Files\nodejs\;C:\Program Files\gnuplot\bin;C:\Users\Dan\AppData\Local\Microsoft\WindowsApps;d:\YouTube_dl;C:\Users\Dan\AppData\Roaming\npm;

Say you decide that these small useful programs will be stored in C:\Utilites. We need to add C:\Utilities to the path list. In Windows, go to Control Panel | System | Advanced System Settings | Environment Variables. In the "User Variables for [your user name]" section, highlight the "Path" line and then click on "Edit". Click on "New", type in c:\utilities (case is not important in Windows) and click on OK, and then OK again, and OK again to finish closing all these windows.





We need to load a new instance of CMD, as the current instance will not be aware of the change in the path yet. If the CMD window is still open, close it by typing EXIT, then type CMD again to open as a new instance. Type PATH to check again. I see (scroll to the end of the line below to see the addition):

PATH=C:\Mingw\local64\bin-video;C:\Mingw\local64\bin-audio;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86);C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\GNU\GnuPG\pub;C:\Program Files (x86)\GNU\GnuPG;C:\Program Files (x86)\WinMerge;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\QuickTime\QTSystem\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Common Files\Acronis\VirtualFile\;C:\Program Files (x86)\Common Files\Acronis\VirtualFile64\;C:\Program Files (x86)\Common Files\Acronis\SnapAPI\;C:\Program Files\nodejs\;C:\Program Files\gnuplot\bin;C:\Users\Dan\AppData\Local\Microsoft\WindowsApps;d:\YouTube_dl;C:\Users\Dan\AppData\Roaming\npm;c:\utilities;

Now, from any drive and directory you can just type
ffmpeg


You may also have more than one copy of FFMPEG.EXE in your path list. There is a WHERE command that you can use to see which one will be found first. In the example below, there are 3 instances of the search string found. The second find is the one which will be executed since the first find is not an executable. (Note: I don't have a c:\utilities in my path list):

C:\WINDOWS\system32>where /t ffmpeg
       163   22/03/2019    7:01:02 AM  C:\Mingw\media-autobuild_suite-master\local64\bin-video\ffmpeg
  25594880   22/03/2019    7:01:00 AM  C:\Mingw\media-autobuild_suite-master\local64\bin-video\ffmpeg.exe
  25594880   22/03/2019    7:01:00 AM  d:\YouTube_dl\ffmpeg.exe


Note: the "/t" switch is used here to show the date/time and size of the matches. FFMPEG.EXE is included in a lot of freeware video programs, and you often end up with multiple older copies of it on your HDD.

Dan.


sark

Firstly, thanks for all the input, it really is appreciated and has proved extremely informative.

I've spent the last few days trying to solve some problems that were due to me testing everything on an XP system. I finally realised I was using the wrong ffmpeg version.
As a result of replacing several previous versions with ffmpeg 3.3 win32 static I thought I had everything working. Whilst the bat file converts to mp4 (aac) with original time. I can't get it to do this to more than one file.
If I select more than one file and drag onto the bat file, only one m2ts is converted?
Could this be a Win XP issue perhaps? I'm in the process of switching a Win 7 system over to 64bit, so am currently playing around on an older XP system. Is it possible the cmd line script could differ for versions later than XP. If so, this is fine as I'll be using it on Win 7 64bit ultimately.

Thanks again

sark

dosdan

#10
Quote from: sark on April 10, 2019, 10:52:36 AM
Could this be a Win XP issue perhaps?

Possibly. I'm surprised it works under XP. I think the batch language was significantly enhanced in Win7.

You still have the SHIFT command in the right place? It's what causes the loop to see a different input file each time.

Here is a multi-file test I did under Win10:

I copied/renamed a small .MTS file to test1.mts, test2.mts & test4.mts. I also created an invalid ("corrupted") .MTS, test3.mts, by copy/renaming a text file i.e.

copy copy mylist.txt test3.mts

Here's the 4-file test set:

dir test?.mts

Directory of C:\Filename_test

09/07/2017  08:43 PM        64,296,960 test1.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


Now I ran this set though the conversion batchfile from the command line. (Should give the same result as specifying each file separately on  the command line i.e. "Convert_MTS_to_MP4.bat test1.mts test2.mts test3.mts test4.mts" or using drag-and-drop.)

Convert_MTS_to_MP4.bat test?.mts
Converting    test1.MTS    to    C:\Filename_test\converted\test1.mp4
09/07/2017  08:43 PM        60,840,130 test1.mp4

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

Converting    test3.mts    to    C:\Filename_test\converted\test3.mp4
test3.mts: Invalid data found when processing input

An error occurred when trying to process test3.mts
Aborting...


Press any key to continue . . .



You can see that two files were converted before the bogus .mts caused FFMPEG to throw an errorlevel of 1 or greater and the batchfile detected this and stopped.

By the way, this thread covers similar ground: https://avidemux.org/smif/index.php?topic=18522.0

Dan.

sark

The conversion doesn't abort. Everything runs just fine except there is only one converted file in the folder. I'm pretty certain it's an XP issue. I don't intend using XP so I'm sure all will work fine when I get the Win 7 (64bit) setup.

Thanks again for the input. It's encouraged me to delve a bit deeper to get a grip of the coding and create an opposite version of the script (mp4 to m2ts) for creating HD DVD/BD output.

sark


dosdan

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

Convert_MTS_to_MP4_v2.bat
@echo off

cls
set filename=""

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

        rem Next line sets the MP4's file date/time to the same as the original MP4 file.
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.
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 "%%F"

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

:ERROR_OCCURRED
echo.
echo An error occurred when trying to process %filename%
echo Aborting...

:FINISHED
echo.
echo.
pause


The test set of files was three .MTS files, two of which have spaces in their filenames:

Convert_MTS_to_MP4.bat "new test?.mts" test4.mts
Converting    "new test1.mts"    to    "C:\Filename_test\converted\new test1.mp4"
09/07/2017  08:43 PM        60,840,130 new test1.mp4

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

Converting    "test4.mts"    to    "C:\Filename_test\converted\test4.mp4"
09/07/2017  08:43 PM        60,840,130 test4.mp4



Press any key to continue . . .


Dan.

dosdan

#13
Quote from: sark on April 11, 2019, 10:07:27 AM
create an opposite version of the script (mp4 to m2ts) for creating HD DVD/BD output.

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.

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.

Dan.

dosdan

#14
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
@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:
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.