File size: 2,554 Bytes
b0716df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import moviepy.editor as mp
import tempfile
import os

def main():
    st.title("Simple Video Editor")

    # File uploaders
    video_file = st.file_uploader("Upload a video file (MP4)", type=["mp4"])
    audio_file = st.file_uploader("Upload an audio file (MP3 or WAV)", type=["mp3", "wav"])

    if video_file and audio_file:
        # Save uploaded files to temporary locations
        temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
        temp_video.write(video_file.read())
        temp_video_path = temp_video.name

        temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=f".{audio_file.name.split('.')[-1]}")
        temp_audio.write(audio_file.read())
        temp_audio_path = temp_audio.name

        # Load video and audio
        video = mp.VideoFileClip(temp_video_path)
        audio = mp.AudioFileClip(temp_audio_path)

        # Display video info
        st.write(f"Video duration: {video.duration:.2f} seconds")
        st.write(f"Audio duration: {audio.duration:.2f} seconds")

        # Trim video
        st.subheader("Trim Video")
        video_start = st.slider("Video start time (seconds)", 0.0, video.duration, 0.0)
        video_end = st.slider("Video end time (seconds)", 0.0, video.duration, video.duration)
        trimmed_video = video.subclip(video_start, video_end)

        # Trim audio
        st.subheader("Trim Audio")
        audio_start = st.slider("Audio start time (seconds)", 0.0, audio.duration, 0.0)
        audio_end = st.slider("Audio end time (seconds)", 0.0, audio.duration, audio.duration)
        trimmed_audio = audio.subclip(audio_start, audio_end)

        # Combine video and audio
        if st.button("Combine Video and Audio"):
            final_clip = trimmed_video.set_audio(trimmed_audio)
            
            # Save the final video
            output_path = "output_video.mp4"
            final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac")

            # Provide download link
            st.success("Video processing complete!")
            with open(output_path, "rb") as file:
                btn = st.download_button(
                    label="Download processed video",
                    data=file,
                    file_name="processed_video.mp4",
                    mime="video/mp4"
                )

        # Clean up temporary files
        video.close()
        audio.close()
        os.unlink(temp_video_path)
        os.unlink(temp_audio_path)

if __name__ == "__main__":
    main()