Ericwang commited on
Commit
d40a36c
·
1 Parent(s): 1313462

modularized code

Browse files
Files changed (4) hide show
  1. Dockerfile +0 -2
  2. app.py +87 -54
  3. requirements.txt +3 -2
  4. utlis.py +34 -1
Dockerfile CHANGED
@@ -6,8 +6,6 @@ COPY requirements.txt .
6
 
7
  RUN pip install --no-cache-dir -r requirements.txt
8
 
9
- RUN apt-get update && apt-get install -y ffmpeg
10
-
11
  ENV MPLCONFIGDIR /tmp/matplotlib
12
 
13
  RUN mkdir /app/results_vc
 
6
 
7
  RUN pip install --no-cache-dir -r requirements.txt
8
 
 
 
9
  ENV MPLCONFIGDIR /tmp/matplotlib
10
 
11
  RUN mkdir /app/results_vc
app.py CHANGED
@@ -1,12 +1,12 @@
1
  import os
 
2
  import threading
3
  import time
4
  from pathlib import Path
5
 
6
  import gradio as gr
7
 
8
- from utlis import (HHMMSS_to_sec, convert_video_format, molly_xlsx_to_table,
9
- table_to_ELAN_tsv, trim_media)
10
 
11
 
12
  def delete_files(files):
@@ -18,62 +18,102 @@ def delete_files(files):
18
  pass
19
  print("files deleted")
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def trim_video_wt(input_file, input_transcript, output_format, start_time, end_time):
22
  # trim video with transcript
23
  try:
24
- # Set output file name and extension
25
  output_folder = f"{os.getcwd()}/results_wt/"
26
- if not os.path.exists(output_folder):
27
- os.makedirs(output_folder)
28
- file_name = f"{Path(input_file.name).stem.partition('.')[0]}_trimmed.{output_format}"
29
- output_file = os.path.join(output_folder, file_name)
30
- print(f"Output folder: {output_folder}")
31
- print(f"file_name: {file_name}")
32
- print(f"Output file: {output_file}")
33
- if start_time == "":
34
- start_time = "00:00:00"
35
- if end_time == "":
36
- end_time = "00:05:00"
37
  # Trim the video
38
- print("start trimming")
39
- output_file = trim_media(input_file.name, output_file, start_time, end_time)
40
-
41
- # trim transcript
42
- print("start trimming transcript")
43
- table = molly_xlsx_to_table(xl_file=input_transcript.name)
44
- transcript_name = f"{Path(input_transcript.name).stem.partition('.')[0]}_trimmed.tsv"
45
- transcript_file = os.path.join(output_folder, transcript_name)
46
- table_to_ELAN_tsv(table, transcript_file)
47
 
 
 
 
 
48
  # remove file after 10 minutes for security
49
  print("start deleting files")
50
- path_to_delete = [input_file.name, input_transcript.name, output_file, transcript_file]
51
  threading.Thread(target=delete_files, args=([path_to_delete])).start()
52
 
53
- return output_file, transcript_file
54
  except Exception as e:
55
  return f"Error: {str(e)}"
56
 
57
  def trim_video(input_file, output_format, start_time, end_time):
58
  try:
59
- # Set output file name and extension
60
  output_folder = f"{os.getcwd()}/results_vt/"
61
- if not os.path.exists(output_folder):
62
- os.makedirs(output_folder)
63
- file_name = f"{Path(input_file.name).stem.partition('.')[0]}_trimmed.{output_format}"
64
- output_file = os.path.join(output_folder, file_name)
65
- print(f"Output folder: {output_folder}")
66
- print(f"file_name: {file_name}")
67
- print(f"Output file: {output_file}")
68
- if start_time == "":
69
- start_time = "00:00:00"
70
- if end_time == "":
71
- end_time = "00:05:00"
72
  # Trim the video
73
- print("start trimming")
74
- output_file = trim_media(input_file.name, output_file, start_time, end_time)
75
 
76
- # remove file after 10 minutes for security
77
  print("start deleting files")
78
  path_to_delete = [input_file.name, output_file]
79
  threading.Thread(target=delete_files, args=([path_to_delete])).start()
@@ -84,19 +124,13 @@ def trim_video(input_file, output_format, start_time, end_time):
84
 
85
  def convert_video(input_file, output_format):
86
  try:
87
- # Set output file name and extension
88
- output_folder = f"{os.getcwd()}/results_vc/"
89
- if not os.path.exists(output_folder):
90
- os.makedirs(output_folder)
91
- file_name = f"{Path(input_file.name).stem.partition('.')[0]}_converted.{output_format}"
92
- output_file = os.path.join(output_folder, file_name)
93
- print(f"Output folder: {output_folder}")
94
- print(f"file_name: {file_name}")
95
- print(f"Output file: {output_file}")
96
- # Convert the video
97
- print("start converting")
98
- output_file = convert_video_format(input_file.name, output_file)
99
-
100
  # remove file after 10 minutes for security
101
  print("start deleting files")
102
  path_to_delete = [input_file.name, output_file]
@@ -121,7 +155,6 @@ interface_wt = gr.Interface(fn=trim_video_wt, inputs=[input_file_wt, input_trans
121
 
122
  # gr components for video trimmer
123
  input_file = gr.File(label="Select video file")
124
- # input_file.upload(show_progress=True)
125
  output_format = gr.Dropdown(choices=["mkv", "MOV", "mp4", "wav"], label="Select output format", value="mp4", )
126
  start_time = gr.Textbox(label="Start time (in seconds or HH:MM:SS). Leave blank to start at beginning.")
127
  end_time = gr.Textbox(label="End time (in seconds or HH:MM:SS). Leave blank to trim a 5-minute interval since start.")
 
1
  import os
2
+ import re
3
  import threading
4
  import time
5
  from pathlib import Path
6
 
7
  import gradio as gr
8
 
9
+ from utlis import HHMMSS_to_sec, convert_video_format, molly_xlsx_to_table, xlsx_to_table, table_to_ELAN_tsv, trim_media
 
10
 
11
 
12
  def delete_files(files):
 
18
  pass
19
  print("files deleted")
20
 
21
+ def classify_input_format(input_string):
22
+ # check if the input string is empty
23
+ if not input_string:
24
+ return 0
25
+
26
+ # check if the input string is a valid time in the format HH:MM:SS
27
+ hhmmss_pattern = re.compile('^\d{1,2}:\d{1,2}:\d{1,2}$')
28
+ if hhmmss_pattern.match(input_string):
29
+ return HHMMSS_to_sec(time_str=input_string)
30
+
31
+ # check if the input string is a valid number in the format of seconds
32
+ try:
33
+ seconds = float(input_string)
34
+ return float(input_string)
35
+ except ValueError:
36
+ pass
37
+
38
+ # if the input format is neither seconds nor HH:MM:SS, return None
39
+ return None
40
+
41
+ def set_output_file(input_file, output_format, folder):
42
+ # Set output file name and extension
43
+ if not os.path.exists(folder):
44
+ os.makedirs(folder)
45
+ file_name = f"{Path(input_file.name).stem.partition('.')[0]}_trimmed.{output_format}"
46
+ output_file = os.path.join(folder, file_name)
47
+ print(f"Output folder: {folder}")
48
+ print(f"file_name: {file_name}")
49
+ print(f"Output file: {output_file}")
50
+ return output_file
51
+
52
+ def trim_video_helper(input_file, output_file, start_time, end_time):
53
+ if start_time == "":
54
+ start_time = 0
55
+ if end_time == "":
56
+ end_time = 300 + HHMMSS_to_sec(time_str=start_time)
57
+ # Trim the video
58
+ print("start trimming")
59
+ start_time = classify_input_format(start_time)
60
+ end_time = classify_input_format(end_time)
61
+ print("start time: ", start_time)
62
+ print("end time: ", end_time)
63
+ output_file = trim_media(input_file.name, output_file, start_time, end_time)
64
+ print("finish trimming")
65
+ return output_file
66
+
67
+ def convert_video_helper(input_file, output_file, output_format):
68
+ # convert video
69
+ print("start converting")
70
+ output_file = convert_video_format(input_file.name, output_file)
71
+ print("finish converting")
72
+ return output_file
73
+
74
+ def convert_transcript_helper(input_transcript, output_transcript):
75
+ # convert transcript
76
+ print("start converting transcript")
77
+ table = xlsx_to_table(xl_file=input_transcript)
78
+ print("finish converting transcript to table")
79
+ output_file = table_to_ELAN_tsv(table, output_transcript)
80
+ print("finish converting transcript")
81
+ return output_file
82
+
83
  def trim_video_wt(input_file, input_transcript, output_format, start_time, end_time):
84
  # trim video with transcript
85
  try:
86
+ # set output file
87
  output_folder = f"{os.getcwd()}/results_wt/"
88
+ output_file = set_output_file(input_file, output_format, output_folder)
89
+ output_transcript = set_output_file(input_transcript, "tsv", output_folder)
90
+
 
 
 
 
 
 
 
 
91
  # Trim the video
92
+ output_file = trim_video_helper(input_file, output_file, start_time, end_time)
 
 
 
 
 
 
 
 
93
 
94
+ # convert transcript
95
+ path = input_transcript.name
96
+ output_transcript = convert_transcript_helper(path, output_transcript)
97
+ # output_transcript = output_file
98
  # remove file after 10 minutes for security
99
  print("start deleting files")
100
+ path_to_delete = [input_file.name, input_transcript.name, output_file, output_transcript]
101
  threading.Thread(target=delete_files, args=([path_to_delete])).start()
102
 
103
+ return output_file, output_transcript
104
  except Exception as e:
105
  return f"Error: {str(e)}"
106
 
107
  def trim_video(input_file, output_format, start_time, end_time):
108
  try:
109
+ # Set output file
110
  output_folder = f"{os.getcwd()}/results_vt/"
111
+ output_file = set_output_file(input_file, output_format, output_folder)
112
+
 
 
 
 
 
 
 
 
 
113
  # Trim the video
114
+ output_file = trim_video_helper(input_file, output_file, start_time, end_time)
 
115
 
116
+ # Remove files after 10 minutes for security
117
  print("start deleting files")
118
  path_to_delete = [input_file.name, output_file]
119
  threading.Thread(target=delete_files, args=([path_to_delete])).start()
 
124
 
125
  def convert_video(input_file, output_format):
126
  try:
127
+ # Set output file
128
+ output_folder = f"{os.getcwd()}/results_cv/"
129
+ output_file = set_output_file(input_file, output_format, output_folder)
130
+
131
+ # Convert video
132
+ output_file = convert_video_helper(input_file, output_file, output_format)
133
+
 
 
 
 
 
 
134
  # remove file after 10 minutes for security
135
  print("start deleting files")
136
  path_to_delete = [input_file.name, output_file]
 
155
 
156
  # gr components for video trimmer
157
  input_file = gr.File(label="Select video file")
 
158
  output_format = gr.Dropdown(choices=["mkv", "MOV", "mp4", "wav"], label="Select output format", value="mp4", )
159
  start_time = gr.Textbox(label="Start time (in seconds or HH:MM:SS). Leave blank to start at beginning.")
160
  end_time = gr.Textbox(label="End time (in seconds or HH:MM:SS). Leave blank to trim a 5-minute interval since start.")
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
- gradio==3.23.0
2
  moviepy==1.0.3
3
- pandas==1.1.3
 
 
1
+ gradio==3.24.1
2
  moviepy==1.0.3
3
+ pandas==1.1.3
4
+ xlrd==1.2.0
utlis.py CHANGED
@@ -140,6 +140,39 @@ def molly_xlsx_to_table(xl_file):
140
 
141
  return table
142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  def table_to_ELAN_tsv(table:pd.DataFrame, path:str):
144
  # write table to tsv compatible with ELAN import
145
- table.to_csv(path, index=False, float_format='%.3f',sep='\t')
 
 
140
 
141
  return table
142
 
143
+ def xlsx_to_table(xl_file):
144
+ # read the first sheet of the Excel file into a DataFrame
145
+ print(f'...reading {xl_file}...')
146
+ breakpoint()
147
+ table = pd.read_excel(xl_file, sheet_name=0)
148
+ print(f'...done reading {xl_file}...')
149
+ # extract start and end time from the Timecode column
150
+ print(f'...splitting Timecode column into start and end time...')
151
+ timecodes = table['Timecode'].str.split(' - ', expand=True)
152
+ table['start_time'] = timecodes[0]
153
+ table['end_time'] = timecodes[1]
154
+ print(f'...done splitting Timecode column into start and end time...')
155
+ # convert start and end time to seconds using the HHMMSS_to_sec function
156
+ print(f'...converting start and end time to seconds...')
157
+ table['start_sec'] = table['start_time'].apply(HHMMSS_to_sec)
158
+ table['end_sec'] = table['end_time'].apply(HHMMSS_to_sec)
159
+ print(f'...done converting start and end time to seconds...')
160
+ # drop unnecessary columns
161
+ print(f'...dropping unnecessary columns...')
162
+ table.drop(['Timecode', 'Annotations', 'Error Type', 'Duration'], axis=1, inplace=True)
163
+
164
+ # rename columns
165
+ print(f'...renaming columns...')
166
+ table.rename(columns={'#': 'uttID', 'Speaker': 'speaker', 'Dialogue': 'transcript'}, inplace=True)
167
+
168
+ # reorder columns
169
+ print(f'...reordering columns...')
170
+ table = table[['uttID', 'speaker', 'transcript', 'start_sec', 'end_sec']]
171
+
172
+ return table
173
+
174
+
175
  def table_to_ELAN_tsv(table:pd.DataFrame, path:str):
176
  # write table to tsv compatible with ELAN import
177
+ table.to_csv(path, index=False, float_format='%.3f',sep='\t')
178
+ return path