VideoHelp Forum
+ Reply to Thread
Page 2 of 3
FirstFirst 1 2 3 LastLast
Results 31 to 60 of 63
Thread
  1. Member hydra3333's Avatar
    Join Date
    Oct 2009
    Location
    Australia
    Search Comp PM
    Thanks for this thread. Great examples of how to script conversions !
    I wish I'd seen this thread before embarking on scripting my workflow.

    Has anyone come across and resolved issues with ffmpeg not handling source files' "audio delay" properly when encoding into different audio/video formats ?

    In my case, the purpose is timeshifted home viewing, like a PVR, except using other viewing mechanisms such as chromecast and media players, which require a common format.
    So, workflow is
    1. OTA capture to .TS for timeshifted viewing (content will be avc or mpeg2)
    2. identify/fix any capture issues and move into either mp4 or .mpg container depending whether avc or mpeg2 (using VideoReDo's QuickStreamFix facility)
    3. level loudness using ffmpeg's loudnorm filter and convert to, say, .mp3
    4. if avc, copy video; if mpeg2, re-encode video as mpeg4 (in an .mp4 container)
    5. mux video into a .mp4 container, for later pleasurable viewing

    Some of the mpeg2 capture have can largeish "audio delays" eg -1728 ms.
    eg a 20mbs sample mpeg4 in a .TS here https://drive.google.com/open?id=0B5RV2aJ2vdhSbnRjaGItSlNVSUE
    Ffmpeg fails to preserve any audio delay and the muxed audio/video is way out of sync.
    Ffmpeg doesn't maintain audio/video sync even if audio/video are both converted in the one commandline.
    Muxing with mp4mux and a specified delay nearly puts it back in sync however the audio still appears to be just a bit ahead of the video.
    (Adding flavour, for some .TS files mediainfo also reports a strange Video_Delay result, eg "-1824-1824" instead of "-1824" )

    eg in a .bat file, minus the quickstreamfix, here's a demo of audio desync.
    The .TS file plays in sync in MPC-HC, however it plays out of sync in both resulting .mp4 files (much moreso on the delay=0 file)
    Code:
    @setlocal ENABLEDELAYEDEXPANSION
    @setlocal enableextensions
    REM 20Mb download at https://drive.google.com/open?id=0B5RV2aJ2vdhSbnRjaGItSlNVSUE
    mediainfo.exe "--Inform=Audio;%%Video_Delay%%" "G:\HDTV\test\sample5.mpeg4.delay.-1728ms.ts" > ".\o.txt"
    set /p AudioDelayms=< ".\o.txt"
    SET lI=-16
    SET lTP=0.0
    SET lLRA=11
    set jsonfile=.\loudnorm_findings.json
    REM --- find loudness parameters 
    ffmpeg.exe -threads 0 -nostats -nostdin -y -hide_banner -threads 0 -i "G:\HDTV\test\sample5.mpeg4.delay.-1728ms.ts" -threads 0 -vn -threads 0 -af loudnorm=I=%lI%:TP=%lTP%:LRA=%lLRA%:print_format=json -threads 0 -f null - 2> "%jsonFile%"
    REM all the windows trickery below is simply to remove quotes and tabs and spaces from the single-level json response
    set input_i=
    set input_tp=
    set input_lra=
    set input_thresh=
    set target_offset=
    for /f "tokens=1,2 delims=:, " %%a in (' find ":" ^< "%jsonFile%" ') do (
       set "var="
       for %%c in (%%~a) do set "var=!var!,%%~c"
       set var=!var:~1!
       set "val="
       for %%d in (%%~b) do set "val=!val!,%%~d"
       set val=!val:~1!
    REM   echo .!var!.=.!val!.
       IF /I "!var!" == "input_i"         set !var!=!val!
       IF /I "!var!" == "input_tp"        set !var!=!val!
       IF /I "!var!" == "input_lra"       set !var!=!val!
       IF /I "!var!" == "input_thresh"    set !var!=!val!
       IF /I "!var!" == "target_offset"   set !var!=!val!
    )
    echo input_i=%input_i%
    echo input_tp=%input_tp%
    echo input_lra=%input_lra%
    echo input_thresh=%input_thresh%
    echo target_offset=%target_offset%
    set loudnormfilter=loudnorm=I=%lI%:TP=%lTP%:LRA=%lLRA%:measured_I=%input_i%:measured_LRA=%input_lra%:measured_TP=%input_tp%:measured_thresh=%input_thresh%:offset=%target_offset%:linear=true:print_format=summary
    echo loudnormfilter=%loudnormfilter% 
    REM loudnormfilter=loudnorm=I=-16:TP=0.0:LRA=11:measured_I=-24.40:measured_LRA=2.50:measured_TP=-10.47:measured_thresh=-34.75:offset=-0.04:linear=true:print_format=summary 
    REM ffmpeg.exe -threads 0 -nostats -threads 0 -i "G:\HDTV\test\sample5.mpeg4.delay.-1728ms.ts" -threads 0 -vn -threads 0 -map_metadata -1 -af loudnorm=I=-16:TP=0.0:LRA=11:measured_I=-24.40:measured_LRA=2.50:measured_TP=-10.47:measured_thresh=-34.75:offset=-0.04:linear=true:print_format=summary -c:a libmp3lame -ab 256k -ar 48000 -threads 0 -y "G:\HDTV\test\sample5.mpeg4.delay.-1728ms-temp.mp3"
    ffmpeg.exe -threads 0 -nostats -threads 0 -i "G:\HDTV\test\sample5.mpeg4.delay.-1728ms.ts" -threads 0 -vn -threads 0 -map_metadata -1 -af %loudnormfilter% -c:a libmp3lame -ab 256k -ar 48000 -threads 0 -y "G:\HDTV\test\sample5.mpeg4.delay.-1728ms-temp.mp3"
    ffmpeg.exe -threads 0 -nostats -threads 0 -i "D:\TEMP\SCRATCH\sample5.mpeg4.delay.-1728ms.mp4" -threads 0 -an -threads 0 -map_metadata -1 -c:v copy -threads 0 -y "D:\TEMP\SCRATCH\sample5.mpeg4.delay.-1728ms-temp.MP4"
    MP4Box.exe -add "D:\TEMP\SCRATCH\sample5.mpeg4.delay.-1728ms-temp.MP4":lang=eng -add "D:\TEMP\SCRATCH\sample5.mpeg4.delay.-1728ms-temp.mp3":lang=eng:delay=000 -isma -new "G:\HDTV\test\Converted\sample5.mpeg4.delay.-1728ms.mp3.nodelay.mp4" 
    MP4Box.exe -add "D:\TEMP\SCRATCH\sample5.mpeg4.delay.-1728ms-temp.MP4":lang=eng -add "D:\TEMP\SCRATCH\sample5.mpeg4.delay.-1728ms-temp.mp3":lang=eng:delay=%AudioDelayms% -isma -new "G:\HDTV\test\Converted\sample5.mpeg4.delay.-1728ms.mp3.%AudioDelayms%.mp4"
    Last edited by hydra3333; 13th Jan 2017 at 21:11.
    Quote Quote  
  2. Eac3to should fix delay while demuxing. Not possible to use? Demux your streams, treat them as you wish, and then mux them together.

    edit: eac3to seams not to load that MBAFF avc sample video, so not good for your type of capture
    Last edited by _Al_; 14th Jan 2017 at 20:25.
    Quote Quote  
  3. ffmpeg can do something similar. Pad beginning with silence (or delete samples if necessary):
    -af aresample=first_pts=0
    Quote Quote  
  4. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    Hello fellow Batchers. Some of you will know me from here and also here.

    I have a scenario that one or more of you may be able to help me with, that I just haven't had a chance to look at yet.

    I download videos (usually in the MP4 format) that get converted to MKV and then get chapters placed in them at 5 minute intervals. In doing this, I end up with 3 files of roughly the same size, though by default, the first MKV gets deleted after a chapterized version gets created ... based purely on a size comparison by code. That still leaves me with an extra copy, that ultimately I don't want, but I keep for a while in case something went wrong with conversion to MKV.

    I only keep the original source (downloaded) file until I check the status of the converted one. In the past, I have done this by checking the converted file on my hardware media player (NeoTV 550). To do so, I use the numbers on my remote (1 to 9) to jump to those sections of the file ... the file being divided up into 10 sections by the player. I play a few seconds of each section and if all is fine (audio + video) ... including at least one sync check, then I am happy to delete the original downloaded source file.

    Now for a while (years), this process has served me well, but at some point I slipped behind, and now here I am many months later and I am way behind, always getting far more than I can watch and keep up with. So the situation begs for something to streamline it ... especially as after filling up drives with originals, I am getting close to needing to expand further until or unless I can reclaim space.

    I have in mind, something like the following.
    1. A program I can drop a converted video file on.
    2. That program will grab chunks of the video at specified intervals, for a number of specified seconds per chunk.
    3. Those chunks will be combined into one video of at most, 30 seconds duration. I call this the checking file.

    All I need do, is watch that checking file, without having to muck around with remote control clicks and remembering to click next when getting distracted by what I am watching.

    I feel fairly certain, that ffmpeg has the ability to do this, but as I have discovered in the past, rarely is anything easy to research in regard to ffmpeg options.

    So any help in this regard would be much appreciated, and I am sure not just to my benefit alone. I could maybe even do away with having to drop on a program file, and just incorporate an option in my Dropper program to create a Checking video file automatically for each download.

    P.S. A pinch and a punch for the first day of the month, and no returns.

    P.S.S. Long gone are the days, where I used to check the downloaded file for errors and then re-download if faulty .... that very rarely happens now, except for maybe a few favorites. Of course, that is what I want to get back to, checking files in time ... to re-download if necessary.
    Last edited by Tombs; 31st Aug 2017 at 22:11.
    Quote Quote  
  5. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Hi

    This will wrap the video in an MKV container and add chapters.
    Code:
    for %%a in (*.mp4) do "C:/Program Files (x86)/MKVToolNix\mkvmerge.exe" --ui-language en "%%a" --output "%%~na.mkv" --generate-chapters interval:00:05:00.000 --track-order 0:0,0:1
    No intermediate file, and quick.

    That program will grab chunks of the video at specified intervals, for a number of specified seconds per chunk.
    Looking to check video/audio sync. A lot of the chunks will not have speech or someone facing the camera.
    I'll have a think about it.

    Cheers.

    Edit:
    If the original mp4 is in sync then so too the mkv.
    Only the container is changed.
    Last edited by pcspeak; 31st Aug 2017 at 23:51. Reason: Re-read Tombs post and realized I'd missed a bit.
    Quote Quote  
  6. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    Originally Posted by pcspeak View Post
    This will wrap the video in an MKV container and add chapters.
    Code:
    for %%a in (*.mp4) do "C:/Program Files (x86)/MKVToolNix\mkvmerge.exe" --ui-language en "%%a" --output "%%~na.mkv" --generate-chapters interval:00:05:00.000 --track-order 0:0,0:1
    No intermediate file, and quick.
    Thanks for that, but my code could do the same thing, except I deliberately choose not to let it after a few tests failed. So now I create the extra file and then immediately auto delete it afterward when file sizes are close enough. Of course, when it fails it is due to an error in the source file, and mkvmerge is not very forgiving in that regard I have found, and just exits ... doesn't process the rest of the file, truncates it even or zero bytes it.

    Originally Posted by pcspeak View Post
    Looking to check video/audio sync. A lot of the chunks will not have speech or someone facing the camera.
    I'll have a think about it.
    Thanks. True, but can't be helped. I am mostly concerned with audio and video being in existence, and that start and end points are what they should be. I have seen all sorts of results over the years ... some with no audio, some no video, some incomplete ... and of course audio/video sync issues. That last issue I can usually do something about at a later date (except for any missing portions). In reality all I am trying to achieve is a quick & dirty check, that won't catch everything, but hopefully covers most bases. I know I am going to find files with errors and missing portions, as I have seen that regularly in the past, so I am only making a limited attempt to catch some of those.

    Originally Posted by pcspeak View Post
    If the original mp4 is in sync then so too the mkv.
    Only the container is changed.
    Definitely, if the conversion (repackaging) went well. MKV has superior error correction, and even though all you are doing is changing the container, you will note there can be quite a difference in final video size ... so some things are changed (indexing etc) to suit the new format.

    Thanks for your response, suggestions and looking into/pondering it all.
    Last edited by Tombs; 1st Sep 2017 at 00:36.
    Quote Quote  
  7. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    You could use MediaInfoCLI to get the video and audio durations in a list.
    Then eyeball them to look for major discrepancies.
    Thats down & dirty.

    i.e.
    Video name
    V-length=
    A-length=

    Video name
    V-length=
    A-length=
    Quote Quote  
  8. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    Originally Posted by pcspeak View Post
    You could use MediaInfoCLI to get the video and audio durations in a list.
    Then eyeball them to look for major discrepancies.
    Thats down & dirty.
    True, but after years of using MediaInfoCLI I cannot trust it. It appears it will tell lies in some circumstances. A bit like saying a video file is a certain size, even though it only partially copied ... space was reserved for it and so the full size is reported but not all the file is there ... despite the evidence to say otherwise ... play it and you find it is incomplete compared to original, even though sizes are identical. Maybe it gets the detail from file header.
    Quote Quote  
  9. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Not much testing done.

    5 seconds of video every 240 seconds
    Code:
    ffmpeg.exe -i in.mp4 -vf select='lt(mod(t^,240)^,5)',setpts=N/FRAME_RATE/TB -af aselect='lt(mod(t^,240)^,5)',asetpts=N/SR/TB out.mp4
    I'd validate the mp4 before creating an mkv.
    It took 70 seconds to create a 1 minute video from a 45 minute input.mp4 on my pc.

    Cheers
    Quote Quote  
  10. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    Originally Posted by pcspeak View Post
    Not much testing done.

    5 seconds of video every 240 seconds
    Code:
    ffmpeg.exe -i in.mp4 -vf select='lt(mod(t^,240)^,5)',setpts=N/FRAME_RATE/TB -af aselect='lt(mod(t^,240)^,5)',asetpts=N/SR/TB out.mp4
    I'd validate the mp4 before creating an mkv.
    It took 70 seconds to create a 1 minute video from a 45 minute input.mp4 on my pc.
    Excellent, will check it out a bit later when I get enough time.
    I was looking at making it from the resulting MKV rather than original MP4, but will see what can be done.

    Quote Quote  
  11. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    I was looking at making it from the resulting MKV rather than original MP4
    Do Both.
    Quote Quote  
  12. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    To do multiple files.
    Removed the escape character (caret) and enclosed the filter in double quotes. Tested OK.
    Code:
    for %%a in ("*.mp4", "*.mkv") do "c:\vidaud\ffmpeg32\bin\ffmpeg.exe" -i "%%a" -vf "select='lt(mod(t,240),5)'",setpts=N/FRAME_RATE/TB -af "aselect='lt(mod(t,240),5)'",asetpts=N/SR/TB "Preview-%%~na.mp4"
    Quote Quote  
  13. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    Originally Posted by pcspeak View Post
    To do multiple files.
    Removed the escape character (caret) and enclosed the filter in double quotes. Tested OK.
    Code:
    for %%a in ("*.mp4", "*.mkv") do "c:\vidaud\ffmpeg32\bin\ffmpeg.exe" -i "%%a" -vf "select='lt(mod(t,240),5)'",setpts=N/FRAME_RATE/TB -af "aselect='lt(mod(t,240),5)'",asetpts=N/SR/TB "Preview-%%~na.mp4"
    Tried this version first and it gave surprising results ... created 2 files. I put your code into a BAT file and then dropped a video on it.

    Preview-videoname.mkv (35 secs)
    Preview-Preview-videoname.mkv (5 secs)

    So then I went back to your first code posting and that worked fine, except it took quite a while on my Netbook and the final frames of the video weren't captured.
    That last I can do something about with a little maths based on title duration and varying the capture points accordingly. If I am grabbing 5 seconds for each chunk, then I would divide the video duration (minus 5 seconds) by a number of specified chunks i.e. 8 maybe or 10 if easier.

    I am wondering if it is possible to save time by just grabbing the chunks individually and join them together afterward? At the moment it seems to parse every frame of the whole file, which seems unnecessary and time consuming. 70 seconds for you was probably at least triple that for me .... in fact the whole video probably downloaded quicker.

    Once again, thanks for your help with this.

    P.S. Obviously I need to study the code more deeply. I was tired and it was late last night when I tested it.
    Last edited by Tombs; 1st Sep 2017 at 21:01.
    Quote Quote  
  14. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    Spent a few hours on this, along with AV hassles and a reboot. Gotta go and do something else now, but here is what I have done so far.

    Once the program has run once, you can manually adjust some settings in the Preview.ini file.

    Best way to use it though, is to drop an MKV or MP4 file on the EXE. Make sure the exe is in the same folder as ffmpeg.exe ... though %path% may also work.

    Once again pcspeak thanks for your invaluable help. Pretty cool feature to have such a preview ability.

    Sadly it is currently too slow for me, as it took 15 minutes to get 6 x 5 second chunks into one preview file.
    [Duration]
    chunks=6
    time=5
    [Source File]
    duration=00:27:34.08
    seconds=1654
    [Working Duration]
    seconds=1649
    [Gaps Between Chunks]
    seconds=274
    [Run BAT File]
    start=2017/09/02 15:41:13
    finish=2017/09/02 15:56:18
    taken=14 mins 59 secs
    I am guessing it will run much faster for most others.
    I shudder to think how long an hour long or more video would take for me.
    Number of chunks doesn't seem to impact time taken, so I guess the whole video file is parsed regardless.

    Could well be quicker to divide the file up into individual video files first, then grab the first or last 5 seconds of each, then join together or as you go.

    NOTE - You can skip at the dialog and then run the created BAT file manually at your leisure. Or you can even disable the dialog and have it execute the BAT file immediately. See the last few settings in the Preview.ini file, and change the values of the relevant settings to a number other than '1'.

    EDIT
    Time does seem to make a difference. Same source file. Chunks always equates to one more than specified, so 4 = 5 .... with the last one being the last few specified seconds of the video file (usually contains end credits). I had hard-coded 5 seconds, but that very minor change made it match specified time. Chunks in reality means division points, so even with '1' you will have a before and after, so '2'.
    [Duration]
    chunks=4
    time=10
    [Run BAT File]
    taken=17 mins 36 secs
    Culminated in a preview file of 50 seconds.

    And by direct contrast. Same source file.
    [Duration]
    chunks=1
    time=5
    [Run BAT File]
    taken=11 mins 4 secs
    This preview file of 10 seconds, had the first 5 and last 5 seconds of the source video.

    And a special modified version of the program that allowed '0' chunks. Same source file.
    [Duration]
    chunks=0
    time=5
    [Run BAT File]
    taken=11 mins 2 secs
    Resulting preview file of first 5 seconds of the source video.


    Make Preview.zip (543.5 KB, 2 views)
    Re-uploaded after one very minor change.
    Image Attached Files
    Last edited by Tombs; 2nd Sep 2017 at 05:32.
    Quote Quote  
  15. Member hydra3333's Avatar
    Join Date
    Oct 2009
    Location
    Australia
    Search Comp PM
    update regarding post #31 :
    https://forum.doom9.org/showthread.php?p=1817171#post1817171

    I'll probably change it to also use dynaudnorm=f=150
    Quote Quote  
  16. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    I decided to check out the command-line options for mkvmerge and it seems the better way to go after a short test.

    Code:
    mkvmerge.exe -o "D:\Video\DOWNLOADS\URLDown Dropper\Downloads\Preview_If At First Series 1 Ep 2.mkv" --split parts:00:00:00-00:00:05,+00:06:00-00:06:05 "D:\Video\DOWNLOADS\URLDown Dropper\Downloads\If At First Series 1 Ep 2.mkv"
    That created a single file of 12 seconds (2 x 6 second sections).

    More testing needed of course, but very promising at this point, and only took a couple of seconds.
    Quote Quote  
  17. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    See Post #44 for more detail and Post #34 for my initial request for help.

    In the end, I have devised a solution using Mkvmerge instead of Ffmpeg. If only I had thought to go that route sooner.
    BIG thanks to pcspeak for his great effort ... ever the Batch and ffmpeg Guru.

    As mentioned earlier, just drop an MKV video file onto the program file (Make Preview.exe), to have a Preview file for it created.
    I have changed the default time for each segment to 4 seconds, as 5 seconds was delivering 6 seconds ... probably to do with keyframes being less or more.
    So with default chunks set as 9 (10 in reality) you get about 40 seconds of preview, made from the 10 segments extracted at equidistant locations throughout the source video.

    Average test results for me on my slow Netbook were as follows. Compare them to the earlier ffmpeg ones and you can see the huge improvement in time taken.
    [Duration]
    chunks=9
    time=4
    [Source File]
    duration=00:27:34.08
    seconds=1654
    [Working Duration]
    seconds=1650
    [Gaps Between Chunks]
    seconds=183
    [Run BAT File]
    start=2017/09/03 01:25:05
    finish=2017/09/03 01:25:18
    taken=7 secs
    Note - The discrepancy of 6 seconds is due to the delay in the BAT file, which gets removed from total time taken.

    P.S. This version also allows 0 chunks to be set, so you can just grab the first few (specified) seconds of the source video for the preview.
    Image Attached Files
    Last edited by Tombs; 2nd Sep 2017 at 12:15.
    Quote Quote  
  18. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    Finally got around to trying out an MP4 file with Make Preview, and it worked fine, except it retained the MP4 extension, when in reality it is an MKV file. So I need to fix that.
    Quote Quote  
  19. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    Fixed the MKV extension issue. This means that any file type that MkvMerge supports should work.
    Added a Source File Time limit, which is set to 300 (seconds) by default, which is 5 minutes. Any source file which is less than that will not get a Preview file.
    See the relevant entry in the Preview.ini file to modify the value. Set to '0' for no limit.

    The default Time of 4 (seconds) and 9 Chunks (division points) should result in a Preview file of around 40 seconds.

    I don't intend to do any more work on this stand-alone version, but have started implementing the option into my URLDown Dropper program for the v3.4 update, which will also have a dropbox for manual use.
    Image Attached Files
    Quote Quote  
  20. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    Originally Posted by Tombs View Post
    I don't intend to do any more work on this stand-alone version ......
    That is, until I found a bug ... a simple flaw in my maths ... didn't think to change one value. This bug meant any videos over an hour in duration caused an error and no preview file was created. Maths ... always hard work for my poor brain. I obviously did not have a longer video on hand when I did my testing.
    Image Attached Files
    Quote Quote  
  21. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Ran 'Make Preview.exe'
    All I got was an ini file containing:
    Code:
    source=D:\Video\DOWNLOADS\URLDown Dropper\Downloads\Q&A\Q&A - Bregman, Penny, Tharoor, Al-Khatahtbeh, Fullilove.mkv
    destination=D:\Video\DOWNLOADS\URLDown Dropper\Downloads\Q&A\Preview_Q&A - Bregman, Penny, Tharoor, Al-Khatahtbeh, Fullilove.mkv
    Quote Quote  
  22. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    Originally Posted by pcspeak View Post
    Ran 'Make Preview.exe'
    All I got was an ini file containing:
    Code:
    source=D:\Video\DOWNLOADS\URLDown Dropper\Downloads\Q&A\Q&A - Bregman, Penny, Tharoor, Al-Khatahtbeh, Fullilove.mkv
    destination=D:\Video\DOWNLOADS\URLDown Dropper\Downloads\Q&A\Preview_Q&A - Bregman, Penny, Tharoor, Al-Khatahtbeh, Fullilove.mkv
    It's really only an example of proof of concept, though quite usable, if not as user friendly as the manual Dropbox version available in URLDown Dropper.

    Once the program has run once, you can manually adjust some settings in the Preview.ini file.

    Best way to use it though, is to drop an MKV or MP4 file on the EXE. Make sure the exe is in the same folder as mkvmerge.exe and ffmpeg.exe ... though %path% may also work.
    Just drag and drop a video file onto Make Preview.exe ... or manually paste into the two INI file settings or use some other method to get them there.

    Drag and Drop is equivalent (of course) to just passing a quoted file path via the command-line.

    i.e. Make Preview.exe "D:\Video\DOWNLOADS\URLDown Dropper\Downloads\Q&A\Q&A - Bregman, Penny, Tharoor, Al-Khatahtbeh, Fullilove.mkv"

    P.S. More INI entries will exist after creating your first preview file. They can then be modified to suit.
    Last edited by Tombs; 10th Sep 2017 at 00:40. Reason: missing program name
    Quote Quote  
  23. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Thanks. Hadn't realised it's ffmpeg.exe & mkvmerge.exe in the folder with your program and the video.

    Cheers.
    Quote Quote  
  24. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    Oops! Sorry, I should have added in mkvmerge. Both mkvmerge and ffmpeg are required in same folder (or maybe %%path).
    Hadn't realized I'd quoted earlier instructions (pre mkvmerge) ... just copied & pasted ... I blame the hour.

    Mkvmerge creates the preview file based on the duration ffmpeg reports.
    Last edited by Tombs; 10th Sep 2017 at 00:45.
    Quote Quote  
  25. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    The following batch file seems to work happily with MP4 & MKV vids.
    Others, AVI, TS, MPG, etc. gave varying results. YMMV.
    Put the the batch file, ffmpeg.exe and all videos in the same folder.
    Run the batch file & come back later.

    Be aware, when you run the batch file a second time all existing previews are deleted.
    (else you end up with previews of previews - with no content.)

    Add whatever video file types you wish, to the last line.
    "*.avi", "*.ts", "*.mpg", "*.flv", "*.vob", etc.
    Results not guaranteed.

    Preview_Clips.cmd
    Code:
    @echo off
    echo 5 seconds of video output every 10 minutes.
    echo 30 seconds preview per 1 hour of video.
    timeout /T 5 /nobreak >nul
    
    for %%a in ("Preview-*.*") do del /q %%a
    for %%a in ("*.mp4", "*.mkv") do ffmpeg.exe -i "%%a" -vf "select='lt(mod(t,600),5)'",setpts=N/FRAME_RATE/TB -af "aselect='lt(mod(t,600),5)'",asetpts=N/SR/TB "Preview-%%a"
    Cheers.
    Last edited by pcspeak; 10th Sep 2017 at 03:45.
    Quote Quote  
  26. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    A shame we can't speed ffmpeg up, but I guess if you have the time then the simplicity of using a CMD batch file, where all you need is ffmpeg and no maths based on source duration, is attractive. Alas for me though, on my under-powered Netbook, time runs into several minutes with your CMD process (per video), whereas with mkvmerge + ffmpeg it only takes a few seconds each.

    Cheers!
    Quote Quote  
  27. Member
    Join Date
    Nov 2014
    Location
    Australia
    Search Comp PM
    And I discovered another bug, a 60 that should have been 59 .... that's what happens when you copy & paste & modify, rather than work through it from scratch.
    This stopped a preview file being created in some instances.
    Image Attached Files
    Quote Quote  
  28. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    If there is a major spike in the audio anywhere then normalizing without first removing the spike(s) is pointless.
    I always drop a video onto Audacity and check the wave form for such distortions.
    If there are, I use Audacity's 'Effect/Limiter.../Soft Limit' - Limit to (dB): - adjusted acordingly.
    Export to a new audio.
    Mux with the video. (see an earlier post in this thread)
    Then run the following batch file.

    It may seem like a lot of effort but it's mostly:
    Drop the video(s) onto Audacity.
    Looks good - no audio spikes
    Exit Audacity.
    Run the batch file.

    For me, the following batch file does a better job of enhancing the audio of a video than the one I previously posted.
    Mind you, if you start with 128kb input audio then you'll never be really happy with results.
    Start with the highest bit rate you can. Make all the changes. Reduce the bit rate if you have to.
    Don't get too hooked up on "smallest" file sizes. If the audio is rubbish, what's the point?

    Put the the batch file, ffmpeg.exe and all videos in the same folder.
    Run the batch file & come back later.
    (Speed depends a LOT on the power of your PC. Mine takes approx. 20 mins. per hour of video.)

    Code:
    @echo off
    :: 2 pass. 1st pass: Detect the loudness.
    ::         2nd pass: Modify the volume.
    
    if not exist Amplified\*.* md Amplified
    if exist tmp*.txt del /q tmp*.txt
    for %%a in ("*.mp4", "*.avi", "*.mkv") do call :loudnorm "%%a"
    goto :end
    
    :loudnorm
    SETLOCAL
    echo. &echo "%~1"
    echo Detecting the loudness, and setting environment variables.
    echo Please wait.....
    ffmpeg.exe -i "%~1" -af loudnorm=I=-16:TP=-1.5:LRA=11:print_format=summary -f null -y nul 2>&1 | for /f "tokens=1-6 delims= " %%a in ('find " "') do echo %%a %%b %%c %%d %%e %%f >>tmp1.txt
    
    type tmp1.txt | for /f "tokens=1-4 delims= " %%a in ('find "Input Integrated"')do echo %%c >tmp2.txt
    set /p in-integ= < tmp2.txt
    set "in-integ=%in-integ:~0,-1%"
    type tmp1.txt | for /f "tokens=1-6 delims= " %%a in ('find "Input True Peak"')do echo %%d >tmp2.txt
    set /p in-truep= < tmp2.txt
    set in-truep=%in-truep:~0,-1%
    type tmp1.txt | for /f "tokens=1-4 delims= " %%a in ('find "Input LRA"')do echo %%c >tmp2.txt
    set /p in-lra= < tmp2.txt
    set "in-lra=%in-lra:~0,-1%"
    type tmp1.txt | for /f "tokens=1-4 delims= " %%a in ('find "Input Threshold"')do echo %%c >tmp2.txt
    set /p in-thresh= < tmp2.txt
    set "in-thresh=%in-thresh:~0,-1%"
    type tmp1.txt | for /f "tokens=1-4 delims= " %%a in ('find "Target Offset"')do echo %%c >tmp2.txt
    set /p offset= < tmp2.txt
    set "offset=%offset:~0,-1%"
    
    :: Normalize the loudness.
    ffmpeg.exe -i "%~1" -c:v copy -af loudnorm=I=-16:TP=-1.5:LRA=11:measured_I=%in-integ%:measured_TP=%in-truep%:measured_LRA=%in-lra%:measured_thresh=%in-thresh%:offset=%offset%:linear=true:print_format=summary -max_muxing_queue_size 400 -ac 2 -threads 0 -y "Amplified\%~1"
    
    del /q tmp*.txt
    ENDLOCAL
    goto :eof
    
    :end
    echo. &echo Output is in %CD%\Amplified
    echo Press any key to exit... &pause>nul
    Have fun.
    Cheers.

    EDIT:
    Small fix.
    If trying this batch file, please grab it again.
    Forgot stream copy of video. -c:v copy
    Last edited by pcspeak; 26th Sep 2017 at 21:14.
    Quote Quote  
  29. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    @hydra3333
    I was just reading through this thread again to refresh my memory.
    I've just realised that you had already posted a method, using loudnorm, that was pretty much the same
    as the one in my last post.
    All kudos to you. I'm just a little slow. (And late to the party.)

    Cheers.
    Last edited by pcspeak; 3rd Dec 2017 at 22:19.
    Quote Quote  
  30. Member hydra3333's Avatar
    Join Date
    Oct 2009
    Location
    Australia
    Search Comp PM
    Yours is better than mine

    I've found recently that a number of "variable audio sources" seem to "pump" a bit with loudnorm and I'm not altogether comfortable with the results when listening in my car.

    Why not ... a couple of hacked up crummy scripts for posterity.

    1. the loudnorm one (it falls back to dynaudnorm if required)
    Code:
    @echo on
    
    rem http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide
    
    setlocal enableextensions enabledelayedexpansion
    
    set xpause=
    set mp3path=.\newmp3
    
    MD "%mp3path%"
    
    for %%f in (*.m*) do (
       CALL :do_with_normalisation "%%f"
    REM   CALL :do_without_normalisation "%%f"
    )
    pause
    exit
    
    :do_with_normalisation
    echo OFF
    @setlocal ENABLEDELAYEDEXPANSION
    @setlocal enableextensions
    
    REM --- Start audio and other parameters ------------------------------ 
    REM set makeaudiotype=aac
    call :000-setup
    call :maketempheader
    REM
    set makeaudiotype=mp3
    set audiofreq=44100
    set audiobitrate=256k
    SET lI=-12
    SET lTP=0.0
    SET lLRA=11
    set loudnormfilter=
    REM +++
    SET tempFile=temp_%tempheader%.txt
    "!mediainfoexe!" "--Inform=Audio;%%Codec/String%%" "%~dpnx1" > "%tempfile%"
    set /p theAudioCodec= < "%tempfile%" 
    SET tempFile=temp_%tempheader%.txt
    "!mediainfoexe!" "--Inform=Audio;%%BitRate/String%%" "%~dpnx1" > "%tempfile%"
    set /p theAudioBitrate= < "%tempfile%" 
    SET tempFile=temp_%tempheader%.txt
    "!mediainfoexe!" "--Inform=Audio;%%SamplingRate/String%%" "%~dpnx1" > "%tempfile%"
    set /p theAudioSamplingRate= < "%tempfile%" 
    set theExt=%~x1
    REM +++
    REM --- End audio parameters ------------------------------ 
    
    ECHO *** !DATE! !TIME! Start Loudness Adjusted Audio conversion to %makeaudiotype%
    REM --- Start Find audio characteristics ------------------------------ 
    REM to adjust Audio volume. find the loudness parameters in a first pass
    ECHO *** !DATE! !TIME! *** Start Find Audio Loudness ***
    SET jsonFile=%~dpn1-%tempheader%.json
    echo on
    "%ffmpegexex64%" -nostats -hide_banner -nostdin -y -i "%~1" -vn -af loudnorm=I=%lI%:TP=%lTP%:LRA=%lLRA%:print_format=json -f null - 2> "%jsonFile%"  
    SET EL=!ERRORLEVEL!
    echo off
    IF /I "!EL!" NEQ "0" (
       Echo *** !DATE! !TIME! ***  Error !EL! was found 
       Echo *** !DATE! !TIME! ***  ABORTING ... 
       %xpause%
       EXIT !EL!
    )
    REM all the trickery below is simply to remove quotes and tabs and spaces from the json single-level response
    set input_i=
    set input_tp=
    set input_lra=
    set input_thresh=
    set target_offset=
    for /f "tokens=1,2 delims=:, " %%a in (' find ":" ^< "%jsonFile%" ') do (
       set "var="
       for %%c in (%%~a) do set "var=!var!,%%~c"
       set var=!var:~1!
       set "val="
       for %%d in (%%~b) do set "val=!val!,%%~d"
       set val=!val:~1!
    REM   echo .!var!.=.!val!.
       IF "!var!" == "input_i"         set !var!=!val!
       IF "!var!" == "input_tp"        set !var!=!val!
       IF "!var!" == "input_lra"       set !var!=!val!
       IF "!var!" == "input_thresh"    set !var!=!val!
       IF "!var!" == "target_offset"   set !var!=!val!
    )
    REM ECHO *** !DATE! !TIME! input_i=%input_i% 
    REM ECHO *** !DATE! !TIME! input_tp=%input_tp% 
    REM ECHO *** !DATE! !TIME! input_lra=%input_lra% 
    REM ECHO *** !DATE! !TIME! input_thresh=%input_thresh% 
    REM ECHO *** !DATE! !TIME! target_offset=%target_offset% 
    REM check for bad loudnorm values ... if baddies found, use dynaudnorm instead
    set AUDprocess=loudnorm
    IF /I "%input_i%" == "inf" (set AUDprocess=dynaudnorm)
    IF /I "%input_i%" == "-inf" (set AUDprocess=dynaudnorm)
    IF /I "%input_tp%" == "inf" (set AUDprocess=dynaudnorm)
    IF /I "%input_tp%" == "-inf" (set AUDprocess=dynaudnorm)
    IF /I "%input_lra%" == "inf" (set AUDprocess=dynaudnorm)
    IF /I "%input_lra%" == "-inf" (set AUDprocess=dynaudnorm)
    IF /I "%input_thresh%" == "inf" (set AUDprocess=dynaudnorm)
    IF /I "%input_thresh%" == "-inf" (set AUDprocess=dynaudnorm)
    IF /I "%target_offset%" == "inf" (set AUDprocess=dynaudnorm)
    IF /I "%target_offset%" == "-inf" (set AUDprocess=dynaudnorm)
    REM
    REM later, in an encoding pass we MUST down-convert from 192k (loadnorm upsamples it to 192k whis is way way too high ... use  -ar 48k or -ar 48000
    REM
    IF /I "%AUDprocess%" == "loudnorm" (
       ECHO *** !DATE! !TIME! "Proceeding with normal LOUDNORM audio normalisation ..."
       set loudnormfilter=loudnorm=I=%lI%:TP=%lTP%:LRA=%lLRA%:measured_I=%input_i%:measured_LRA=%input_lra%:measured_TP=%input_tp%:measured_thresh=%input_thresh%:offset=%target_offset%:linear=true:print_format=summary
    ) ELSE (
       ECHO *** !DATE! !TIME! *** ERROR VALUES DETECTED FROM LOUDNORM - Doing dynaudnorm instead ..." 
       set loudnormfilter=dynaudnorm
    )
    REM ECHO *** !DATE! !TIME! "loudnormfilter=%loudnormfilter%" 
    IF EXIST "%jsonFile%" DEL "%jsonFile%"
    ECHO *** !DATE! !TIME! *** End Find Audio Loudness ***
    REM --- End Find audio characteristics ------------------------------ 
    
    REM --- Start audio conversion ------------------------------ 
    ECHO *** !DATE! !TIME! *** Start ffmpeg %makeaudiotype% AUDIO conversion *** 
    REM mp3 audio conversion
    IF /I "%makeaudiotype%" == "mp3" (
    REM ECHO  mp3 audio conversion "%~f1" ...
    echo on
    echo "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -af %loudnormfilter% -c:a libmp3lame -q 0 -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y "%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.mp3" 
    "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -af %loudnormfilter% -c:a libmp3lame -q 0 -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y "%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.mp3" 
    SET EL=!ERRORLEVEL!
    echo off
    IF /I "!EL!" NEQ "0" (
       Echo *** !DATE! !TIME! ***  Error !EL! was found 
       Echo *** !DATE! !TIME! ***  ABORTING ... 
       %xpause%
       EXIT !EL!
    )
    )
    REM aac audio conversion
    IF /I "%makeaudiotype%" == "aac" (
    REM ECHO  aac audio conversion "%~f1" ...
    echo on
    echo "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -af %loudnormfilter% -c:a libfdk_aac -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y "%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.aac" 
    "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -af %loudnormfilter% -c:a libfdk_aac -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y "%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.aac" 
    SET EL=!ERRORLEVEL!
    echo off
    IF /I "!EL!" NEQ "0" (
       Echo *** !DATE! !TIME! ***  Error !EL! was found 
       Echo *** !DATE! !TIME! ***  ABORTING ... 
       %xpause%
       EXIT !EL!
    )
    )
    ECHO *** !DATE! !TIME! *** End ffmpeg %makeaudiotype% AUDIO conversion  
    ECHO *** !DATE! !TIME! *** Input File: %~1
    %xpause%
    goto :eof
    
    
    :do_without_normalisation
    echo OFF
    @setlocal ENABLEDELAYEDEXPANSION
    @setlocal enableextensions
    
    REM --- Start audio and other parameters ------------------------------ 
    REM set makeaudiotype=aac
    call :000-setup
    REM
    set makeaudiotype=mp3
    set audiofreq=44100
    set audiobitrate=256k
    SET lI=-12
    SET lTP=0.0
    SET lLRA=11
    set loudnormfilter=
    REM +++
    SET tempFile=temp_%tempheader%.txt
    "!mediainfoexe!" "--Inform=Audio;%%Codec/String%%" "%~dpnx1" > "%tempfile%"
    set /p theAudioCodec= < "%tempfile%" 
    SET tempFile=temp_%tempheader%.txt
    "!mediainfoexe!" "--Inform=Audio;%%BitRate/String%%" "%~dpnx1" > "%tempfile%"
    set /p theAudioBitrate= < "%tempfile%" 
    SET tempFile=temp_%tempheader%.txt
    "!mediainfoexe!" "--Inform=Audio;%%SamplingRate/String%%" "%~dpnx1" > "%tempfile%"
    set /p theAudioSamplingRate= < "%tempfile%" 
    set theExt=%~x1
    REM +++
    REM --- End audio parameters ------------------------------ 
    
    REM --- Start audio conversion ------------------------------ 
    ECHO *** !DATE! !TIME! *** Start ffmpeg %makeaudiotype% AUDIO conversion *** 
    REM mp3 audio conversion
    IF /I "%makeaudiotype%" == "mp3" (
    REM ECHO  mp3 audio conversion "%~f1" ...
    echo on
    echo "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -c:a libmp3lame -q 0 -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y ""%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.mp3" 
    "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -c:a libmp3lame -q 0 -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y "%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.mp3" 
    SET EL=!ERRORLEVEL!
    echo off
    IF /I "!EL!" NEQ "0" (
       Echo *** !DATE! !TIME! ***  Error !EL! was found 
       Echo *** !DATE! !TIME! ***  ABORTING ... 
       %xpause%
       EXIT !EL!
    )
    )
    REM aac audio conversion
    IF /I "%makeaudiotype%" == "aac" (
    REM ECHO  aac audio conversion "%~f1" ...
    echo on
    echo "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -c:a libfdk_aac -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y "%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.aac" 
    "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -c:a libfdk_aac -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y "%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.aac" 
    SET EL=!ERRORLEVEL!
    echo off
    IF /I "!EL!" NEQ "0" (
       Echo *** !DATE! !TIME! ***  Error !EL! was found 
       Echo *** !DATE! !TIME! ***  ABORTING ... 
       %xpause%
       EXIT !EL!
    )
    )
    ECHO *** !DATE! !TIME! *** End ffmpeg %makeaudiotype% AUDIO conversion  
    ECHO *** !DATE! !TIME! *** Input File: %~1
    %xpause%
    goto :eof
    
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    :000-setup
    REM Setup variables for executable image paths so they can be commonly used across batch files
    REM ------ WIN10 start
    REM echo *** !DATE! !TIME! Start of exe path variables ...  
    REM -- win32 ---------------------------------------------------------------------
    REM set ffmpegexex32=C:\ffmpeg\x32\bin\ffmpeg.exe
    set ffmpegexex32=C:\ffmpeg\x32\ffmpeg.exe
    set mp4boxexex32=C:\ffmpeg\x32\MP4Box.exe
    REM -- win64 ---------------------------------------------------------------------
    REM set ffmpegexex64=C:\ffmpeg\x64\bin\ffmpeg.exe
    set ffmpegexex64=C:\ffmpeg\x64\ffmpeg.exe
    set x264exex64=C:\ffmpeg\x64\x264-mp4.exe
    set mp4boxexex64=C:\ffmpeg\x64\MP4Box.exe
    REM -- common ---------------------------------------------------------------------
    set mediainfoexe=C:\mediainfo\mediainfo.exe
    REM ------ WIN10 end
    echo -- win32
    REM echo "%ffmpegexex32%"
    REM echo "%x264exex32%"
    REM echo "%mp4boxexex32%"
    echo -- win64
    REM echo "%ffmpegexex64%"
    REM echo "%x264exex64%"
    REM echo "%mp4boxexex64%"
    echo -- common
    REM echo "%mediainfoexe%"
    REM echo *** !DATE! !TIME! End of exe path variables ...  
    goto :eof
    
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    :LoCase
    :: Subroutine to convert a variable VALUE to all lower case.
    :: The argument for this subroutine is the variable NAME.
    FOR %%i IN ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z") DO CALL SET "%1=%%%1:%%~i%%"
    GOTO:EOF
    :UpCase
    :: Subroutine to convert a variable VALUE to all UPPER CASE.
    :: The argument for this subroutine is the variable NAME.
    FOR %%i IN ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") DO CALL SET "%1=%%%1:%%~i%%"
    GOTO:EOF
    :TCase
    :: Subroutine to convert a variable VALUE to Title Case.
    :: The argument for this subroutine is the variable NAME.
    FOR %%i IN (" a= A" " b= B" " c= C" " d= D" " e= E" " f= F" " g= G" " h= H" " i= I" " j= J" " k= K" " l= L" " m= M" " n= N" " o= O" " p= P" " q= Q" " r= R" " s= S" " t= T" " u= U" " v= V" " w= W" " x= X" " y= Y" " z= Z") DO CALL SET "%1=%%%1:%%~i%%"
    GOTO :EOF
    
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    REM --- start set a temp header to date and time
    :maketempheader
    set Datex=%DATE%
    set yyyy=%Datex:~10,4%
    set mm=%Datex:~7,2%
    set dd=%Datex:~4,2%
    set Timex=%time%
    set hh=%Timex:~0,2%
    set min=%Timex:~3,2%
    set ss=%Timex:~6,2%
    set ms=%Timex:~9,2%
    REM Strip any leading spaces from hours
    Set hh=%hh: =%
    REM Ensure the hours have a leading zero
    if 1%hh% LSS 20 Set hh=0%hh%
    REM echo As at %yyyy%.%mm%.%dd%_%hh%.%min%.%ss%.%ms%  
    set tempheader=%yyyy%.%mm%.%dd%.%hh%.%min%.%ss%.%ms%
    REM --- end set a temp header to date and time
    goto :eof
    2. The one I tend to prefer now. It doesn't do the same thing at all ... but is OK for my purposes.

    Code:
    @echo on
    
    rem http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide
    
    setlocal enableextensions enabledelayedexpansion
    
    set xpause=pause
    set mp3path=.\newmp3
    
    MD "%mp3path%"
    
    for %%f in (*.m*) do (
       CALL :do_with_normalisation "%%f"
    REM   CALL :do_without_normalisation "%%f"
    )
    pause
    exit
    
    :do_with_normalisation
    echo OFF
    @setlocal ENABLEDELAYEDEXPANSION
    @setlocal enableextensions
    
    echo %DATE% %TIME% Start "%~1"
    
    REM --- Start audio and other parameters ------------------------------ 
    call :000-setup
    call :maketempheader
    REM
    REM set makeaudiotype=aac
    set makeaudiotype=mp3
    set audiofreq=44100
    set audiobitrate=256k
    REM +++
    SET tempFile=temp_%tempheader%.txt
    "!mediainfoexe!" "--Inform=Audio;%%Codec/String%%" "%~dpnx1" > "%tempfile%"
    set /p theAudioCodec= < "%tempfile%" 
    SET tempFile=temp_%tempheader%.txt
    "!mediainfoexe!" "--Inform=Audio;%%BitRate/String%%" "%~dpnx1" > "%tempfile%"
    set /p theAudioBitrate= < "%tempfile%" 
    SET tempFile=temp_%tempheader%.txt
    "!mediainfoexe!" "--Inform=Audio;%%SamplingRate/String%%" "%~dpnx1" > "%tempfile%"
    set /p theAudioSamplingRate= < "%tempfile%" 
    set theExt=%~x1
    IF EXIST "%tempfile%" DEL "%tempfile%"
    REM +++
    REM --- End audio parameters ------------------------------ 
    
    REM --- Start find loudness adjustment ------------------------------ 
    SET aVolFile=.\%~n1.%tempheader%.vol.txt
    SET aebur128File=.\%~n1.%tempheader%.ebur.txt
    SET vbsVolFile=.\%~n1.%tempheader%.findvolume.vbs
    echo %DATE% %TIME% detect required volume increase ---------------------------
    @echo on
    "%ffmpegexex64%" -hide_banner -y -vn -nostats -i "%~1" -af volumedetect -f null - > "%aVolFile%" 2>&1
    "%ffmpegexex64%" -hide_banner -y -vn -nostats -i "%~1" -filter_complex ebur128 -f null - > "%aebur128File%" 2>&1 
    @echo off
    ECHO ' read vol from ffmpeg file and return the Maximum Volume value only >  "%vbsVolFile%"
    ECHO Const ForReading = 1 >>  "%vbsVolFile%"
    ECHO Dim objStdOut, objFSO , objFile, line, e, s, v, adj, adjV, adjE, er  >>  "%vbsVolFile%"
    ECHO 'Set objStdOut = WScript.StdOut  >>  "%vbsVolFile%"
    ECHO Set objFSO = CreateObject("Scripting.FileSystemObject") >>  "%vbsVolFile%"
    ECHO Set objFile = objFSO.OpenTextFile("!aVolFile!", ForReading) >>  "%vbsVolFile%"
    ECHO er = 1 >>  "%vbsVolFile%"
    ECHO v = 0 >>  "%vbsVolFile%"
    ECHO adj = 0 >>  "%vbsVolFile%"
    ECHO Do Until objFile.AtEndOfStream >>  "%vbsVolFile%"
    ECHO    line = lcase(objFile.ReadLine) >>  "%vbsVolFile%"
    ECHO    if instr(line,lcase("max_volume:")) ^> 0 then >>  "%vbsVolFile%"
    ECHO       s = instr(line,lcase("max_volume:")) + 11 >>  "%vbsVolFile%"
    ECHO       e = instr(s,line,lcase("dB")) - 1 >>  "%vbsVolFile%"
    ECHO       'WScript.StdOut.WriteLine "line=" ^& line >>  "%vbsVolFile%"
    ECHO       'WScript.StdOut.WriteLine s ^& " " ^& e  >>  "%vbsVolFile%"
    ECHO       if s^<=0 or e^<=0 then  >>  "%vbsVolFile%"
    ECHO          exit do >>  "%vbsVolFile%"
    ECHO       end if >>  "%vbsVolFile%"
    ECHO       v = trim(mid(line,s,e-s+1)) >>  "%vbsVolFile%"
    ECHO       'WScript.StdOut.WriteLine "string v=" ^& v >>  "%vbsVolFile%"
    ECHO       vvvc = v >>  "%vbsVolFile%"
    ECHO       v = cdbl(v) >>  "%vbsVolFile%"
    ECHO       'WScript.StdOut.WriteLine "cdbl v=" ^& v >>  "%vbsVolFile%"
    ECHO       adj = min(abs(v),16) >>  "%vbsVolFile%"
    ECHO       'WScript.StdOut.WriteLine "adj=" ^& adj >>  "%vbsVolFile%"
    ECHO       er = 0 >>  "%vbsVolFile%"
    ECHO       exit do >>  "%vbsVolFile%"
    ECHO    end if >>  "%vbsVolFile%"
    ECHO Loop >>  "%vbsVolFile%"
    ECHO adjV=adj >>  "%vbsVolFile%"
    ECHO objFile.Close >>  "%vbsVolFile%"
    ECHO set objFile = nothing >>  "%vbsVolFile%"
    ECHO 'set objFSO = nothing >>  "%vbsVolFile%"
    ECHO 'WScript.StdOut.WriteLine adj >>  "%vbsVolFile%"
    ECHO ' read ebur128 from ffmpeg file and return the Integrated Loudness I value only >>  "%vbsVolFile%"
    ECHO 'Const ForReading = 1 >>  "%vbsVolFile%"
    ECHO 'Dim objStdOut, objFSO , objFile, line, e, s, v, adj, adjV, adjE, er  >>  "%vbsVolFile%"
    ECHO ''Set objStdOut = WScript.StdOut  >>  "%vbsVolFile%"
    ECHO 'Set objFSO = CreateObject("Scripting.FileSystemObject") >>  "%vbsVolFile%"
    ECHO Set objFile = objFSO.OpenTextFile("!aebur128File!", ForReading) >>  "%vbsVolFile%"
    ECHO er = 1 >>  "%vbsVolFile%"
    ECHO v = 0 >>  "%vbsVolFile%"
    ECHO adj = 0 >>  "%vbsVolFile%"
    ECHO Do Until objFile.AtEndOfStream >>  "%vbsVolFile%"
    ECHO    line = lcase(objFile.ReadLine) >>  "%vbsVolFile%"
    ECHO    if instr(line,lcase("Integrated loudness:")) ^> 0 then >>  "%vbsVolFile%"
    ECHO       line = lcase(objFile.ReadLine) >>  "%vbsVolFile%"
    ECHO       s = instr(line,lcase("I:")) + 2 >>  "%vbsVolFile%"
    ECHO       e = instr(s,line,lcase("LUFS")) - 1 >>  "%vbsVolFile%"
    ECHO       'WScript.StdOut.WriteLine "line=" ^& line >>  "%vbsVolFile%"
    ECHO       'WScript.StdOut.WriteLine s ^& " " ^& e  >>  "%vbsVolFile%"
    ECHO       if s^<=0 or e^<=0 then  >>  "%vbsVolFile%"
    ECHO          exit do >>  "%vbsVolFile%"
    ECHO       end if >>  "%vbsVolFile%"
    ECHO       v = trim(mid(line,s,e-s+1)) >>  "%vbsVolFile%"
    ECHO       'WScript.StdOut.WriteLine "string v=" ^& v >>  "%vbsVolFile%"
    ECHO       v = cdbl(v) >>  "%vbsVolFile%"
    ECHO       'WScript.StdOut.WriteLine "cdbl v=" ^& v >>  "%vbsVolFile%"
    ECHO ''' adjust to -19 (-23 is standard) >>  "%vbsVolFile%"
    ECHO '''      v = -23 - v >>  "%vbsVolFile%"
    ECHO       v = -19 - v >>  "%vbsVolFile%"
    ECHO       vvvNL = v >>  "%vbsVolFile%"
    ECHO       'WScript.StdOut.WriteLine "-23-v v=" ^& v >>  "%vbsVolFile%"
    ECHO       adj = min(abs(max(v,0)),16) >>  "%vbsVolFile%"
    ECHO       'WScript.StdOut.WriteLine "adj=" ^& adj >>  "%vbsVolFile%"
    ECHO       er = 0 >>  "%vbsVolFile%"
    ECHO       exit do >>  "%vbsVolFile%"
    ECHO    end if >>  "%vbsVolFile%"
    ECHO Loop >>  "%vbsVolFile%"
    ECHO adjE=adj >>  "%vbsVolFile%"
    ECHO '''adj = max(min(adjV,adjE),0) >>  "%vbsVolFile%"
    ECHO adj = adjE >>  "%vbsVolFile%"
    ECHO 'WScript.StdOut.WriteLine adj >>  "%vbsVolFile%"
    ECHO 'WScript.StdOut.WriteLine adj ^& "," ^& adjV ^& "," ^& adjE >>  "%vbsVolFile%"
    ECHO  WScript.StdOut.WriteLine adj ^& "," ^& adjV ^& "," ^& adjE ^& "," ^& vvvc ^& "," ^& vvvnl >>  "%vbsVolFile%"
    ECHO objFile.Close >>  "%vbsVolFile%"
    ECHO set objFile = nothing >>  "%vbsVolFile%"
    ECHO set objFSO = nothing >>  "%vbsVolFile%"
    ECHO wscript.quit(er) ' 1=error. 0=ok >>  "%vbsVolFile%"
    ECHO function Max(a,b) >>  "%vbsVolFile%"
    ECHO     Max = a >>  "%vbsVolFile%"
    ECHO     If b ^> a then Max = b >>  "%vbsVolFile%"
    ECHO end function >>  "%vbsVolFile%"
    ECHO function Min(a,b) >>  "%vbsVolFile%"
    ECHO     Min = a >>  "%vbsVolFile%"
    ECHO     If b ^< a then Min = b >>  "%vbsVolFile%"
    ECHO end function  >>  "%vbsVolFile%"
    
    FOR /F "tokens=1,2,3,4,5 delims=," %%i IN ('cscript //B //NOLOGO  "%vbsVolFile%"') DO ( 
    @echo ----- FOUND LOUDNESS  adjV=%%i   **adjVOL=%%j**   adjLUFS=%%k   vvvc=%%l   vvvNL=%%m
    @echo ----- FOUND LOUDNESS  adjV=%%i   **adjVOL=%%j**   adjLUFS=%%k   vvvc=%%l   vvvNL=%%m
    @echo ----- FOUND LOUDNESS  adjV=%%i   **adjVOL=%%j**   adjLUFS=%%k   vvvc=%%l   vvvNL=%%m
    @echo ----- FOUND LOUDNESS  adjV=%%i   **adjVOL=%%j**   adjLUFS=%%k   vvvc=%%l   vvvNL=%%m
    @echo ----- FOUND LOUDNESS  adjV=%%i   **adjVOL=%%j**   adjLUFS=%%k   vvvc=%%l   vvvNL=%%m
    SET adjV=%%i
    SET adjVOL=%%j
    SET adjLUFS=%%k
    )
    REM --- End find loudness adjustment ------------------------------ 
    
    ECHO *** !DATE! !TIME! Start Loudness Adjusted Audio conversion to %makeaudiotype%
    REM --- Start audio conversion ------------------------------ 
    ECHO *** !DATE! !TIME! *** Start ffmpeg %makeaudiotype% AUDIO conversion *** 
    REM mp3 audio conversion
    IF /I "%makeaudiotype%" == "mp3" (
    REM ECHO  mp3 audio conversion "%~f1" ...
    echo on
    echo "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -af volume=volume=!adjVOL!dB:precision=float -c:a libmp3lame -q 0 -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y "%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.mp3" 
    "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -af volume=volume=!adjVOL!dB:precision=float -c:a libmp3lame -q 0 -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y "%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.mp3" 
    SET EL=!ERRORLEVEL!
    echo off
    IF /I "!EL!" NEQ "0" (
       Echo *** !DATE! !TIME! ***  Error !EL! was found 
       Echo *** !DATE! !TIME! ***  ABORTING ... 
       %xpause%
       EXIT !EL!
    )
    )
    REM aac audio conversion
    IF /I "%makeaudiotype%" == "aac" (
    REM ECHO  aac audio conversion "%~f1" ...
    echo on
    echo "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -af volume=volume=!adjVOL!dB:precision=float -c:a libfdk_aac -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y "%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.aac" 
    "%ffmpegexex64%" -hide_banner -i "%~1" -vn -map_metadata -1 -af volume=volume=!adjVOL!dB:precision=float -c:a libfdk_aac -cutoff 18000 -ab %audiobitrate% -ar %audiofreq% -y "%mp3path%\%~n1.%audiobitrate%.norm.%theAudioCodec%.%theAudioSamplingRate%%theExt%.aac" 
    SET EL=!ERRORLEVEL!
    echo off
    IF /I "!EL!" NEQ "0" (
       Echo *** !DATE! !TIME! ***  Error !EL! was found 
       Echo *** !DATE! !TIME! ***  ABORTING ... 
       %xpause%
       EXIT !EL!
    )
    )
    IF EXIST "%vbsVolFile%" DEL "%vbsVolFile%"
    IF EXIST "%aVolFile%" DEL "%aVolFile%"
    IF EXIST "%aebur128File%" DEL "%aebur128File%"
    ECHO *** !DATE! !TIME! *** End ffmpeg %makeaudiotype% AUDIO conversion  
    ECHO *** !DATE! !TIME! *** Input File: "%~1"
    REM %xpause%
    goto :eof
    
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    :000-setup
    REM Setup variables for executable image paths so they can be commonly used across batch files
    REM ------ WIN10 start
    REM echo *** !DATE! !TIME! Start of exe path variables ...  
    REM -- win32 ---------------------------------------------------------------------
    REM set ffmpegexex32=C:\ffmpeg\x32\bin\ffmpeg.exe
    set ffmpegexex32=C:\ffmpeg\x32\ffmpeg.exe
    set mp4boxexex32=C:\ffmpeg\x32\MP4Box.exe
    REM -- win64 ---------------------------------------------------------------------
    REM set ffmpegexex64=C:\ffmpeg\x64\bin\ffmpeg.exe
    set ffmpegexex64=C:\ffmpeg\x64\ffmpeg.exe
    set x264exex64=C:\ffmpeg\x64\x264-mp4.exe
    set mp4boxexex64=C:\ffmpeg\x64\MP4Box.exe
    REM -- common ---------------------------------------------------------------------
    set mediainfoexe=C:\mediainfo\mediainfo.exe
    REM ------ WIN10 end
    echo -- win32
    REM echo "%ffmpegexex32%"
    REM echo "%x264exex32%"
    REM echo "%mp4boxexex32%"
    echo -- win64
    REM echo "%ffmpegexex64%"
    REM echo "%x264exex64%"
    REM echo "%mp4boxexex64%"
    echo -- common
    REM echo "%mediainfoexe%"
    REM echo *** !DATE! !TIME! End of exe path variables ...  
    goto :eof
    
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    :LoCase
    :: Subroutine to convert a variable VALUE to all lower case.
    :: The argument for this subroutine is the variable NAME.
    FOR %%i IN ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z") DO CALL SET "%1=%%%1:%%~i%%"
    GOTO:EOF
    :UpCase
    :: Subroutine to convert a variable VALUE to all UPPER CASE.
    :: The argument for this subroutine is the variable NAME.
    FOR %%i IN ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") DO CALL SET "%1=%%%1:%%~i%%"
    GOTO:EOF
    :TCase
    :: Subroutine to convert a variable VALUE to Title Case.
    :: The argument for this subroutine is the variable NAME.
    FOR %%i IN (" a= A" " b= B" " c= C" " d= D" " e= E" " f= F" " g= G" " h= H" " i= I" " j= J" " k= K" " l= L" " m= M" " n= N" " o= O" " p= P" " q= Q" " r= R" " s= S" " t= T" " u= U" " v= V" " w= W" " x= X" " y= Y" " z= Z") DO CALL SET "%1=%%%1:%%~i%%"
    GOTO :EOF
    
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    REM --- start set a temp header to date and time
    :maketempheader
    set Datex=%DATE%
    set yyyy=%Datex:~10,4%
    set mm=%Datex:~7,2%
    set dd=%Datex:~4,2%
    set Timex=%time%
    set hh=%Timex:~0,2%
    set min=%Timex:~3,2%
    set ss=%Timex:~6,2%
    set ms=%Timex:~9,2%
    REM Strip any leading spaces from hours
    Set hh=%hh: =%
    REM Ensure the hours have a leading zero
    if 1%hh% LSS 20 Set hh=0%hh%
    REM echo As at %yyyy%.%mm%.%dd%_%hh%.%min%.%ss%.%ms%  
    set tempheader=%yyyy%.%mm%.%dd%.%hh%.%min%.%ss%.%ms%
    REM --- end set a temp header to date and time
    goto :eof
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!