Author Topic: Batch Encoding  (Read 341 times)

muzzy001

  • Newbie
  • *
  • Posts: 4
Batch Encoding
« on: July 11, 2019, 08:51:02 AM »
Hi there,
I am new here but i have been using avidemux for a while now and i find it to be a great program.
The only thing  i cannot figure out is how i can make it batch encode multiple files saving video and extension as is and only changing audio to AAC FDK

Could someone please show me how to edit this so it can go from episode 1 to episode 2 and so on  to complete 15 episodes in that folder automatically as an example

#PY  <- Needed to identify #
#--automatically built--

adm = Avidemux()
adm.loadVideo("C:/Users/Coolermaster/Desktop/shows/episode1.mkv")
adm.clearSegments()
adm.addSegment(0, 0, 456456416)
adm.markerA = 0
adm.markerB = 456456416
adm.setPostProc(3, 3, 0)
adm.videoCodec("Copy")
adm.audioClearTracks()
adm.setSourceTrackLanguage(0,"eng")
adm.audioAddTrack(0)
adm.audioCodec(0, "FDK_AAC");
adm.audioSetDrc(0, 0)
adm.audioSetShift(0, 0, 0)
adm.setContainer("MKV", "forceDisplayWidth=False", "displayWidth=1280", "displayAspectRatio=0")

Any help with this would be greatly appreciated

Thanking you in advance

eumagga0x2a

  • Hero Member
  • *****
  • Posts: 3015
Re: Batch Encoding
« Reply #1 on: July 11, 2019, 06:54:30 PM »
Regarding Windows batch scripting solutions, please refer to posts by dosdan, he contributed a lot of sophisticated solutions.

Regarding the Avidemux part, which version are you using? Likely not 2.5.x (it would be unsupported anyway). It should be the latest nightly or at least the latest release.

Before you re-encode audio, you should verify that it is not already AAC (there is no such thing as "AAC FDK", FDK is an encoder implementation for AAC).

muzzy001

  • Newbie
  • *
  • Posts: 4
Re: Batch Encoding
« Reply #2 on: July 11, 2019, 09:29:42 PM »
I am using the latest stable version and I usually check the audio with mediainfo in Windows 7 x64 SP1. I know what you mean by fdk but I was referring to the setting under audio which says AAC (FDK)

Could you please help me amend this script so I can batch encode any folder with video files in it as video copy and audio aac

dosdan

  • Full Member
  • ***
  • Posts: 138
Re: Batch Encoding
« Reply #3 on: July 12, 2019, 02:41:50 PM »
I am using the latest stable version and I usually check the audio with mediainfo in Windows 7 x64 SP1. I know what you mean by fdk but I was referring to the setting under audio which says AAC (FDK)

Could you please help me amend this script so I can batch encode any folder with video files in it as video copy and audio aac

I wouldn't use ADM for this. Loading ADM multiple times for a simple change is too kludgy.  FFMPEG is better for stuff like this. But standard builds of FFMPEG don't have the FDK_AAC library included for licensing reasons. I've built a 64-bit Windows one that does: https://dl.dropbox.com/s/f5yvjc87gasj3bk/ffmpeg.exe

This is a somewhat cut-down version of FFMPEG, because often builds include lots of libraries for obscure or specialised formats that the general user is unlikely to ever use. You can check what's in a build of FFMEG using the -version switch.

ffmpeg -version
Code: [Select]
ffmpeg version N-94271-g2601eef850 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 9.1.0 (Rev3, Built by MSYS2 project)
configuration:  --disable-autodetect --enable-libfdk-aac --enable-amf --enable-bzlib --enable-cuda --enable-cuvid --enable-d3d11va --enable-dxva2 --enable-iconv --enable-lzma --enable-nvenc --enable-schannel --enable-zlib --enable-sdl2 --disable-debug --enable-ffnvcodec --enable-nvdec --enable-gmp --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libx264 --enable-libdav1d --extra-libs=-liconv --enable-gpl --enable-version3 --enable-nonfree
libavutil      56. 30.100 / 56. 30.100
libavcodec     58. 53.101 / 58. 53.101
libavformat    58. 28.101 / 58. 28.101
libavdevice    58.  7.100 / 58.  7.100
libavfilter     7. 56.101 /  7. 56.101
libswscale      5.  4.101 /  5.  4.101
libswresample   3.  4.100 /  3.  4.100
libpostproc    55.  4.100 / 55.  4.100

The AAC options for FFMPEG can be found here: https://trac.ffmpeg.org/wiki/Encode/AAC

Here's a batchfile to run it:
Convert_MKV_to_MKV_with_FDK_AAC.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_(FDK_AAC).mkv"
if not exist "%%~dpFconverted\" md "%%~dpFconverted\"
ffmpeg.exe -i "%%~fF" -c:v copy -c:a libfdk_aac -b:a 128k -y -hide_banner -loglevel error -map_metadata 0 "%%~dpFconverted\%%~nF_(FDK_AAC).mkv"
if errorlevel 1 echo error occurred: "%%~F"  >> conversion_error.txt
rem Next line sets the MKV's file date/time to the same as the original MKV file.
if exist "%%~dpFconverted\%%~nF_(FDK_AAC).mkv" touch -r "%%~fF" "%%~dpFconverted\%%~nF_(FDK_AAC).mkv"
rem Next line shows the new MKV after it has been created, so with multiple MKVs it acts as a sort of progress indicator
if exist "%%~dpFconverted\%%~nF_(FDK_AAC).mkv" dir "%%~dpFconverted\%%~nF_(FDK_AAC).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

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

Here's a link to download this batchfile:  https://dl.dropbox.com/s/byjhlx0nkv6qqkx/convert_mkv_to_mkv_with_fdk_aac.bat

The conversion test set is 5 .mkv files. 1,2,3 & 5 are copies of the same video file, while 4 is a renamed text file, which I use to check error-trapping.

A test run. From the command prompt you can use:

Convert_MKV_to_MKV_with_FDK_AAC.bat "test*.mkv"   for all 5 mkv files
Convert_MKV_to_MKV_with_FDK_AAC.bat "test 1.mkv"  for just the 1st mkv file.

You can also create a shortcut to this batchfile, place that shortcut icon on your Windows desktop and then select all 5 mkv files in their folder and drag-and-drop the selected group onto the icon. (Note: this needs to be a selection of files, not the name of a directory/folder containing just these 5 files.) That's why there's a PAUSE command in the last line of the batchfile - otherwise the window closes as soon as it is finished.  Running it this way is equivalent to using a command line of:

Convert_MKV_to_MKV_with_FDK_AAC.bat "C:\Filename_test\Test 1.mkv" "C:\Filename_test\Test 2.mkv" "C:\Filename_test\Test 3.mkv" "C:\Filename_test\Test 4.mkv" "C:\Filename_test\Test 5.mkv"

Here's what I see when I do a run to convert all 5 files:
Code: [Select]
Converting    "C:\Filename_test\Test 1.mkv"   to    "C:\Filename_test\converted\Test 1_(FDK_AAC).mkv"
[matroska,webm @ 00000252921c75c0] Element at 0xd6b9d1 ending at 0xd6bd74 exceeds containing master element ending at 0xd6b9c5
08/04/2019  05:27 PM        13,948,229 Test 1_(FDK_AAC).mkv

Converting    "C:\Filename_test\Test 2.mkv"   to    "C:\Filename_test\converted\Test 2_(FDK_AAC).mkv"
[matroska,webm @ 00000256d4f275c0] Element at 0xd6b9d1 ending at 0xd6bd74 exceeds containing master element ending at 0xd6b9c5
08/04/2019  05:27 PM        13,948,229 Test 2_(FDK_AAC).mkv

Converting    "C:\Filename_test\Test 3.mkv"   to    "C:\Filename_test\converted\Test 3_(FDK_AAC).mkv"
[matroska,webm @ 0000024560cd75c0] Element at 0xd6b9d1 ending at 0xd6bd74 exceeds containing master element ending at 0xd6b9c5
08/04/2019  05:27 PM        13,948,229 Test 3_(FDK_AAC).mkv

Converting    "C:\Filename_test\Test 4.mkv"   to    "C:\Filename_test\converted\Test 4_(FDK_AAC).mkv"
Truncating packet of size 12911 to 56
[matroska,webm @ 00000140847475c0] EBML header parsing failed
C:\Filename_test\Test 4.mkv: Invalid data found when processing input

Converting    "C:\Filename_test\Test 5.mkv"   to    "C:\Filename_test\converted\Test 5_(FDK_AAC).mkv"
[matroska,webm @ 0000019fb4bc75c0] Element at 0xd6b9d1 ending at 0xd6bd74 exceeds containing master element ending at 0xd6b9c5
08/04/2019  05:27 PM        13,948,229 Test 5_(FDK_AAC).mkv



Press any key to continue . . .

The 4 video files are converted successfully. Errors are reported, but the files play correctly with sound in MPC-BE, and the reported errors don't set an errorlevel of 1 or higher.

However, the invalid video file does set this errorlevel. I've designed this batchfile not to stop when this happens. Instead, it writes the names(s) of any problem file(s) in conversion_error.txt, and when the conversion run is finished, if this file exists it is opened in a new window in Notepad (or whatever program you have associated with .txt files).  Here's what it contained:

error occurred: "Test 4.mkv"

Touch.exe is used to give the converted files (in the "converted" sub-directory) the same date/timestamp as the original files. If you don't mind the converted files having the date/time of when the conversion occurred, you can omit this line.  Here's a link to touch.exe: https://dl.dropbox.com/s/na4wxp9jflqd4a6/touch.exe

Note: both ffmpeg.exe and touch.exe should be in the current directory, or in a directory which is in your PATH list of directories, or you should specify the full pathname to them in the batchfile e.g:

c:\utils\ffmpeg.exe -i "%%~fF" -c:v copy -c:a libfdk_aac -b:a 128k -y -hide_banner -loglevel error -map_metadata 0 "%%~dpFconverted\%%~nF_(FDK_AAC).mkv"

Dan.



« Last Edit: July 12, 2019, 10:13:56 PM by dosdan »

muzzy001

  • Newbie
  • *
  • Posts: 4
Re: Batch Encoding
« Reply #4 on: July 13, 2019, 06:29:53 PM »
Thankyou for that I will try it after work tonite. Will let you know how I go. Thankyou once again