Crear clips para tiktok a partir de links de videos de youtube. Los clips seran de los momentos mas vistos del video de youtube.
To upload files, please first save the app
import streamlit as st
import yt_dlp
from moviepy.editor import VideoFileClip
import os
import tempfile
st.title("YouTube to TikTok Clipper")
st.write("Create TikTok clips from YouTube videos most viewed moments")
# Input for YouTube URL
youtube_url = st.text_input("Enter YouTube video URL")
# Duration selector for clip
clip_duration = st.slider("Select clip duration (seconds)", min_value=5, max_value=60, value=15)
if youtube_url and st.button("Generate Clip"):
try:
with st.spinner("Processing video..."):
# Create a temporary directory to store files
temp_dir = tempfile.mkdtemp()
output_path = os.path.join(temp_dir, "output.mp4")
# Download YouTube video
ydl_opts = {
'format': 'best[height<=1080]',
'outtmpl': os.path.join(temp_dir, 'video.mp4'),
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
# Get video info
info = ydl.extract_info(youtube_url, download=True)
video_duration = info.get('duration')
title = info.get('title')
st.write(f"Video Title: {title}")
st.write(f"Duration: {video_duration} seconds")
# For this example, we'll take a clip from the middle of the video
# In a real application, you would need to analyze view statistics
# to find the most viewed moments
start_time = (video_duration - clip_duration) // 2
# Load the video and create clip
with VideoFileClip(os.path.join(temp_dir, 'video.mp4')) as video:
# Create clip
clip = video.subclip(start_time, start_time + clip_duration)
# Resize for TikTok (1080x1920)
clip_resized = clip.resize(height=1920)
# Write the final clip
clip_resized.write_videofile(
output_path,
codec='libx264',
audio_codec='aac'
)
# Display the clip
with open(output_path, 'rb') as f:
video_bytes = f.read()
st.video(video_bytes)
# Download button
st.download_button(
label="Download TikTok Clip",
data=video_bytes,
file_name="tiktok_clip.mp4",
mime="video/mp4"
)
except Exception as e:
st.error(f"An error occurred: {str(e)}")
finally:
# Cleanup temporary files
if 'temp_dir' in locals():
import shutil
shutil.rmtree(temp_dir)
st.markdown("""
### Instructions:
1. Paste a YouTube video URL
2. Select the desired clip duration
3. Click "Generate Clip" to create a TikTok-formatted video
4. Download the clip and upload it to TikTok
Note: This demo creates a clip from the middle of the video. A full version would analyze view statistics to find the most engaging moments.
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?