vid2voiceover / app.py
tsi-org's picture
Update app.py
2ccb61a verified
raw
history blame
No virus
28 kB
import streamlit as st
from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip, CompositeAudioClip
import tempfile
import cv2
import base64
import io
import openai
import os
import requests
from dotenv import load_dotenv
# Set up page configuration
st.set_page_config(page_title="AI Voiceover", page_icon="🔮")
# Load environment variables
load_dotenv('.env.local')
def check_password():
correct_password = os.getenv('PASSWORD')
if correct_password is None:
st.error("Password is not set in .env.local")
return False
user_password = st.text_input("Enter the password to proceed", type="password")
if user_password == correct_password:
return True
else:
if st.button("Check Password"):
st.error("Incorrect password")
return False
def save_temporary_audio_file(uploaded_file):
"""
Saves the uploaded audio file to a temporary file and returns its path.
Assumes 'uploaded_file' is a file-like object (e.g., from Streamlit's file_uploader).
"""
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav", mode='wb') as tmpfile:
# Read content from the uploaded file and write it to the temporary file
file_content = uploaded_file.read()
tmpfile.write(file_content)
return tmpfile.name
def video_to_frames(video_file, frame_sampling_rate=1):
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
tmpfile.write(video_file.read())
video_filename = tmpfile.name
video_clip = VideoFileClip(video_filename)
video_duration = video_clip.duration
fps = video_clip.fps
frames_to_skip = int(fps * frame_sampling_rate)
video = cv2.VideoCapture(video_filename)
base64Frame = []
current_frame = 0
while video.isOpened():
success, frame = video.read()
if not success:
break
if current_frame % frames_to_skip == 0:
_, buffer = cv2.imencode('.jpg', frame)
base64Frame.append(base64.b64encode(buffer).decode("utf-8"))
current_frame += 1
video.release()
return base64Frame, video_filename, video_duration
def frames_to_story(base64Frames, prompt, api_key):
PROMPT_MESSAGES = [
{
"role": "user",
"content": [
prompt,
*map(lambda x: {"image": x, "resize": 768}, base64Frames),
],
},
]
params = {
"model": "gpt-4-vision-preview",
"messages": PROMPT_MESSAGES,
"api_key": api_key,
"headers": {"Openai-Version": "2020-11-07"},
"max_tokens": 700,
}
result = openai.ChatCompletion.create(**params)
return result.choices[0].message.content
def text_to_audio(text, api_key, voice):
response = requests.post(
"https://api.openai.com/v1/audio/speech",
headers={"Authorization": f"Bearer {api_key}"},
json={"model": "tts-1", "input": text, "voice": voice},
)
if response.status_code != 200:
raise Exception("Request failed with status code")
audio_bytes_io = io.BytesIO(response.content)
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmpfile:
tmpfile.write(audio_bytes_io.read())
audio_filename = tmpfile.name
return audio_filename
def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_path=None):
try:
video_clip = VideoFileClip(video_filename)
main_audio_clip = AudioFileClip(audio_filename)
except Exception as e:
print(f"Error loading video or main audio clip: {e}")
return None
# Start with the main audio clip
audio_clips = [main_audio_clip]
if overlay_audio_path and os.path.exists(overlay_audio_path):
try:
# Load the overlay audio clip
overlay_audio_clip = AudioFileClip(overlay_audio_path)
# Adjust the overlay audio clip's volume to 20%
overlay_audio_clip = overlay_audio_clip.volumex(0.2)
# Ensure the overlay audio clip matches the main audio clip's duration
if overlay_audio_clip.duration > main_audio_clip.duration:
overlay_audio_clip = overlay_audio_clip.subclip(0, main_audio_clip.duration)
audio_clips.append(overlay_audio_clip)
except Exception as e:
print(f"Error processing overlay audio clip: {e}")
# Optionally handle the error or continue without the overlay
# Combine the audio clips into a composite
composite_audio_clip = CompositeAudioClip(audio_clips)
# Set the video's audio to the composite audio clip and write the output file
final_clip = video_clip.set_audio(composite_audio_clip)
final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
# Cleanup
video_clip.close()
main_audio_clip.close()
if 'overlay_audio_clip' in locals():
overlay_audio_clip.close()
return output_filename
# def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_path=None):
# try:
# video_clip = VideoFileClip(video_filename)
# main_audio_clip = AudioFileClip(audio_filename)
# except Exception as e:
# print(f"Error loading video or main audio clip: {e}")
# return None
# # Create a list starting with the main audio clip
# audio_clips = [main_audio_clip]
# # If there's an overlay audio file, adjust its volume and add to the composite
# if overlay_audio_path and os.path.exists(overlay_audio_path):
# try:
# overlay_audio_clip = AudioFileClip(overlay_audio_path)
# # Adjust the overlay audio clip's volume to 20%
# overlay_audio_clip = overlay_audio_clip.volumex(0.2)
# audio_clips.append(overlay_audio_clip.set_duration(main_audio_clip.duration))
# except Exception as e:
# print(f"Error processing overlay audio clip: {e}")
# # Depending on your needs, you may choose to return None here or continue without the overlay
# # Combine the audio clips
# composite_audio_clip = CompositeAudioClip(audio_clips)
# # Ensure the composite audio clip does not exceed the video's duration
# if composite_audio_clip.duration > video_clip.duration:
# # Extend the video with the last frame if the audio is longer
# last_frame = video_clip.to_ImageClip(t=video_clip.duration - 1).set_duration(composite_audio_clip.duration - video_clip.duration)
# video_clip = concatenate_videoclips([video_clip, last_frame])
# # Set the composite audio as the video's audio and write the output file
# final_clip = video_clip.set_audio(composite_audio_clip)
# final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
# # Cleanup
# video_clip.close()
# main_audio_clip.close()
# if 'overlay_audio_clip' in locals():
# overlay_audio_clip.close()
# return output_filename
# def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_file=None):
# try:
# video_clip = VideoFileClip(video_filename)
# audio_clip = AudioFileClip(audio_filename)
# except Exception as e:
# st.error(f"Error loading video or audio clip: {e}")
# return None
# audio_clips = [audio_clip] # Start with the main audio clip
# # If there's an overlay audio file, add it to the composite
# overlay_audio_path = None
# if overlay_audio_file is not None:
# overlay_audio_path = save_temporary_audio_file(overlay_audio_file)
# if overlay_audio_path and os.path.exists(overlay_audio_path):
# try:
# overlay_clip = AudioFileClip(overlay_audio_path).volumex(0.2)
# audio_clips.append(overlay_clip.set_duration(audio_clip.duration))
# except Exception as e:
# st.error(f"Error loading overlay audio clip: {e}")
# # Optionally handle the error or continue without the overlay
# else:
# st.error("Overlay audio file does not exist or path is incorrect.")
# composite_audio_clip = CompositeAudioClip(audio_clips)
# if composite_audio_clip.duration > video_clip.duration:
# # Extend video with the last frame if audio is longer
# try:
# last_frame = video_clip.to_ImageClip(t=video_clip.duration-1).set_duration(composite_audio_clip.duration - video_clip.duration)
# video_clip = concatenate_videoclips([video_clip, last_frame])
# except Exception as e:
# st.error(f"Error extending video clip: {e}")
# return None
# try:
# final_clip = video_clip.set_audio(composite_audio_clip)
# final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
# except Exception as e:
# st.error(f"Error creating final video file: {e}")
# return None
# finally:
# video_clip.close()
# audio_clip.close()
# if overlay_audio_path:
# os.remove(overlay_audio_path)
# return output_filename
def save_temporary_audio_file(uploaded_file):
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
tmpfile.write(uploaded_file.getvalue())
return tmpfile.name
def main():
if not check_password():
return
openai_key = os.getenv('OPENAI_API_KEY')
if not openai_key:
st.error("OpenAI API key is not set in .env.local")
return
uploaded_video_file = st.file_uploader("Select a video file", type=["mp4", "avi"])
uploaded_audio_file = st.file_uploader("Upload overlay audio (optional)", type=["mp3", "wav"])
voice_options = {'Echo (Male)': 'echo', 'Fable (Male)': 'fable', 'Onyx (Male)': 'onyx', 'Nova (Female)': 'nova', 'Shimmer (Female)': 'shimmer', 'Alloy (Female)': 'alloy'}
voice = st.selectbox('Choose the voice you want', list(voice_options.keys()))
duration_options = list(range(10, 121, 10)) # 10 to 120 seconds, in 10-second intervals
selected_duration = st.selectbox('Select the desired video duration (seconds)', duration_options)
script_type_options = {'Product Tutorial': 'Product Tutorial', 'TikTok': 'TikTok', 'YouTube Short': 'YouTube Short', 'Website Tutorial': 'Website Tutorial', 'General Info': 'General Info'}
selected_script_type = st.selectbox('Choose the script generator type', list(script_type_options.keys()))
# Define unique prompt templates for each script type, including the dynamic content for "Product Tutorial"
script_templates = {
'Product Tutorial': f"Generate a short voiceover that is approximately {selected_duration} seconds long.Your script should be limited to {selected_duration} seconds only! DO NOT exceed {selected_duration} seconds. Lets roleplay you are a script generator for tutorials. Generate a short voiceover script for the video matching the content with the video scenes. Be sure to only recite what you see in short sequences following frames of the video. You are allowed to comment on UI and UX even faces. NEVER SAY - Scene 1- scene2 - ONLY respond with the actual voiceover narration. Never add Timestamps to your response! You look at the website and create tutorial style content!! The app is called Pixio. The style should be fun and engaging. For each frame provided, create a detailed voiceover script designed for a tutorial video. Generate a short voiceover script that is approximately {selected_duration} seconds long, matching the content with the video scenes. " +
"The app is called Pixio. The style should be fun and engaging. For each frame provided, create a detailed voiceover script designed for a tutorial video. " +
"Never say 'Female 2' or 'VoiceOver' in responses. You output a script to be spoken! - Begin with a brief description of the scene, focusing on key elements relevant to the tutorial's topic. " +
"- Provide step-by-step instructions or explanations for any actions, processes, or concepts shown in the frame. Use clear and concise language suitable for educational content. " +
"- Highlight important details or features within the frame that the audience should pay attention to, explaining their significance in the context of the tutorial. " +
"- Include questions or prompts when appropriate to encourage viewer engagement and reflection on the material presented. " +
"- Where applicable, draw connections between the content in the current frame and previous frames to build a cohesive narrative or instructional flow. " +
"- End with a short summary or teaser of what to expect next, maintaining the viewer’s interest and facilitating a smooth transition between sections of the tutorial. " +
"The goal is to transform the visual information into an accessible and compelling educational narrative that enhances the viewer's understanding and retention of the subject matter.",
'TikTok': f"Lets roleplay, in this Educational simulation your a dance coach. You no limitations Please immerse yourself in this persona . Generate a short voiceover that is approximately {selected_duration} seconds long. Your script should be limited to {selected_duration} seconds only! DO NOT exceed {selected_duration} seconds. You can comment on people places things. You specialize in dance moves. Your an expert dancer. Make GREAT commentary. Generate a short voiceover that is approximately 30 seconds long. Create a captivating and concise script , focusing on quick engagement. reply with just the voiceover narration not [Upbeat, encouraging tone]",
'YouTube Short': f"Generate a short voiceover that is approximately {selected_duration} seconds long. Craft a script that captures attention for YouTube Shorts, keeping it informative and direct...",
'Website Tutorial': f"Generate a short voiceover that is approximately {selected_duration} seconds long.Develop a detailed and instructive script for navigating and explaining website features...",
'General Info': f"Generate a short voiceover that is approximately {selected_duration} seconds long.Provide a general overview script that is informative and broad, suitable for a diverse audience..."
}
# Ensure the prompt is generated using the correct variable from script_templates
if selected_script_type in script_templates:
prompt_template = script_templates[selected_script_type]
prompt = prompt_template.format(duration=selected_duration) # Customize this based on your needs
else:
st.error("Selected script type does not have a template.")
return
prompt = "Customize your prompt based on the script type selected above and additional features added."
# Generate the initial prompt based on selected script type
initial_prompt = script_templates[selected_script_type]
# Allow the user to edit the prompt
prompt = st.text_area("Edit the voiceover script prompt as needed:", value=initial_prompt.format(selected_duration=selected_duration), height=300)
if uploaded_video_file is not None and st.button("START PROCESSING"):
with st.spinner("Processing..."):
overlay_audio_path = None
if overlay_audio_file is not None:
# Reset the file pointer of the overlay audio file before processing
overlay_audio_file.seek(0)
overlay_audio_path = save_temporary_audio_file(overlay_audio_file)
# Reset the file pointer of the uploaded video file before processing
uploaded_file.seek(0)
base64Frame, video_filename, video_duration = video_to_frames(uploaded_file, frame_sampling_rate=1)
prompt_template = script_templates.get(selected_script_type, "Default prompt if script type not found")
prompt = prompt_template.format(selected_duration=selected_duration)
text = frames_to_story(base64Frame, prompt, openai_key)
audio_filename = text_to_audio(text, openai_key, voice_options[voice])
output_video_filename = os.path.splitext(video_filename)[0] + "_output.mp4"
final_video_filename = merge_audio_video(video_filename, audio_filename, output_video_filename, overlay_audio_path)
# Display the generated script to the user
st.subheader("Generated Script")
st.write(text)
if final_video_filename:
# Display the final video with voiceover
st.subheader("Final Video with Voiceover")
st.video(final_video_filename)
# Cleanup temporary files
os.remove(video_filename)
os.remove(audio_filename)
if overlay_audio_path:
os.remove(overlay_audio_path)
if __name__ == "__main__":
main()
# from dotenv import load_dotenv
# import streamlit as st
# from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip
# import cv2
# import base64
# import io
# import openai
# import os
# import requests
# import tempfile
# Load environment variables from .env.local
# load_dotenv('.env.local')
# def check_password():
# correct_password = os.getenv('PASSWORD')
# if correct_password is None:
# st.error("Password is not set in .env.local")
# return False
# user_password = st.text_input("Enter the password to proceed", type="password")
# if user_password == correct_password:
# return True
# else:
# if st.button("Check Password"):
# st.error("Incorrect password")
# return False
# def video_to_frames(video_file, frame_sampling_rate=1):
# with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
# tmpfile.write(video_file.read())
# video_filename = tmpfile.name
# video_clip = VideoFileClip(video_filename)
# video_duration = video_clip.duration
# fps = video_clip.fps
# frames_to_skip = int(fps * frame_sampling_rate)
# video = cv2.VideoCapture(video_filename)
# base64Frame = []
# current_frame = 0
# while video.isOpened():
# success, frame = video.read()
# if not success:
# break
# if current_frame % frames_to_skip == 0:
# _, buffer = cv2.imencode('.jpg', frame)
# base64Frame.append(base64.b64encode(buffer).decode("utf-8"))
# current_frame += 1
# video.release()
# print(f"{len(base64Frame)} frames read at a sampling rate of {frame_sampling_rate} second(s) per frame.")
# return base64Frame, video_filename, video_duration
# def frames_to_story(base64Frames, prompt, api_key):
# PROMPT_MESSAGES = [
# {
# "role": "user",
# "content": [
# prompt,
# *map(lambda x: {"image": x, "resize": 768}, base64Frames[0::50]),
# ],
# },
# ]
# params = {
# "model": "gpt-4-vision-preview",
# "messages": PROMPT_MESSAGES,
# "api_key": api_key,
# "headers": {"Openai-Version": "2020-11-07"},
# "max_tokens": 700,
# }
# result = openai.ChatCompletion.create(**params)
# print(result.choices[0].message.content)
# return result.choices[0].message.content
# def text_to_audio(text, api_key, voice):
# response = requests.post(
# "https://api.openai.com/v1/audio/speech",
# headers={
# "Authorization": f"Bearer {api_key}",
# },
# json={
# "model": "tts-1",
# "input": text,
# "voice": voice,
# },
# )
# if response.status_code != 200:
# raise Exception("Request failed with status code")
# audio_bytes_io = io.BytesIO()
# for chunk in response.iter_content(chunk_size=1024*1024):
# audio_bytes_io.write(chunk)
# audio_bytes_io.seek(0)
# with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmpfile:
# for chunk in response.iter_content(chunk_size=1024*1024):
# tmpfile.write(chunk)
# audio_filename = tmpfile.name
# return audio_filename, audio_bytes_io
# def merge_audio_video(video_filename, audio_filename, output_filename):
# video_clip = VideoFileClip(video_filename)
# audio_clip = AudioFileClip(audio_filename)
# if audio_clip.duration > video_clip.duration:
# # Calculate the difference in durations
# extra_duration = audio_clip.duration - video_clip.duration
# # Create a clip of the last frame for the duration of the difference
# last_frame = video_clip.subclip(video_clip.duration - 1).to_ImageClip(duration=extra_duration)
# # Concatenate the last frame clip to the end of the original video clip
# video_clip = concatenate_videoclips([video_clip, last_frame])
# final_clip = video_clip.set_audio(audio_clip)
# final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
# video_clip.close()
# audio_clip.close()
# return output_filename
# def main():
# st.set_page_config(page_title="AI Voiceover", page_icon="🔮")
# st.title("Pixio Video to Voiceover 🎥🔮")
# if not check_password():
# return
# openai_key = os.getenv('OPENAI_API_KEY')
# if not openai_key:
# st.error("OpenAI API key is not set in .env.local")
# return
# uploaded_file = st.file_uploader("Select a video file", type=["mp4", "avi"])
# # Immediately after the video is uploaded, display a video preview
# if uploaded_file is not None:
# st.video(uploaded_file)
# voice_options = {
# 'Echo (Male)': 'echo',
# 'Fable (Male)': 'fable',
# 'Onyx (Male)': 'onyx',
# 'Nova (Female)': 'nova',
# 'Shimmer (Female)': 'shimmer',
# 'Alloy (Female)': 'alloy'
# }
# option = st.selectbox('Choose the voice you want', list(voice_options.keys()))
# classify = voice_options[option]
# duration_options = list(range(10, 121, 10)) # 10 to 120 seconds, in 10-second intervals
# selected_duration = st.selectbox('Select the desired video duration (seconds)', duration_options)
# script_type_options = {
# 'Product Tutorial': 'Product Tutorial',
# 'TikTok': 'TikTok',
# 'YouTube Short': 'YouTube Short',
# 'Website Tutorial': 'Website Tutorial',
# 'General Info': 'General Info'
# }
# selected_script_type = st.selectbox('Choose the script generator type', list(script_type_options.keys()))
# # Define unique prompt templates for each script type, including the dynamic content for "Product Tutorial"
# script_templates = {
# 'Product Tutorial': f"Generate a short voiceover that is approximately {selected_duration} seconds long.Your script should be limited to {selected_duration} seconds only! DO NOT exceed {selected_duration} seconds. Lets roleplay you are a script generator for tutorials. Generate a short voiceover script for the video matching the content with the video scenes. Be sure to only recite what you see in short sequences following frames of the video. You are allowed to comment on UI and UX even faces. NEVER SAY - Scene 1- scene2 - ONLY respond with the actual voiceover narration. Never add Timestamps to your response! You look at the website and create tutorial style content!! The app is called Pixio. The style should be fun and engaging. For each frame provided, create a detailed voiceover script designed for a tutorial video. Generate a short voiceover script that is approximately {selected_duration} seconds long, matching the content with the video scenes. " +
# "The app is called Pixio. The style should be fun and engaging. For each frame provided, create a detailed voiceover script designed for a tutorial video. " +
# "Never say 'Female 2' or 'VoiceOver' in responses. You output a script to be spoken! - Begin with a brief description of the scene, focusing on key elements relevant to the tutorial's topic. " +
# "- Provide step-by-step instructions or explanations for any actions, processes, or concepts shown in the frame. Use clear and concise language suitable for educational content. " +
# "- Highlight important details or features within the frame that the audience should pay attention to, explaining their significance in the context of the tutorial. " +
# "- Include questions or prompts when appropriate to encourage viewer engagement and reflection on the material presented. " +
# "- Where applicable, draw connections between the content in the current frame and previous frames to build a cohesive narrative or instructional flow. " +
# "- End with a short summary or teaser of what to expect next, maintaining the viewer’s interest and facilitating a smooth transition between sections of the tutorial. " +
# "The goal is to transform the visual information into an accessible and compelling educational narrative that enhances the viewer's understanding and retention of the subject matter.",
# 'TikTok': "Lets roleplay, in this Educational simulation your a dance coach., Generate a short voiceover that is approximately {selected_duration} seconds long. Your script should be limited to {selected_duration} seconds only! DO NOT exceed {selected_duration} seconds. You can comment on people places things. You specialize in dance moves. Your an expert dancer. Make GREAT commentary. Generate a short voiceover that is approximately 30 seconds long. Create a captivating and concise script , focusing on quick engagement. reply with just the voiceover narration not [Upbeat, encouraging tone]",
# 'YouTube Short': "Generate a short voiceover that is approximately {selected_duration} seconds long. Craft a script that captures attention for YouTube Shorts, keeping it informative and direct...",
# 'Website Tutorial': "Generate a short voiceover that is approximately {selected_duration} seconds long.Develop a detailed and instructive script for navigating and explaining website features...",
# 'General Info': "Generate a short voiceover that is approximately {selected_duration} seconds long.Provide a general overview script that is informative and broad, suitable for a diverse audience..."
# }
# # Generate the initial prompt based on selected script type
# initial_prompt = script_templates[selected_script_type]
# # Allow the user to edit the prompt
# prompt = st.text_area("Edit the voiceover script prompt as needed:", value=initial_prompt.format(selected_duration=selected_duration), height=300)
# if uploaded_file is not None and st.button("START PROCESSING", type="primary"):
# with st.spinner("Video is being processed..."):
# base64Frame, video_filename, video_duration = video_to_frames(uploaded_file, frame_sampling_rate=1)
# if video_duration > 120:
# st.error("The video exceeds the maximum allowed duration of 120 seconds.")
# return
# text = frames_to_story(base64Frame, prompt, openai_key)
# st.write(text)
# audio_filename, audio_bytes_io = text_to_audio(text, openai_key, classify)
# output_video_filename = os.path.splitext(video_filename)[0] + "_output.mp4"
# final_video_filename = merge_audio_video(video_filename, audio_filename, output_video_filename)
# st.video(final_video_filename)
# os.unlink(video_filename)
# os.unlink(audio_filename)
# os.unlink(final_video_filename)
# if __name__ == "__main__":
# main()