Ericwang commited on
Commit
ecfddf0
·
1 Parent(s): ffccc46

added another interface

Browse files
Files changed (3) hide show
  1. Dockerfile +5 -2
  2. app.py +48 -72
  3. utlis.py +85 -2
Dockerfile CHANGED
@@ -2,10 +2,13 @@ FROM python:3.9-slim-buster
2
 
3
  WORKDIR /app
4
 
 
 
 
 
5
  COPY requirements.txt .
6
 
7
- RUN pip install --no-cache-dir --upgrade pip && \
8
- pip install --no-cache-dir -r requirements.txt
9
 
10
  RUN apt-get update && apt-get install -y ffmpeg
11
 
 
2
 
3
  WORKDIR /app
4
 
5
+ RUN mkdir /app/results_t
6
+
7
+ RUN mkdir /app/results_c
8
+
9
  COPY requirements.txt .
10
 
11
+ RUN pip install --no-cache-dir -r requirements.txt
 
12
 
13
  RUN apt-get update && apt-get install -y ffmpeg
14
 
app.py CHANGED
@@ -1,92 +1,68 @@
 
 
1
  import gradio as gr
2
- import subprocess
3
- from pathlib import Path
4
- from utlis import HHMMSS_to_sec
5
 
6
- def trim_media(media_in, media_out, start, end):
7
-
8
- # options for writing out audio if converting
9
- WAV_CHANNELS = 1
10
- WAV_SAMPLE_RATE = 16000
11
-
12
- media_type = Path(media_in).suffix
13
- ext = Path(media_out).suffix
14
 
15
- if isinstance(start, str):
16
- start_sec = HHMMSS_to_sec(start)
17
- else:
18
- start_sec = float(start)
19
- if isinstance(end, str):
20
- end_sec = HHMMSS_to_sec(end)
21
- else:
22
- end_sec = float(end)
23
 
24
- if ext == '.wav':
25
- # convert to wav with standard format for audio models
26
- print(f'...Using ffmpeg to trim video from {start} to {end} \n and convert to {WAV_SAMPLE_RATE}Hz WAV with {WAV_CHANNELS} channels...')
27
- print(f'...generating {media_out}...')
28
 
29
- subprocess.call(['ffmpeg',
30
- '-y',
31
- '-i',
32
- media_in,
33
- '-ss',
34
- f'{start_sec}',
35
- '-to',
36
- f'{end_sec}',
37
- '-acodec',
38
- 'pcm_s16le',
39
- '-ac',
40
- WAV_CHANNELS,
41
- '-ar',
42
- WAV_SAMPLE_RATE,
43
- media_out,
44
- '-hide_banner',
45
- '-loglevel',
46
- 'warning'
47
- ], shell=False)
48
-
49
- else:
50
- print(f'...Using ffmpeg to trim video from {start_sec} to {end_sec}...')
51
- print(f'...generating {media_out}...')
52
-
53
- subprocess.call(['ffmpeg',
54
- '-y',
55
- '-i',
56
- media_in,
57
- '-ss',
58
- f'{start_sec}',
59
- '-to',
60
- f'{end_sec}',
61
- '-c',
62
- 'copy',
63
- media_out,
64
- '-hide_banner',
65
- '-loglevel',
66
- 'warning'
67
- ], shell=False)
68
-
69
- def convert_video(input_file, output_format, start_time, end_time):
70
  try:
71
  # Set output file name and extension
72
- output_file = f"{input_file.name.split('.')[0]}.{output_format}"
73
-
 
 
 
 
 
 
74
  if start_time == "":
75
- start_time = 0
76
  if end_time == "":
77
- end_time = 0
78
  # Trim the video
79
- trim_media(input_file.name, output_file, start_time, end_time)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  return output_file
82
  except Exception as e:
83
  return f"Error: {str(e)}"
84
 
 
85
  input_file = gr.File(label="Select video file")
86
  output_format = gr.Dropdown(choices=["mkv", "MOV", "mp4", "wav"], label="Select output format")
87
  start_time = gr.Textbox(label="Start time (in seconds or HH:MM:SS). Leave blank to start at beginning.")
88
- end_time = gr.Textbox(label="End time (in seconds or HH:MM:SS). Leave blank to end at end.")
89
  output_file = gr.File(label="Download converted file")
90
 
91
- interface = gr.Interface(fn=convert_video, inputs=[input_file, output_format, start_time, end_time], outputs=output_file, title="Video Trimmer")
92
- interface.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
  import gradio as gr
 
 
 
4
 
5
+ from pathlib import Path
 
 
 
 
 
 
 
6
 
7
+ from utlis import HHMMSS_to_sec, convert_video_format, trim_media
 
 
 
 
 
 
 
8
 
 
 
 
 
9
 
10
+ def trim_video(input_file, output_format, start_time, end_time):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  try:
12
  # Set output file name and extension
13
+ output_folder = f"{os.getcwd()}/results_vt/"
14
+ if not os.path.exists(output_folder):
15
+ os.makedirs(output_folder)
16
+ file_name = f"{Path(input_file.name).stem.partition('.')[0]}_trimmed.{output_format}"
17
+ output_file = os.path.join(output_folder, file_name)
18
+ print(f"Output folder: {output_folder}")
19
+ print(f"file_name: {file_name}")
20
+ print(f"Output file: {output_file}")
21
  if start_time == "":
22
+ start_time = "00:00:00"
23
  if end_time == "":
24
+ end_time = "00:05:00"
25
  # Trim the video
26
+ print("start trimming")
27
+ output_file = trim_media(input_file.name, output_file, start_time, end_time)
28
+
29
+ return output_file
30
+ except Exception as e:
31
+ return f"Error: {str(e)}"
32
+
33
+ def convert_video(input_file, output_format):
34
+ try:
35
+ # Set output file name and extension
36
+ output_folder = f"{os.getcwd()}/results_ct/"
37
+ if not os.path.exists(output_folder):
38
+ os.makedirs(output_folder)
39
+ output_file = os.path.join(output_folder, f"{input_file.name.split('.')[0]}.{output_format}")
40
+ print(f"Output folder: {output_folder}")
41
+ print(f"Output file: {output_file}")
42
+ # Convert the video
43
+ print("start converting")
44
+ convert_video_format(input_file.name, output_file)
45
 
46
  return output_file
47
  except Exception as e:
48
  return f"Error: {str(e)}"
49
 
50
+ # gr components for video trimmer
51
  input_file = gr.File(label="Select video file")
52
  output_format = gr.Dropdown(choices=["mkv", "MOV", "mp4", "wav"], label="Select output format")
53
  start_time = gr.Textbox(label="Start time (in seconds or HH:MM:SS). Leave blank to start at beginning.")
54
+ end_time = gr.Textbox(label="End time (in seconds or HH:MM:SS). Leave blank to trim a 5-minute interval since start.")
55
  output_file = gr.File(label="Download converted file")
56
 
57
+ interface = gr.Interface(fn=trim_video, inputs=[input_file, output_format, start_time, end_time], outputs=output_file, title="Video Trimmer")
58
+
59
+ # gr components for video converter
60
+ input_file_c = gr.File(label="Select video file")
61
+ output_format_c = gr.Dropdown(choices=["mkv", "MOV", "mp4", "wav"], label="Select output format")
62
+ output_file_c = gr.File(label="Download converted file")
63
+
64
+ interface_c = gr.Interface(fn=convert_video, inputs=[input_file_c, output_format_c], outputs=output_file_c, title="Video Converter")
65
+
66
+ demo = gr.TabbedInterface([interface, interface_c], ["Video Trimmer", "Video Converter"])
67
+
68
+ demo.launch(server_name="0.0.0.0", server_port=7860)
utlis.py CHANGED
@@ -1,9 +1,92 @@
1
- import os
2
  import csv
 
3
  import re
4
- import pandas as pd
5
  from pathlib import Path
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def HHMMSS_to_sec(time_str):
8
  """Get Seconds from timestamp string with milliseconds."""
9
  if not time_str:
 
 
1
  import csv
2
+ import os
3
  import re
4
+ import subprocess
5
  from pathlib import Path
6
 
7
+ import pandas as pd
8
+
9
+ def convert_video_format(media_in, media_out):
10
+ WAV_CHANNELS = 1
11
+ WAV_SAMPLE_RATE = 16000
12
+
13
+ if not isinstance(media_in, (str, Path)):
14
+ raise TypeError("media_in must be a string or a PathLike object")
15
+
16
+ if not isinstance(media_out, (str, Path)):
17
+ raise TypeError("media_out must be a string or a PathLike object")
18
+ ext = Path(media_out).suffix
19
+ if ext == '.wav':
20
+ # convert to wav with standard format for audio models
21
+ print(f'...generating {media_out}...')
22
+ subprocess.run(f'ffmpeg -y -i "{media_in}" -acodec pcm_s16le -ac {WAV_CHANNELS} -ar {WAV_SAMPLE_RATE} "{media_out}" -hide_banner -loglevel warning', check=True, shell=True)
23
+
24
+
25
+ else:
26
+ print(f'...Using ffmpeg to convert {media_in} to {media_out}...')
27
+ print(f'...generating {media_out}...')
28
+
29
+ subprocess.run(['ffmpeg',
30
+ '-y',
31
+ '-i',
32
+ media_in,
33
+ '-c',
34
+ 'copy',
35
+ media_out,
36
+ '-hide_banner',
37
+ '-loglevel',
38
+ 'warning'
39
+ ], check=True)
40
+
41
+ def trim_media(media_in, media_out, start, end):
42
+
43
+ # options for writing out audio if converting
44
+ WAV_CHANNELS = 1
45
+ WAV_SAMPLE_RATE = 16000
46
+
47
+ media_type = Path(media_in).suffix
48
+ ext = Path(media_out).suffix
49
+ print("EXT", ext)
50
+ if isinstance(start, str):
51
+ start_sec = HHMMSS_to_sec(start)
52
+ else:
53
+ start_sec = float(start)
54
+ if isinstance(end, str):
55
+ end_sec = HHMMSS_to_sec(end)
56
+ else:
57
+ end_sec = float(end)
58
+
59
+ if ext == '.wav':
60
+ # convert to wav with standard format for audio models
61
+ print(f'...Using ffmpeg to trim video from {start} to {end} \n and convert to {WAV_SAMPLE_RATE}Hz WAV with {WAV_CHANNELS} channels...')
62
+ print(f'...generating {media_out}...')
63
+
64
+ subprocess.run(f'ffmpeg -y -i "{media_in}" -ss {start_sec} -to {end_sec} -acodec pcm_s16le -ac {WAV_CHANNELS} -ar {WAV_SAMPLE_RATE} "{media_out}" -hide_banner -loglevel warning', check=True, shell=True)
65
+
66
+ print(f'...done trimming and converting to {media_out}...')
67
+ return media_out
68
+
69
+ else:
70
+ print(f'...Using ffmpeg to trim video from {start_sec} to {end_sec}...')
71
+ print(f'...generating {media_out}...')
72
+
73
+ subprocess.run(['ffmpeg',
74
+ '-y',
75
+ '-i',
76
+ media_in,
77
+ '-ss',
78
+ f'{start_sec}',
79
+ '-to',
80
+ f'{end_sec}',
81
+ '-c',
82
+ 'copy',
83
+ media_out,
84
+ '-hide_banner',
85
+ '-loglevel',
86
+ 'warning'
87
+ ], check=True)
88
+ return media_out
89
+
90
  def HHMMSS_to_sec(time_str):
91
  """Get Seconds from timestamp string with milliseconds."""
92
  if not time_str: