> On May 29, 2025, at 4:18 PM, Mark Filipak <markfilipak.i...@gmail.com> wrote:
> 
>> Sorry, I should have made it clear that I'm a hobbyist (in case my post 
>> didn't adequately do that for me). Also, I probably shouldn't have included 
>> "-r 24" in my example without further explanation. I have had better audio 
>> sync by using either "-r 24", "-r 23.976" to specify output frame rate, ...
> 
> Specifying output frame rate is necessary only when you want to change frame 
> rate and don't care about results. I believe you are packaging as MKV? MKV 
> has known problems preserving audio sync. There are many reasons why I 
> recommend MP4.

I provided with MKV example for the purpose of simplicity. Most of what I do 
using ffmpeg is almost exclusively MP4 output. 

> 
>> ... but that only helps when I add a frame rate input specification to the 
>> beginning of the filter chain, such as "fps=30000/1001" ("29.97" may be 
>> accepted there instead, but I think it has not always been so). I've also 
>> tried ""-fps_mode vfr" instead of the "-r" specification so I could try 
>> "mpdecimate" after "decimate" to clean up remaining duplicates. I am 
>> becoming convinced the the duplicates that remain after applying "decimate" 
>> result from telecine errors in the source video. If that is true (that I'm 
>> transcoding video that contains flaws), then, for my most commonly 
>> transcoded sources, my best audio sync has been achieved using "-vf 
>> fps=30000/1001,fieldmatch,decimate" in the filter chain, then "-r 23.976" to 
>> specify output frame rate. But that's just my experience.
> 
> '-r' works by dropping and-or duplicating frames, only -- nothing fancy. That 
> may be your entire problem.

Thanks. I'm testing "-fps_mode vfr" instead. I would try "vsync", but last time 
I did, ffmpeg complained. I also remain suspicious of errors in my source 
video. For short passages, it seems there are fewer duplicate frames when I 
follow "fieldmatch, decimate" with "mpdecimate=hi=100:lo=100:frac=1:max=0", 
followed by my crop values (my source videos commonly have black bars). I need 
the variable frame rate to give "mpdecimate" any chance of helping.

>>  for any of you who may be interested in adapting for your own scripting 
>> systems, so I'll be happy to share on request.
> 
> Please do. Sometimes a script is just a script, but sometimes, it's a good 
> cigar.

I'm not sure what the list may do to this, here it is. (I have a blog, but it 
might as well be a journal for all the traffic it gets. I've copied the script 
from there hoping that it will be the same when posted here. I'm including the 
URL in case the AppleScript is damaged too much through posting to be useful:
https://prehensileblog.wordpress.com/2025/05/29/applescript-for-simplifying-small-audio-duration-changes-using-ffmpeg/
Here's an AppleScript droplet I use to make small changes in the duration of 
AC3 audio files so I can fine tune a/v sync for video I transcode. It makes use 
of AppleScript "do shell script" construct to send the command line task to the 
Terminal. It asks the user for the duration in milliseconds by which the audio 
needs to be adjusted by means of the ffmpeg "atempo" command. The script also 
allows the user to specify whether the audio is to be lengthened or shortened 
through the use of the "-" (minus) character. I used to have a pipe method that 
fed the audio from ffmpeg to SoX and back to ffmpeg, which employed a cute 
little animation showing progress, but I've long since abandoned that for the 
sake of efficiency. I'm sure that the script could be used for a lot of 
different audio codecs with very little modification, but the one I've use here 
is AC3. The script is for use on a Silicon Mac where a recent version of ffmpeg 
has been installed natively (the ffmpeg executable should exist in 
"/opt/homebrew/bin/"). There are quite a few notification dialogs in the script 
that may be removed if you prefer. The basic usage for me is to 1) demux to 
separate audio and video component files, 2) convert the audio to AC3 (or 
modify the script to accommodate your audio file type), 3) use the script by 
dropping the audio file onto it's icon or by running the script in Script 
Editor and selecting the file, and 4) remuxing the original video component 
file with the newly created (and hopefully fixed) audio file.
## Begin AppleScript

property newline : ASCII character 10
property tmpfile : "/tmp/execme.command"

on open the_items
    my runtime(the_items)
end open

on runtime(the_items)
    set afile to item 1 of the_items
    set targetstring to (quoted form of POSIX path of afile)
    set targetinfo to (do shell script "/opt/homebrew/bin/ffmpeg -i" & space & 
targetstring & space & "2>&1 | grep \"Duration\"") as text
    set hrs to word 2 of targetinfo
    set mns to word 3 of targetinfo
    set secnds to word 4 of targetinfo
    set hrs2secnds to (hrs * 3600)
    set mns2secnds to (mns * 60)
    set calctime to (hrs2secnds + mns2secnds + secnds)
    
    #set initval to ((calctime as text) & "0")
    
    set initval to (calctime as text)
    
    display dialog "The existing duration of the audio is" & space & initval & 
space & "seconds."
    
    set adjustval to text returned of (display dialog "How much should the 
original audio be changed in seconds? IMPORTANT: If original audio is too 
short, use \"-\"(minus) before the number. Include all three decimal places as 
shown." default answer "5.000")
    
    set tryval to my roundthis((initval - adjustval), 2)
    
    display dialog "The adjusted duration of the audio will be" & space & 
tryval & space & "seconds."
    
    set calcval to (initval / (initval - adjustval) as Unicode text)
    
    display dialog "The tempo will be changed to" & space & calcval & space & 
"of the current tempo (greater than 1 indicates its duration will be decreased, 
less than 1 indicates it will be increased)."
    
    set i to afile as alias
    tell application "Finder" to set P to (i's container as Unicode text)
    set n to (info for i)'s name
    my replace_chars(n, ".ac3", "_adjusted.ac3")
    set output_file to POSIX path of (P & result)
    set outputfile to (quoted form of output_file)
    
    set theShellScript to ("sleep 1" & ";/opt/homebrew/bin/ffmpeg -i" & space & 
targetstring & space & "-af atempo=" & calcval & space & "-acodec ac3 -ab 640k 
-y" & space & outputfile & ";mv" & space & (quoted form of tmpfile) & space & 
(quoted form of (POSIX path of (path totrash)))) as Unicode text
    delay 1
    do shell script "echo " & quoted form of theShellScript & " > " & tmpfile
    
    repeat
        try
            do shell script "chmod +x " & tmpfile
            do shell script "open -a Terminal.app" & space & tmpfile
            exit repeat
        end try
    end repeat
end runtime

on replace_chars(this_text, _bad, _good)
    set AppleScript's text item delimiters to the _bad
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the _good as text
    set this_text to the item_list as text
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars

on roundthis(n, numDecimals)
    set x to 10 ^ numDecimals
    (((n * x) + 0.5) div 1) / x
end roundthis

on run
    try
        runtime(the_items)
    on error
        set the_items to ((choose file with prompt "Please choose the AC3 file 
for which you want to change duration:") as list)
        runtime(the_items)
    end try
end run

## End AppleScript



L. Lee
_______________________________________________
ffmpeg-user mailing list
ffmpeg-user@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
ffmpeg-user-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to