Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	File size: 2,863 Bytes
			
			| cb9ee50 c90f138 cb9ee50 c90f138 cb9ee50 c90f138 cb9ee50 c90f138 cb9ee50 c90f138 cb9ee50 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | # Gradio seems to truncate files without keeping the extension, so we need to truncate the file prefix ourself 
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
        # Load source name if not provided
        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:
        # Download from YouTube. This could also be a playlist or a channel.
        output.extend([ AudioSource(x) for x in download_url(urlData, input_audio_max_duration, playlistItems=None) ])
    else:
        # Add input files
        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
    # Calculate total audio length. We do this even if input_audio_max_duration
    # is disabled to ensure that all the audio files are valid.
    for source in output:
        audioDuration = ffmpeg.probe(source.source_path)["format"]["duration"]
        total_duration += float(audioDuration)
        
        # Save audio duration
        source._audio_duration = float(audioDuration)
    # Ensure the total duration of the audio is not too long
    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 a list of audio sources
    return output |