News:

--

Main Menu

Streamline audio encoding?

Started by stuffy1, February 17, 2020, 12:49:02 AM

Previous topic - Next topic

stuffy1

Hi all. I re-encode the audio on a LOT of video files. I'm wondering if there's a way to quicken the process beyond opening the video with Avidemux, waiting for it to load, selecting the right audio options, and saving it. Something along the lines of adding a command to the Windows context menu, or dragging the video file on to a shortcut. Specifically, I need to convert the audio to AAC 384kbps and save the file as an mkv. I'm not doing this to a large batch of files, I need to do one or two files a day. My knowledge of running scripts is zero so I ask forgiveness in advance. Any help you could give me to quicken this process would be much appreciated.

dosdan

#1
Quote from: stuffy1 on February 17, 2020, 12:49:02 AM
Hi all. I re-encode the audio on a LOT of video files. I'm wondering if there's a way to quicken the process beyond opening the video with Avidemux, waiting for it to load, selecting the right audio options, and saving it. Something along the lines of adding a command to the Windows context menu, or dragging the video file on to a shortcut. Specifically, I need to convert the audio to AAC 384kbps and save the file as an mkv. I'm not doing this to a large batch of files, I need to do one or two files a day. My knowledge of running scripts is zero so I ask forgiveness in advance. Any help you could give me to quicken this process would be much appreciated.

This is probably easier to achieve in a batch file running FFMPEG, upon whose batchfile icon you drag&drop one or more video files in which you want to convert the audio stream and repackage as MKVs.

What audio stream formats and container formats are the source videos in?  Can you provide a MediaInfo readout ((in text format i.e. View | Text) of a source file?   

For example, here is such a MerdiaInfo readout from a .MTS file from a camcorder, wrapped in this site's Code formatting tags:

General
ID                                       : 0 (0x0)
Complete name                            : D:\20161130 - OIC Christmas Carols\Video\00007.MTS
Format                                   : BDAV
Format/Info                              : Blu-ray Video
File size                                : 2.71 GiB
Duration                                 : 15 min 34 s
Overall bit rate mode                    : Variable
Overall bit rate                         : 24.9 Mb/s
Maximum Overall bit rate                 : 28.0 Mb/s
Recorded date                            : 2016-11-30 18:52:05+10:00
Writing application                      : Panasonic

Video
ID                                       : 4113 (0x1011)
Menu ID                                  : 1 (0x1)
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4.2
Format settings                          : CABAC / 2 Ref Frames
Format settings, CABAC                   : Yes
Format settings, RefFrames               : 2 frames
Codec ID                                 : 27
Duration                                 : 15 min 34 s
Bit rate mode                            : Variable
Bit rate                                 : 23.7 Mb/s
Maximum bit rate                         : 25.0 Mb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate                               : 59.940 (60000/1001) FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.190
Stream size                              : 2.58 GiB (95%)

Audio
ID                                       : 4352 (0x1100)
Menu ID                                  : 1 (0x1)
Format                                   : AC-3
Format/Info                              : Audio Coding 3
Commercial name                          : Dolby Digital
Codec ID                                 : 129
Duration                                 : 15 min 34 s
Bit rate mode                            : Constant
Bit rate                                 : 256 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 31.250 FPS (1536 SPF)
Bit depth                                : 16 bits
Compression mode                         : Lossy
Stream size                              : 28.5 MiB (1%)
Service kind                             : Complete Main

Text
ID                                       : 4608 (0x1200)
Menu ID                                  : 1 (0x1)
Format                                   : PGS
Codec ID                                 : 144
Duration                                 : 15 min 33 s





How many audio channels will there be for the destination audio: 2ch or 5.1ch?  384kps seems to imply it will be 5.1ch, as 384kbps for 2ch would be wasteful.


stuffy1

90% of the time they're E-AC3 audio in .mkv containers. I'm not sure what you used to get that text (FFMPEG?) but here's what I got using file -> information from Avidemux for one example:

=====================================================
Video
=====================================================
Codec 4CC: AVC1
Image Size: 1920 x 804
Aspect Ratio: 1:1 (1:1)
Frame Rate: 23.976 fps
Total Duration: 00:48:42.086

=====================================================
Extra Video Properties
=====================================================
ExtraDataSize: 46
Extra data: 01 64 00 29 FF E1 00 1D 67 64

=====================================================
Audio
=====================================================
Codec: E-AC3
Channels: 6
Bitrate: 96000 Bps / 768 kbps
Frequency: 48000 Hz
Total Duration: 00:48:42.163


^That's pretty typical. Going for 5.1 channels. Occasionally I'll want to up the bitrate to 640kbps. I'm also fine using AC3 audio or an mp4 container, but for some reason those often turn out choppy for me with Avidemux.

eumagga0x2a

Quote from: stuffy1 on February 17, 2020, 06:42:05 AM
I'm also fine using AC3 audio or an mp4 container, but for some reason those often turn out choppy for me with Avidemux.

Did you try with the current Avidemux builds? Assuming you use Windows:

https://avidemux.org/nightly/win64/ (cross-compiled with MinGW on Linux)
https://avidemux.org/nightly/vsWin64/ (natively built with Microsoft Visual Studio on Windows)

There was a bug with mutlichannel audio in old versions, fixed long time ago. If this one is a different issue which persists in current builds, please provide a sample and steps to reproduce.

dosdan

Quote from: stuffy1 on February 17, 2020, 06:42:05 AM
I'm not sure what you used to get that text

MediaInfo. This program is updated often and is fairly good at reporting the internal format of a large variety of media files.

https://mediaarea.net/en/MediaInfo/Download

After installing it, the first time you need to start the program manually.  If you then click the "Explorer extension" box, it will analyse a media file any time you r.click on the file.

If you build FFMPEG for Windows (https://github.com/m-ab-s/media-autobuild_suite) it will also offer to create mediainfo.exe as a separate CLI utility.  But I prefer to use the version from MediaArea since I can r.click to use it.

Dan.

dosdan

#5
OK, try this.

1. Get a 64-bit static windows build of FFMPEG: https://ffmpeg.zeranoe.com/builds/

2. Copy ffpmeg.exe to your current directory, or preferably, to a directory in your PATH list.

I mainly use MP4 files from my camcorder with 2 AAC audio channels, but I found an old 5.1ch .MTS file:

General
ID                                       : 0 (0x0)
Complete name                            : D:\Dropbox\Test Directory\Dancing.MTS
Format                                   : BDAV
Format/Info                              : Blu-ray Video
File size                                : 561 MiB
Duration                                 : 3 min 8 s
Overall bit rate mode                    : Variable
Overall bit rate                         : 25.0 Mb/s
Maximum Overall bit rate                 : 28.0 Mb/s
Recorded date                            : 2018-01-20 20:12:27+10:00
Writing application                      : Panasonic

Video
ID                                       : 4113 (0x1011)
Menu ID                                  : 1 (0x1)
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4.2
Format settings                          : CABAC / 2 Ref Frames
Format settings, CABAC                   : Yes
Format settings, RefFrames               : 2 frames
Codec ID                                 : 27
Duration                                 : 3 min 7 s
Bit rate mode                            : Variable
Bit rate                                 : 23.6 Mb/s
Maximum bit rate                         : 25.0 Mb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate                               : 50.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.228
Stream size                              : 529 MiB (94%)

Audio
ID                                       : 4352 (0x1100)
Menu ID                                  : 1 (0x1)
Format                                   : AC-3
Format/Info                              : Audio Coding 3
Commercial name                          : Dolby Digital
Codec ID                                 : 129
Duration                                 : 3 min 7 s
Bit rate mode                            : Constant
Bit rate                                 : 384 kb/s
Channel(s)                               : 6 channels
Channel layout                           : L R C LFE Ls Rs
Sampling rate                            : 48.0 kHz
Frame rate                               : 31.250 FPS (1536 SPF)
Bit depth                                : 16 bits
Compression mode                         : Lossy
Stream size                              : 8.59 MiB (2%)
Service kind                             : Complete Main

Text
ID                                       : 4608 (0x1200)
Menu ID                                  : 1 (0x1)
Format                                   : PGS
Codec ID                                 : 144
Duration                                 : 3 min 7 s




In Windows explorer change to the test directory. To get to the console command line for this directory, in the Explorer address box type cmd and press enter.

This is the command line I used:

ffmpeg -i Dancing.MTS -c:v copy -c:a  aac -b:a 384k -map 0:v -map 0:a -y dancing.mkv

Where:

ffmpeg is ffmpeg.exe
-i Dancing.MTS  specifies the input filename.
-c:v copy means for the destination video codec, just copy the video stream without conversion. (fast and no further quality loss).
-c:a aac means for the destination audio codec, use AAC. (The input audio stream format in this example was already AAC, so it's needlessly re-compressed, but it could have been just about anything e.g. AC-3.)
-b:a 384k  is the bitrate of the output audio stream.
-map 0:v means all video streams from the first (counting from zero) input file, will be processed i.e. copied.
-map 0:a means all audio streams from the first input file will be processed i.e. converted to AAC.
Since there are 2 map options used here in this order, the Video is Stream 1 & the Audio will be Stream 2.
For more on map see https://stackoverflow.com/questions/37820083/ffmpeg-not-copying-all-audio-streams
I didn't specify what to do with the subtitle stream, so it will not be included in the output file.
-y means that if the output file exists it will be overwritten without querying.
dancing.mkv specifies the output filename and the container format.

General
Unique ID                                : 50584653839937050143033642322054326546 (0x260E3FFD186DFE2004C17C575C93A912)
Complete name                            : D:\Dropbox\Test Directory\dancing.mkv
Format                                   : Matroska
Format version                           : Version 4
File size                                : 534 MiB
Duration                                 : 3 min 7 s
Overall bit rate mode                    : Variable
Overall bit rate                         : 23.9 Mb/s
Writing application                      : Lavf58.38.100
Writing library                          : Lavf58.38.100
ErrorDetectionType                       : Per level 1

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4.2
Format settings                          : CABAC / 2 Ref Frames
Format settings, CABAC                   : Yes
Format settings, RefFrames               : 2 frames
Codec ID                                 : V_MPEG4/ISO/AVC
Duration                                 : 3 min 7 s
Bit rate mode                            : Variable
Maximum bit rate                         : 25.0 Mb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 50.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Default                                  : Yes
Forced                                   : No

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : A_AAC-2
Duration                                 : 3 min 7 s
Channel(s)                               : 6 channels
Channel layout                           : L R C Cb Rls Rb
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Delay relative to video                  : -21 ms
Writing library                          : Lavc58.68.102 aac
Default                                  : Yes
Forced                                   : No



For a batchfile structure to run this conveniently, see https://avidemux.org/smif/index.php/topic,18673.msg86235.html#msg86235 as a starting point.

Dan.









stuffy1

Quote from: dosdan on February 17, 2020, 09:42:28 AMOK, try this.

I got the script to run properly but the resulting file seems to have messed up audio in some way, like there's something wrong with the channels. Note that I left it as converting to MP4 instead of MKV, and I used a test video with DTS audio. I know 384 AAC is going to sound worse than 1509 DTS but this sounds WAY worse.

I tried to insert the MediaInfo results but my post kept getting marked as spam.

dosdan

#7
Your screen example appeared to have E-AC3 audio, not DTS. I haven't got any files with DTS audio. Can you upload a short version of such a media clip to a file-sharing service and then post the link here?

Alternatively, see if you can extract, convert and then play the audio stream only using:

ffmpeg -i input_file -vn  -c:a aac -ar 48000 -ab 384k -ac 6 6ch.m4a

Here's what I got when I tried it on my Dancing.MTS source file:

General
Complete name                            : D:\6ch.m4a
Format                                   : MPEG-4
Format profile                           : Apple audio with iTunes info
Codec ID                                 : M4A  (M4A /isom/iso2)
File size                                : 8.64 MiB
Duration                                 : 3 min 7 s
Overall bit rate mode                    : Constant
Overall bit rate                         : 386 kb/s
Writing application                      : Lavf58.38.100

Audio
ID                                       : 1
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 3 min 7 s
Bit rate mode                            : Constant
Bit rate                                 : 384 kb/s
Channel(s)                               : 6 channels
Channel layout                           : C L R Ls Rs LFE
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 8.61 MiB (100%)
Default                                  : Yes
Alternate group                          : 1



I played this .m4a file successfully in the AIMP player.

Dan.

stuffy1

#8
I successfully created the .m4a but again the audio sounded bad, like some channels were missing. Here's a sample of an MKV with DTS audio:

https://www74.zippyshare.com/v/S6J5Rdge/file.html

And here's that sample converted to AAC audio using the bat file:
https://www27.zippyshare.com/v/YFTReu8J/file.html

I just tried the conversion from E-AC3 to AAC and had the same result, so the source audio format doesn't seem to be the problem. Note that doing these with Avidemux and creating what should be identical files results in a video with good audio.

Here's the code I'm using for videos:

@echo off

cls
set filename=""

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

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


And here's the important part from MediaInfo:
Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 2 h 11 min
Bit rate mode                            : Constant
Bit rate                                 : 384 kb/s
Channel(s)                               : 6 channels
Channel layout                           : L R C Cb Lb Rb
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 362 MiB (5%)
Language                                 : Korean
Default                                  : Yes
Alternate group                          : 1
Encoded date                             : UTC 2020-02-15 10:54:17
Tagged date                              : UTC 2020-02-15 10:54:17

dosdan

#9
Stuffy1,  I has no problems with either of your files.

1. Try to see if you can play the audio I just extracted from your file, using the cmd line in my 3rd msg, in a media player like AIMP (I only have 2ch playback capability on my PC, so I can't hear inappropriate inter-channel switching/dropouts):  https://dl.dropbox.com/s/66c4io8sklzmmd9/6ch.m4a
2. See if you can play the converted MKV which I created using the cmd line in my 2nd msg: https://dl.dropbox.com/s/1e4l1w8mmb417lh/sample_converted.mkv
3. Here is a win64 static version of FFMPEG I built recently. See if it works better than what you used: https://dl.dropbox.com/s/f5yvjc87gasj3bk/ffmpeg.exe

Note: the converted MKV I created plays fine in both ADM and MPC-BE.

Dan.

stuffy1

Quote from: dosdan on February 19, 2020, 09:48:04 AM...

First, thanks for all your help with this.

I'm surprised those files sound the same for you. I played them on both my laptop and my TV and there's a clear difference, with the original sounding far superior.

Your samples each sounded different to me. The audio clip was perfect with sounds split to different channels, while the video file sounded messed up like mine. I played them both in VLC and also played the audio in Winamp.

The version of FFMPEG you provided yielded no difference.

dosdan

#11
Quote from: stuffy1 on February 22, 2020, 09:51:53 AM
Your samples each sounded different to me. The audio clip was perfect with sounds split to different channels, while the video file sounded messed up like mine.

This will be a 6ch thing, which is why I'm not hearing it.  Look at the channel layout in your original sample.mkv with its DTS audio codec:

C L R Ls Rs LFE

And in the extracted and converted 6ch.m4a

C L R Ls Rs LFE

But now look at sample_converted.mkv

L R C Cb Lb Rb

As you can see, the layout is wrong.

I can't test this as I don't have 5.1ch playback. But if the MediaInfo channel layouts are correct, (I'm not sure if this is the case), then this command might be needed for converting clips with DTS audio:

ffmpeg -i sample.mkv -c:v copy -c:a  aac -b:a 384k -af "channelmap=1|2|0|5|3|4:5.1" sample_converted_120534.mkv

Where:
-af channelmap is an audio filter
"channelmap=1|2|0|5|3|4:5.1" means assign the following as 5.1 format (FFMPEG had incorrectly guessed it was 5.1(side) format) in the following order, counting indices from zero):

Input   1(L)     2(R)     0(C)       5(LFE, not CB)  3(LS, not LB)  4(RS, not RB)
Output  0(L)     1(R)     2(C)       3(LFE)          4(LS)          5(RS)


If you've got a mixture of DTS and non-DTS imput files, it should be possible to detect DTS audio using FFPROBE and then execute 1 of 2 different FFMPEG command lines depending on the outcome of thsi test. For example, the following cmd line will only only clear the screen if the input file has DTS audio in its audio stream:

ffprobe -v error -select_streams a -show_entries stream=codec_name -of csv=p=0 sample.mkv | find "dts" && cls


You can test this by first probing a file with DTS audio. Once this cmd line has executed and the screen has cleared, press UpArrow to bring back your last cmd, change the search string in FIND to "dtss" and retry. This time the screen won't clear.

If you're expecting both 2ch DTS and 6ch DTS, the cmd would need to be a little more complex.

The issue with DTS audio and FFMPEG is common. For some further reading see:

https://forum.videohelp.com/threads/377474-How-to-see-a-5-1-file-s-channel-layout-Channel-layout-error-with-ffmpeg
https://video.stackexchange.com/questions/23140/transcode-5-1-audio-in-ffmpeg-leads-to-incorrect-audio-mapping-in-output (read the answer explaining why this problem occurs)
https://trac.ffmpeg.org/wiki/AudioChannelManipulation
https://mediaarea.net/AudioChannelLayout

Dan.




stuffy1

Actually the problem was not limited to DTS, and unfortunately this has not fixed the error, though I can see that it had an effect. Here are the audio channels of the conversion using the original bat file:

Channel layout                           : L R C Cb Lb Rb

And here they are for the new one:

Channel layout                           : C L R Ls Rs LFE

This new one does match what MediaInfo says is the proper channel layout. You seemed skeptical so maybe it's not correct and a different order is needed.

dosdan

#13
Quote from: stuffy1 on February 23, 2020, 03:10:37 AM
This new one does match what MediaInfo says is the proper channel layout. You seemed skeptical so maybe it's not correct and a different order is needed.

Yes, I'm skeptical. I think it will require careful listening, maybe muting one or more channels to work out which corresponds to which.

For example I think the LFE channel content should not have much HF. Now listening to it though the LFE output channel of your surround-sound receiver may not be indicative of this because your receiver might apply a strong LP input filter. So I'd temporarily try it though say the Centre channel of your receiver to insure that this channel, which you hope is LFE, actually is just LF stuff.

I believe, as well as DTS, this may also apply to multi-channel surround sound encoded as WAV/PCM, then later converted with FFMPEG to multi-channel AAC. But I can't imaging why anyone would want to distribute 5.1ch stuff encoded as PCM?

Dan.

dosdan

BTW, I wouldn't be surprised if both Handbrake and VidCoder had a similar issue with this clip.  Have you tried either of them?

Dan.