Skip to content

Instantly share code, notes, and snippets.

@elbruno
Created July 26, 2024 18:45
Show Gist options
  • Save elbruno/e8fe05f18fe66b0c7545fd99f253baea to your computer and use it in GitHub Desktop.
Save elbruno/e8fe05f18fe66b0c7545fd99f253baea to your computer and use it in GitHub Desktop.
removeaudiofromvideo.py
# Copyright (c) 2024
# Author : Bruno Capuano
# Change Log :
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Prerequisites
# 1st install moviepy library using command below:
# pip install moviepy
import os
from moviepy.editor import VideoFileClip
# Define the starting directory
starting_directory = "f:\\Temp\\Video Files"
# Check if a file is a video
def is_video(file_path):
video_extensions = ['.mp4', '.mov', '.avi', '.mkv', '.flv', '.wmv']
return any(file_path.lower().endswith(ext) for ext in video_extensions)
# Remove audio from video
def remove_audio_from_video(file_path):
try:
video = VideoFileClip(file_path)
video_no_audio = video.without_audio()
new_file_path = file_path.rsplit('.', 1)[0] + "-no audio." + file_path.rsplit('.', 1)[1]
video_no_audio.write_videofile(new_file_path, codec='libx264')
video.close()
print(f"Processed video: {file_path}")
except Exception as e:
print(f"Failed to process video: {file_path}. Error: {e}")
# Iterate through the files in the starting directory
for root, dirs, files in os.walk(starting_directory):
for file in files:
file_path = os.path.join(root, file)
if is_video(file_path):
remove_audio_from_video(file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment