Last active
April 21, 2024 17:30
-
-
Save ParityError/63c74bb424df5480a67f7d67a6f10320 to your computer and use it in GitHub Desktop.
ffmpeg audio/video sync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
def extract_audio_ffmpeg(input_file, output_file, start_time, end_time): | |
command = [ | |
'ffmpeg', | |
'-i', input_file, | |
'-ss', start_time, # Start time in HH:MM:SS format | |
'-to', end_time, # End time in HH:MM:SS format | |
'-async', '1', # Attempts to correct for any sync issues | |
'-c:a', 'pcm_s16le', # Uncompressed WAV output | |
'-ar', '44100', # Force audio sample rate to 44100 Hz | |
output_file | |
] | |
subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
# Example usage: Adjust times as needed. | |
extract_audio_ffmpeg('input.mp4', 'output.wav', '00:00:00', '00:02:28') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
def extractAudioUsingFFmpeg(inputFile, outputFile, startTimeInSeconds, endTimeInSeconds): | |
command = [ | |
'ffmpeg', | |
'-i', inputFile, | |
'-ss', str(startTimeInSeconds), | |
'-to', str(endTimeInSeconds), | |
'-c:a', 'pcm_s16le', # PCM codec for WAV output | |
'-ar', '44100', # Sample rate in Hz | |
outputFile | |
] | |
subprocess.run(command) | |
# Example usage | |
extractAudioUsingFFmpeg("input.mp4", "output.wav", 0, 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also test/analyze your input files for any discrepancies:
ffmpeg -i input.mp4 -filter_complex "[0:a]ashowinfo" -f null -