Wazzzabeee commited on
Commit
1141764
1 Parent(s): 95fdbe6

[+] V1.0 Youtube Videos

Browse files
Files changed (1) hide show
  1. pages/02_🎥_Input_Youtube_Link.py +94 -2
pages/02_🎥_Input_Youtube_Link.py CHANGED
@@ -1,7 +1,15 @@
 
 
 
 
 
1
  import streamlit as st
 
2
  from streamlit_lottie import st_lottie
 
3
 
4
  from models.deep_colorization.colorizers import eccv16
 
5
  from utils import load_lottieurl, change_model
6
 
7
  st.set_page_config(page_title="Image & Video Colorizer", page_icon="🎨", layout="wide")
@@ -20,7 +28,15 @@ with col2:
20
  st.write("""
21
  ## B&W Videos Colorizer
22
  ##### Input a YouTube black and white video link and get a colorized version of it.
23
- ###### I recommend starting with the first model and then experimenting with the second one.""")
 
 
 
 
 
 
 
 
24
 
25
 
26
  def main():
@@ -33,7 +49,83 @@ def main():
33
 
34
  link = st.text_input("YouTube Link (The longer the video, the longer the processing time)")
35
  if st.button("Colorize"):
36
- st.info('This feature hasn\'t been implemented yet', icon="ℹ️")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
 
39
  if __name__ == "__main__":
 
1
+ import time
2
+
3
+ import cv2
4
+ import moviepy.editor as mp
5
+ import numpy as np
6
  import streamlit as st
7
+ from pytube import YouTube
8
  from streamlit_lottie import st_lottie
9
+ from tqdm import tqdm
10
 
11
  from models.deep_colorization.colorizers import eccv16
12
+ from utils import colorize_frame, format_time
13
  from utils import load_lottieurl, change_model
14
 
15
  st.set_page_config(page_title="Image & Video Colorizer", page_icon="🎨", layout="wide")
 
28
  st.write("""
29
  ## B&W Videos Colorizer
30
  ##### Input a YouTube black and white video link and get a colorized version of it.
31
+ ###### This space is using CPU Basic so it might take a while to colorize a video.
32
+ ###### ➠ If you want more models and GPU available please support this space by donating.""")
33
+
34
+
35
+ @st.cache_data()
36
+ def download_video(link):
37
+ yt = YouTube(link)
38
+ video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(filename="video.mp4")
39
+ return video
40
 
41
 
42
  def main():
 
49
 
50
  link = st.text_input("YouTube Link (The longer the video, the longer the processing time)")
51
  if st.button("Colorize"):
52
+ yt_video = download_video(link)
53
+ print(yt_video)
54
+ col1, col2 = st.columns([0.5, 0.5])
55
+ with col1:
56
+ st.markdown('<p style="text-align: center;">Before</p>', unsafe_allow_html=True)
57
+ st.video(yt_video)
58
+ with col2:
59
+ st.markdown('<p style="text-align: center;">After</p>', unsafe_allow_html=True)
60
+ with st.spinner("Colorizing frames..."):
61
+ # Colorize video frames and store in a list
62
+ output_frames = []
63
+
64
+ audio = mp.AudioFileClip("video.mp4")
65
+ video = cv2.VideoCapture("video.mp4")
66
+
67
+ total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
68
+ fps = video.get(cv2.CAP_PROP_FPS)
69
+
70
+ progress_bar = st.progress(0) # Create a progress bar
71
+ start_time = time.time()
72
+ time_text = st.text("Time Remaining: ") # Initialize text value
73
+
74
+ for _ in tqdm(range(total_frames), unit='frame', desc="Progress"):
75
+ ret, frame = video.read()
76
+ if not ret:
77
+ break
78
+
79
+ colorized_frame = colorize_frame(frame, loaded_model)
80
+ output_frames.append((colorized_frame * 255).astype(np.uint8))
81
+
82
+ elapsed_time = time.time() - start_time
83
+ frames_completed = len(output_frames)
84
+ frames_remaining = total_frames - frames_completed
85
+ time_remaining = (frames_remaining / frames_completed) * elapsed_time
86
+
87
+ progress_bar.progress(frames_completed / total_frames) # Update progress bar
88
+
89
+ if frames_completed < total_frames:
90
+ time_text.text(f"Time Remaining: {format_time(time_remaining)}") # Update text value
91
+ else:
92
+ time_text.empty() # Remove text value
93
+ progress_bar.empty()
94
+
95
+ with st.spinner("Merging frames to video..."):
96
+ frame_size = output_frames[0].shape[:2]
97
+ output_filename = "output.mp4"
98
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v") # Codec for MP4 video
99
+ out = cv2.VideoWriter(output_filename, fourcc, fps, (frame_size[1], frame_size[0]))
100
+
101
+ # Display the colorized video using st.video
102
+ for frame in output_frames:
103
+ frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
104
+
105
+ out.write(frame_bgr)
106
+
107
+ out.release()
108
+
109
+ # Convert the output video to a format compatible with Streamlit
110
+ converted_filename = "converted_output.mp4"
111
+ clip = mp.VideoFileClip(output_filename)
112
+ clip = clip.set_audio(audio)
113
+
114
+ clip.write_videofile(converted_filename, codec="libx264")
115
+
116
+ # Display the converted video using st.video()
117
+ st.video(converted_filename)
118
+ st.balloons()
119
+
120
+ # Add a download button for the colorized video
121
+ st.download_button(
122
+ label="Download Colorized Video",
123
+ data=open(converted_filename, "rb").read(),
124
+ file_name="colorized_video.mp4"
125
+ )
126
+
127
+ # Close and delete the temporary file after processing
128
+ video.release()
129
 
130
 
131
  if __name__ == "__main__":