|
|
|
import os |
|
import pathlib |
|
from typing import List |
|
import zipfile |
|
|
|
import ffmpeg |
|
from more_itertools import unzip |
|
|
|
from src.download import ExceededMaximumDuration, download_url |
|
|
|
MAX_FILE_PREFIX_LENGTH = 17 |
|
|
|
class AudioSource: |
|
def __init__(self, source_path, source_name = None, audio_duration = None): |
|
self.source_path = source_path |
|
self.source_name = source_name |
|
self._audio_duration = audio_duration |
|
|
|
|
|
if (self.source_name is None): |
|
file_path = pathlib.Path(self.source_path) |
|
self.source_name = file_path.name |
|
|
|
def get_audio_duration(self): |
|
if self._audio_duration is None: |
|
self._audio_duration = float(ffmpeg.probe(self.source_path)["format"]["duration"]) |
|
|
|
return self._audio_duration |
|
|
|
def get_full_name(self): |
|
return self.source_name |
|
|
|
def get_short_name(self, max_length: int = MAX_FILE_PREFIX_LENGTH): |
|
file_path = pathlib.Path(self.source_name) |
|
short_name = file_path.stem[:max_length] + file_path.suffix |
|
|
|
return short_name |
|
|
|
def __str__(self) -> str: |
|
return self.source_path |
|
|
|
class AudioSourceCollection: |
|
def __init__(self, sources: List[AudioSource]): |
|
self.sources = sources |
|
|
|
def __iter__(self): |
|
return iter(self.sources) |
|
|
|
def get_audio_source_collection(urlData: str, multipleFiles: List, microphoneData: str, input_audio_max_duration: float = -1) -> List[AudioSource]: |
|
output: List[AudioSource] = [] |
|
|
|
if urlData: |
|
|
|
output.extend([ AudioSource(x) for x in download_url(urlData, input_audio_max_duration, playlistItems=None) ]) |
|
else: |
|
|
|
if (multipleFiles is not None): |
|
output.extend([ AudioSource(x.name) for x in multipleFiles ]) |
|
if (microphoneData is not None): |
|
output.append(AudioSource(microphoneData)) |
|
|
|
total_duration = 0 |
|
|
|
|
|
|
|
for source in output: |
|
audioDuration = ffmpeg.probe(source.source_path)["format"]["duration"] |
|
total_duration += float(audioDuration) |
|
|
|
|
|
source._audio_duration = float(audioDuration) |
|
|
|
|
|
if input_audio_max_duration > 0: |
|
if float(total_duration) > input_audio_max_duration: |
|
raise ExceededMaximumDuration(videoDuration=total_duration, maxDuration=input_audio_max_duration, message="Video(s) is too long") |
|
|
|
|
|
return output |