Created
March 15, 2024 13:12
-
-
Save springcoil/242183ff5bbe9e38925cd4d198e32850 to your computer and use it in GitHub Desktop.
How to turn a CSV into audio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import audiostack | |
import csv | |
import os | |
audiostack.api_key = os.environ["AUDIOSTACK_API_KEY"] | |
VOICE = "narration_wade" | |
def read_large_csv(file_path): | |
""" | |
Reads a large CSV file and returns a nested list containing the rows and columns. | |
Args: | |
file_path (str): The path to the CSV file. | |
Returns: | |
list: A nested list containing the rows and columns of the CSV file. | |
""" | |
data = [] | |
# Open the CSV file and read its contents | |
with open(file_path, "r", encoding="utf-8") as file: | |
reader = csv.reader(file) | |
# Iterate over each row in the CSV file | |
for row in reader: | |
data.append(row) | |
return data | |
# Combine your video and audio and pass the scriptId dynamically | |
content = read_large_csv("sample_2.csv") | |
def audiostack_create(): | |
for i in range(len(content)): | |
my_str = " ".join(map(str, content[i])) | |
script_content = f""" | |
<as:section name="main" soundsegment="main"> | |
{my_str.replace(os.linesep, " ")} | |
</as:section> | |
""" | |
script_content = script_content.replace("\n", "").replace( | |
" ", "" | |
) | |
print(script_content) | |
script = audiostack.Content.Script.create( | |
scriptText=script_content, | |
scriptName=i, | |
moduleName="dynamicVoiceOver", | |
projectName="dynamicVoiceOver", | |
) | |
print(script) | |
speech = audiostack.Speech.TTS.create(scriptItem=script, voice=VOICE) | |
file_name = f"{i+1}th_top_streamed_song__{VOICE}_{i}" | |
mix = audiostack.Production.Mix.create( | |
speechItem=speech, soundTemplate="sound_affects" | |
) | |
mix.download(fileName=file_name) | |
current_directory = os.path.dirname(os.path.abspath(__file__)) | |
print(f"File downloaded to: {current_directory}/{file_name}.wav") | |
delivery = audiostack.Delivery.Encoder.encode_mix( | |
productionItem=mix, | |
preset="mp3_high", | |
public=True, | |
) | |
print("MP3 file URL:", delivery.url) | |
if __name__ == "__main__": | |
audiostack_create() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment