|
|
|
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): |
|
self.source_path = source_path |
|
self.source_name = source_name |
|
|
|
|
|
if (self.source_name is None): |
|
file_path = pathlib.Path(self.source_path) |
|
self.source_name = file_path.name |
|
|
|
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) |
|
|
|
|
|
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 |
|
|