News:

--

Main Menu

Batch copy .mp4 to .mkv script?

Started by inadream, March 05, 2022, 10:21:59 PM

Previous topic - Next topic

inadream

I've been searching and trying a few scripts to convert a folder of .MP4 files to .MKV by copy/copy with no re-encoding. I don't understand how to write a batch file myself that will successfully execute that task and the few I've tried haven't worked. I unfortunately don't understand how to write a batch script file myself -
would anyone be willing to share the script for a batch file that should achieve achieve this task?
Any help would be hugely appreciated.

dosdan

#1
This batchfile is designed to convert all the MP4s (not other filetypes) inside a folder into MKVs using copying of both video and audio streams. You don't run it from the command line or tag specific files inside a directory. The way it is designed to be used is to create a shortcut to the batchfile and place the shortcut on your desktop. Then you drag the directory_name_to_be_processed icon onto this shortcut icon. The batchfile ends with a PAUSE so you can see the result before the window closes.

If one or more errors occurs it will continue on until the end, then it will also open a new window, in the program that's associated with .TXT files (Notepad, in my case), showing any error messages. Otherwise, with a large group of files, any error messages can scroll off the screen and you may miss them.

Convert_MP4_to_MKV_(folder).bat
@echo off
setlocal EnableDelayedExpansion
cls

if exist conversion_error.txt del /q conversion_error.txt

:FILES_LOOP
for %%F  in ("%~1\*.mkv") do (
    echo Converting    "%%~F"    to    "%%~dpFconverted\%%~nF.mp4"
if not exist "%%~dpFconverted\" md "%%~dpFconverted\"
ffmpeg.exe -i "%%~fF" -c copy -y -hide_banner -loglevel error -map 0 "%%~dpFconverted\%%~nF.mp4"
if errorlevel 1 echo error occurred processing: "%%~F"  >> conversion_error.txt
rem Next line shows the MP4 after it has been created, so with multiple MP4s it acts as a sort of progress indicator.
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
if exist conversion_error.txt start /i conversion_error.txt
echo.
echo.
pause

Where some conversion batchfiles get into problems are with spaces in filenames and with characters like apostrophes in these names. So I tested it with a directory name containing a space and with 3 filenames. Contents of:

C:\test directory\MP4 Test
01.mp4
0 2.mp4
0 (3)'s.mp4

Note that 01.mp4 is not a video clip - it's a renamed textfile used here to create an error when FFMPEG tries to convert it.

The batchfile creates a subdirectory off the dropped directory called "converted". It is not designed to process any subdirectories below the initial directory.

The screen output when processing this directory:

Converting    "C:\test directory\MP4 Test\01.mp4"    to    "C:\test directory\MP4 Test\converted\01.mkv"
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001973c1f9e40] moov atom not found
C:\test directory\MP4 Test\01.mp4: Invalid data found when processing input

Converting    "C:\test directory\MP4 Test\0 2.mp4"    to    "C:\test directory\MP4 Test\converted\0 2.mkv"
06/03/2022  11:54 AM        65,381,990 0 2.mkv

Converting    "C:\test directory\MP4 Test\0 (3)'s.mp4"    to    "C:\test directory\MP4 Test\converted\0 (3)'s.mkv"
06/03/2022  11:54 AM        65,381,990 0 (3)'s.mkv

Press any key to continue . . .

And this is the contents of the Notepad window:
error occurred: "C:\test directory\MP4 Test\01.mp4"

Either ffmpeg.exe needs to be in a directory mentioned in your Windows environmental variable PATH list, or you have to fully specify the pathname to it in your batchfile. Tip: in Windows File Explorer, if you hold down your Shift key when you r.click on a file, you'll see an extra command "Copy as path" which copies the full pathname into your clipboard and you can then paste it into your editor e.g.:

"D:\FFMPEG versions\ffmpeg-N-104013-g2ee4077248-win64-gpl\bin\ffmpeg.exe"


Dan.

inadream

Quote from: dosdan on March 06, 2022, 01:59:37 AMThis batchfile is designed to convert all the MP4s (not other filetypes) inside a folder into MKVs using copying of both video and audio streams. You don't run it from the command line or tag specific files inside a directory. The way it is designed to be used is to create a shortcut to the batchfile and place the shortcut on your desktop. Then you drag the directory_name_to_be_processed icon onto this shortcut icon. The batchfile ends with a PAUSE so you can see the result before the window closes.

If one or more errors occurs it will continue on until the end, then it will also open a new window, in the program that's associated with .TXT files (Notepad, in my case), showing any error messages. Otherwise, with a large group of files, any error messages can scroll off the screen and you may miss them.

Convert_MP4_to_MKV_(folder).bat
@echo off
setlocal EnableDelayedExpansion
cls

if exist conversion_error.txt del /q conversion_error.txt

:FILES_LOOP
for %%F  in ("%~1\*.mkv") do (
    echo Converting    "%%~F"    to    "%%~dpFconverted\%%~nF.mp4"
    if not exist "%%~dpFconverted\" md "%%~dpFconverted\"
    ffmpeg.exe -i "%%~fF" -c copy -y -hide_banner -loglevel error -map 0 "%%~dpFconverted\%%~nF.mp4"
    if errorlevel 1 echo error occurred processing: "%%~F"  >> conversion_error.txt
    rem Next line shows the MP4 after it has been created, so with multiple MP4s it acts as a sort of progress indicator.
    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
if exist conversion_error.txt start /i conversion_error.txt
echo.
echo.
pause

Where some conversion batchfiles get into problems are with spaces in filenames and with characters like apostrophes in these names. So I tested it with a directory name containing a space and with 3 filenames. Contents of:

C:\test directory\MP4 Test
01.mp4
0 2.mp4
0 (3)'s.mp4

Note that 01.mp4 is not a video clip - it's a renamed textfile used here to create an error when FFMPEG tries to convert it.

The batchfile creates a subdirectory off the dropped directory called "converted". It is not designed to process any subdirectories below the initial directory.

The screen output when processing this directory:

Converting    "C:\test directory\MP4 Test\01.mp4"    to    "C:\test directory\MP4 Test\converted\01.mkv"
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001973c1f9e40] moov atom not found
C:\test directory\MP4 Test\01.mp4: Invalid data found when processing input

Converting    "C:\test directory\MP4 Test\0 2.mp4"    to    "C:\test directory\MP4 Test\converted\0 2.mkv"
06/03/2022  11:54 AM        65,381,990 0 2.mkv

Converting    "C:\test directory\MP4 Test\0 (3)'s.mp4"    to    "C:\test directory\MP4 Test\converted\0 (3)'s.mkv"
06/03/2022  11:54 AM        65,381,990 0 (3)'s.mkv

Press any key to continue . . .

And this is the contents of the Notepad window:
error occurred: "C:\test directory\MP4 Test\01.mp4"

Either ffmpeg.exe needs to be in a directory mentioned in your Windows environmental variable PATH list, or you have to fully specify the pathname to it in your batchfile. Tip: in Windows File Explorer, if you hold down your Shift key when you r.click on a file, you'll see an extra command "Copy as path" which copies the full pathname into your clipboard and you can then paste it into your editor e.g.:

"D:\FFMPEG versions\ffmpeg-N-104013-g2ee4077248-win64-gpl\bin\ffmpeg.exe"


Dan.


Thank you so much for your help - your bat code converted all MKV files to FLV files, but I figured out how to correct that and where to replace 'ffmpe.exe' in the bat code with the ffmpeg.exe location on my pc so that the correct bat code turned out as:


@echo off
setlocal EnableDelayedExpansion
cls

if exist conversion_error.txt del /q conversion_error.txt

:FILES_LOOP
for %%F  in ("%~1\*.flv") do (
    echo Converting    "%%~F"    to    "%%~dpFconverted\%%~nF.mkv"
    if not exist "%%~dpFconverted\" md "%%~dpFconverted\"
    "C:\Program Files (x86)\SVP 4\utils\ffmpeg.exe" -i "%%~fF" -c copy -y -hide_banner -loglevel error -map 0 "%%~dpFconverted\%%~nF.mkv"
    if errorlevel 1 echo error occurred processing: "%%~F"  >> conversion_error.txt
    rem Next line shows the mkv after it has been created, so with multiple mkvs it acts as a sort of progress indicator.
    if exist "%%~dpFconverted\%%~nF.mkv" dir "%%~dpFconverted\%%~nF.mkv" | 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
if exist conversion_error.txt start /i conversion_error.txt
echo.
echo.
pause

I am now wondering is there a way to convert ALL non-mkv files in a folder to mkv using batch code? Is it a simple alteration to my bat above?

dosdan

#3
Quote from: inadream on January 08, 2024, 01:05:22 PMI am now wondering is there a way to convert ALL non-mkv files in a folder to mkv using batch code? Is it a simple alteration to my bat above?

I presume you are only working with a few different filetypes. If so, just specifically list each of the types you wish to convert in the filenameset inside the "(...)".

In the following example, the test folder I drop on to the batchfile icon contains .MKV, .MP4 and .TS media files. To only convert the .MP4 and .TS containers, but not the .MKVs, into MKV containers, change the line containing:

for %%F in ("%~1\*.mp4") do (
to

for %%F in ("%~1\*.mp4" "%~1\*.ts") do (
Here, I have mentioned two filetypes in the filenameset, but you could add more, if needed. 

Also, it does not matter if some of listed filetypes_to_be_converted do not exist in a folder - it won't produce an error message.

inadream

Quote from: dosdan on January 11, 2024, 11:18:15 AM
Quote from: inadream on January 08, 2024, 01:05:22 PMI am now wondering is there a way to convert ALL non-mkv files in a folder to mkv using batch code? Is it a simple alteration to my bat above?

I presume you are only working with a few different filetypes. If so, just specifically list each of the types you wish to convert in the filenameset inside the "(...)".

In the following example, the test folder I drop on to the batchfile icon contains .MKV, .MP4 and .TS media files. To only convert the .MP4 and .TS containers, but not the .MKVs, into MKV containers, change the line containing:

for %%F in ("%~1\*.mp4") do (
to

for %%F in ("%~1\*.mp4" "%~1\*.ts") do (
Here, I have mentioned two filetypes in the filenameset, but you could add more, if needed. 

Also, it does not matter if some of listed filetypes_to_be_converted do not exist in a folder - it won't produce an error message.


Thanks again, sorry I didn't see your last reply until just now! I implemented your advice in order to convert multiple file types to MKV at once and it did indeed work for all types EXCEPT .mpg files which gives the following error:

QuoteConverting    "I:\TEST\40 The Big Red Thing.mpg"    to    "I:\TEST\converted\40 The Big Red Thing.mkv"
[matroska @ 06c6ea00] Only audio, video, and subtitles are supported for Matroska.
[out#0/matroska @ 08d34780] Could not write header (incorrect codec parameters ?): Invalid argument
[aost#0:2/copy @ 08b3db80] Error initializing output stream:

I realise more specific file info for the .mpg files might be needed, but I was wondering if you might recognize this error at all?