sergiomar73 commited on
Commit
289c6b4
·
1 Parent(s): a4a1677

Initial implementation

Browse files
Files changed (2) hide show
  1. app.py +122 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import datetime
3
+ import gradio as gr
4
+ import pandas as pd
5
+ from io import BytesIO
6
+ from pathlib import Path
7
+ from urllib.parse import urlparse
8
+
9
+
10
+ def format_seconds(secs):
11
+ t = datetime.datetime(
12
+ year=1, month=1, day=1, hour=0, minute=0
13
+ ) + datetime.timedelta(seconds=secs)
14
+ return t.strftime("%M:%S.%f")[:-3]
15
+
16
+
17
+ def get_filename_and_extension(url):
18
+ parsed_url = urlparse(url)
19
+ path = parsed_url.path
20
+ filename = Path(path).name
21
+ filename_without_extension = Path(filename).stem
22
+ file_extension = Path(filename).suffix
23
+ return filename, filename_without_extension, file_extension
24
+
25
+
26
+ def calculate_times(input_url, input_text, ms_before, ms_after):
27
+ _, _, file_extension = get_filename_and_extension(input_url)
28
+ file_extension = file_extension.replace(".", "")
29
+ df = pd.DataFrame({"text": [], "start": [], "stop": [], "file": []})
30
+ lines = input_text.splitlines()
31
+ segments = []
32
+ if len(lines) != len(segments):
33
+ msg = f"DETECTED CLIPS AND INPUT LINES DO NOT MATCH!\n\nYou are expecting {len(lines)} clips BUT {len(segments)} segments have been found in the video file."
34
+ df.loc[len(df.index)] = ["", "", "", ""]
35
+ return msg, None, df
36
+ else:
37
+ res = []
38
+ for i in range(len(segments)):
39
+ line = lines[i].rstrip()
40
+ res.append(f"{line}\t{segments[i][0]}\t{segments[i][1]}\t{input_url}")
41
+ df.loc[len(df.index)] = [line, segments[i][0], segments[i][1], input_url]
42
+ df.to_csv(
43
+ "clips.tsv",
44
+ sep="\t",
45
+ encoding="utf-8",
46
+ index=False,
47
+ header=False,
48
+ quoting=csv.QUOTE_NONE,
49
+ )
50
+ return "\n".join(res), "clips.tsv", df
51
+
52
+
53
+ def load_video(input_url):
54
+ if input_url:
55
+ return input_url
56
+ return None
57
+
58
+
59
+ css = """
60
+ .required {background-color: #FFCCCB !important, font-size: 24px !important}
61
+ """
62
+
63
+
64
+ with gr.Blocks(title="Start and stop times", css=css) as app:
65
+ gr.Markdown(
66
+ """# Start and stop times generator
67
+ Please, fill the Video URL and Clip texts textboxes and click the Run button"""
68
+ )
69
+ with gr.Row():
70
+ with gr.Column(scale=3):
71
+ text1 = gr.Textbox(
72
+ lines=1,
73
+ placeholder="Video URL...",
74
+ label="Video URL",
75
+ elem_classes=["required"],
76
+ )
77
+ text2 = gr.Textbox(
78
+ lines=5,
79
+ max_lines=10,
80
+ placeholder="List of clip texts...",
81
+ label="Clip texts",
82
+ elem_classes=["required"],
83
+ )
84
+ slider1 = gr.Slider(
85
+ minimum=0,
86
+ maximum=1000,
87
+ step=50,
88
+ value=0,
89
+ label="Milliseconds BEFORE each clip",
90
+ )
91
+ slider2 = gr.Slider(
92
+ minimum=0,
93
+ maximum=1000,
94
+ step=50,
95
+ value=500,
96
+ label="Milliseconds AFTER each clip",
97
+ )
98
+ btn_submit = gr.Button(value="Run", variant="primary", size="sm")
99
+ video = gr.Video(
100
+ format="mp4", label="Video file", show_label=True, interactive=False
101
+ )
102
+ with gr.Column(scale=5):
103
+ file = gr.File(
104
+ label="Clips", show_label=True, file_count=1, interactive=False
105
+ )
106
+ lines = gr.Textbox(
107
+ lines=10, label="Clips", interactive=False, show_copy_button=True
108
+ )
109
+ data = gr.Dataframe(
110
+ label="Clips",
111
+ headers=["text", "start", "stop", "file"],
112
+ datatype=["str", "str", "str", "str"],
113
+ row_count=0,
114
+ )
115
+ btn_submit.click(
116
+ calculate_times,
117
+ inputs=[text1, text2, slider1, slider2],
118
+ outputs=[lines, file, data],
119
+ )
120
+ text1.blur(load_video, inputs=[text1], outputs=[video])
121
+
122
+ app.launch()
requirements.txt ADDED
File without changes