Skip to content

Instantly share code, notes, and snippets.

@Orangestar12
Created August 5, 2023 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Orangestar12/c6cc0116b59f952b3b24fea8dcbd9796 to your computer and use it in GitHub Desktop.
Save Orangestar12/c6cc0116b59f952b3b24fea8dcbd9796 to your computer and use it in GitHub Desktop.
Split audio tracks out of a video file with multiple audio tracks. Intended for use when editing videos with Blender.
import subprocess
import sys
import json
if len(sys.argv) == 1: # no drag and drop
print('This script needs at least 1 additional argument. All arguments must be a list of files to split.')
exit()
for video in sys.argv:
if video == sys.argv[0]: continue # first argv is always the script itself
shortVideo = video[video.rfind('\\') + 1:]
probe = subprocess.run(["ffprobe", "-hide_banner", "-v", "error", '-show_streams', "-select_streams", "a", "-of", "json", video], capture_output=True, encoding='utf-8')
# print(probe.stdout)
data = json.loads(probe.stdout)
for stream in data['streams']:
index = stream['index']
print(f'Extracting stream {index} from {shortVideo}')
ext = stream['codec_name']
if ext == 'aac':
ext = 'm4a'
if ext == 'vorbis':
ext = 'ogg'
dot = video.rfind('.')
filename = f'{video[:dot]}-{index}.{ext}'
try:
subprocess.run(['ffmpeg', '-hide_banner', '-n', '-loglevel', 'error', '-i', video, '-map', f'0:{index}', '-c:a', 'copy', '-vn', filename], check=True)
print(f'Saved to {filename}')
except subprocess.CalledProcessError:
print('')
print('FFmpeg reported an error while extracting. Details are above.')
print(f"(An incomplete extraction might be saved to {filename}.)")
input('Press enter to continue extracting, or press Ctrl+C or close this window to abort the process.')
print('Process complete.')
print('Any errors from FFmpeg have been printed.')
input('Press enter to continue.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment