Pranjal Pruthi commited on
Commit
76ae737
·
1 Parent(s): 7cc344d

Add application files and dependencies

Browse files
Files changed (4) hide show
  1. README.md +5 -0
  2. app.py +106 -0
  3. packages.txt +1 -0
  4. requirements.txt +3 -0
README.md CHANGED
@@ -10,4 +10,9 @@ pinned: false
10
  license: apache-2.0
11
  ---
12
 
 
 
 
 
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
10
  license: apache-2.0
11
  ---
12
 
13
+
14
+ # YouTube Video Processor
15
+
16
+ This Streamlit app allows you to process YouTube videos or playlists. It downloads videos, trims them using auto-editor, and re-encodes them with ffmpeg.
17
+
18
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import subprocess
3
+ import os
4
+ import glob
5
+
6
+ def run_command(command):
7
+ process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)
8
+ output, error = process.communicate()
9
+ return output, error
10
+
11
+ def process_video(url):
12
+ output_dir = "./output_folder"
13
+ os.makedirs(output_dir, exist_ok=True)
14
+
15
+ # Determine if it's a playlist or single video
16
+ content_type = "playlist" if "list=" in url else "video"
17
+
18
+ # Download video(s)
19
+ format = "bestvideo[height<=1080][ext=mp4]+bestaudio[ext=m4a]/best[height<=1080][ext=mp4]"
20
+ download_command = f'yt-dlp -f "{format}" -N 64 -o "{output_dir}/%(title)s.%(ext)s" "{url}"'
21
+ output, error = run_command(download_command)
22
+ st.text(output)
23
+ if error:
24
+ st.error(error)
25
+ return
26
+
27
+ # Process videos
28
+ if content_type == "video":
29
+ video_file = glob.glob(f"{output_dir}/*.mp4")[0]
30
+ trimmed_file = video_file.replace(".mp4", "_trimmed.mp4")
31
+ auto_editor_command = f'auto-editor "{video_file}" --margin 0.5sec --output "{trimmed_file}"'
32
+ output, error = run_command(auto_editor_command)
33
+ st.text(output)
34
+ if error:
35
+ st.error(error)
36
+ return
37
+
38
+ final_file = trimmed_file.replace("_trimmed.mp4", "_f.mp4")
39
+ ffmpeg_command = f'ffmpeg -i "{trimmed_file}" -map 0:v:0 -map 0:a:0 -c:v libx264 -preset ultrafast -crf 23 -maxrate 25M -vf "scale=-1:1080" -c:a aac -b:a 192k -ac 2 -strict -2 "{final_file}"'
40
+ output, error = run_command(ffmpeg_command)
41
+ st.text(output)
42
+ if error:
43
+ st.error(error)
44
+ return
45
+
46
+ return final_file
47
+
48
+ elif content_type == "playlist":
49
+ trimmed_dir = f"{output_dir}/trimmed"
50
+ os.makedirs(trimmed_dir, exist_ok=True)
51
+
52
+ for video_file in glob.glob(f"{output_dir}/*.mp4"):
53
+ video_name = os.path.basename(video_file).replace(".mp4", "")
54
+ trimmed_file = f"{trimmed_dir}/{video_name}_trimmed.mp4"
55
+ auto_editor_command = f'auto-editor "{video_file}" --margin 0.5sec --output "{trimmed_file}"'
56
+ output, error = run_command(auto_editor_command)
57
+ st.text(output)
58
+ if error:
59
+ st.error(error)
60
+ continue
61
+
62
+ final_file = f"{trimmed_dir}/{video_name}_f.mp4"
63
+ ffmpeg_command = f'ffmpeg -i "{trimmed_file}" -map 0:v:0 -map 0:a:0 -c:v libx264 -preset ultrafast -crf 23 -maxrate 25M -vf "scale=-1:1080" -c:a aac -b:a 192k -ac 2 -strict -2 "{final_file}"'
64
+ output, error = run_command(ffmpeg_command)
65
+ st.text(output)
66
+ if error:
67
+ st.error(error)
68
+ continue
69
+
70
+ return trimmed_dir
71
+
72
+ def main():
73
+ st.title("YouTube Video Processor")
74
+
75
+ url = st.text_input("Enter the YouTube URL:")
76
+
77
+ if st.button("Process Video"):
78
+ if url:
79
+ with st.spinner("Processing video..."):
80
+ result = process_video(url)
81
+
82
+ if result:
83
+ if os.path.isfile(result):
84
+ st.success("Video processing completed!")
85
+ st.download_button(
86
+ label="Download Processed Video",
87
+ data=open(result, 'rb'),
88
+ file_name=os.path.basename(result),
89
+ mime="video/mp4"
90
+ )
91
+ elif os.path.isdir(result):
92
+ st.success("Playlist processing completed!")
93
+ for video in glob.glob(f"{result}/*_f.mp4"):
94
+ st.download_button(
95
+ label=f"Download {os.path.basename(video)}",
96
+ data=open(video, 'rb'),
97
+ file_name=os.path.basename(video),
98
+ mime="video/mp4"
99
+ )
100
+ else:
101
+ st.error("An error occurred during processing.")
102
+ else:
103
+ st.warning("Please enter a YouTube URL.")
104
+
105
+ if __name__ == "__main__":
106
+ main()
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ yt-dlp
3
+ auto-editor