video-ai/chunk_test.py
2025-03-05 19:31:45 +01:00

60 lines
1.9 KiB
Python

import os
import math
from moviepy import VideoFileClip
MAX_FILE_SIZE = 26214400 # 25MB in bytes
CHUNK_SIZE = MAX_FILE_SIZE // 2 # Split into ~12MB chunks
def extract_audio(video_path):
"""Extract audio from video file and return path to audio file."""
print("Extracting audio from video file...")
temp_audio_path = os.path.join(os.path.dirname(video_path), "temp_audio.mp3")
video = VideoFileClip(video_path)
video.audio.write_audiofile(temp_audio_path)
video.close()
return temp_audio_path
def split_audio_file(file_path):
"""Split an audio file into smaller chunks using binary reading."""
chunks_dir = os.path.join(os.path.dirname(file_path), 'chunks')
os.makedirs(chunks_dir, exist_ok=True)
chunks = []
file_size = os.path.getsize(file_path)
total_chunks = math.ceil(file_size / CHUNK_SIZE)
with open(file_path, 'rb') as source_file:
for chunk_number in range(total_chunks):
# Create a file for each chunk in the chunks directory
chunk_filename = os.path.join(chunks_dir, f"chunk_{chunk_number}.mp3")
with open(chunk_filename, 'wb') as chunk_file:
chunk_data = source_file.read(CHUNK_SIZE)
chunk_file.write(chunk_data)
chunks.append(chunk_filename)
return chunks
def main():
FILE_URL = 'E:/obs/Trasform PdC - Architettura Fideuram con focus sulla PdC -20241212- parte I.mp4'
# FILE_URL = 'E:/obs/Trasform PdC - Architettura Fideuram con focus sulla PdC- 20241212 - parte II.mp4'
video_path = FILE_URL #input("Enter the file path of the video file: ")
video_path = video_path.strip('"')
# Extract audio from the video
audio_path = extract_audio(video_path)
# # Split the audio file into chunks
# chunks = split_audio_file(audio_path)
# print("Audio has been split into the following chunks:")
# for chunk in chunks:
# print(chunk)
if __name__ == "__main__":
main()