import os import sys import ffmpeg def extract_audio_from_videos(input_folder, output_folder): # Create output folder if it doesn't exist if not os.path.exists(output_folder): os.makedirs(output_folder) # Common video extensions (you can add more if needed) video_extensions = ('.mp4', '.avi', '.mkv', '.mov', '.wmv', '.flv', '.webm') # Iterate through all files in the input folder for filename in os.listdir(input_folder): if filename.lower().endswith(video_extensions): video_path = os.path.join(input_folder, filename) try: # Create output filename (replace video extension with .m4a for AAC) output_filename = os.path.splitext(filename)[0] + '.m4a' output_path = os.path.join(output_folder, output_filename) # Use ffmpeg to extract audio as AAC stream = ffmpeg.input(video_path) stream = ffmpeg.output(stream, output_path, acodec='aac', vn=True, format='ipod') ffmpeg.run(stream) print(f"Extracted audio from {filename} to {output_filename}") except ffmpeg.Error as e: print(f"Error processing {filename}: {str(e)}") if __name__ == "__main__": os.umask(0) # Check if input folder is provided as command-line argument if len(sys.argv) != 2: print("Usage: python3 extract_audio_ffmpeg.py ") sys.exit(1) # Get input folder from command-line argument input_folder = sys.argv[1] # Validate input folder if not os.path.isdir(input_folder): print(f"Error: {input_folder} is not a valid directory") sys.exit(1) # Set output folder (same level as input folder, named 'audio_output') output_folder = os.path.join(os.path.dirname(input_folder), ".") extract_audio_from_videos(input_folder, output_folder)