ruslanmv commited on
Commit
108e243
·
verified ·
1 Parent(s): 6668dc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -18
app.py CHANGED
@@ -10,6 +10,7 @@ import nltk
10
  import textwrap
11
  import os
12
  import glob
 
13
 
14
  # Ensure 'punkt' is downloaded for nltk
15
  try:
@@ -17,22 +18,26 @@ try:
17
  except LookupError:
18
  nltk.download('punkt')
19
 
20
- # Download ffmpeg if not found (using a more robust method)
21
- try:
22
- from imageio_ffmpeg import get_ffmpeg_exe
23
- get_ffmpeg_exe()
24
- except Exception as e:
25
- print(f"Error downloading ffmpeg: {e}")
26
- print("Attempting to download ffmpeg using a different method...")
27
  try:
28
- import imageio
29
- imageio.plugins.ffmpeg.download(directory=os.path.join(os.path.expanduser("~"), ".imageio"))
30
- print("ffmpeg downloaded successfully.")
31
- except Exception as e:
32
- print(f"Failed to download ffmpeg: {e}")
33
- print("Please ensure you have an internet connection and that imageio and imageio_ffmpeg are installed.")
34
- raise
 
 
 
 
 
 
 
35
 
 
 
36
 
37
  description = " Video Story Generator with Audio \n PS: Generation of video by using Artificial Intelligence by dalle-mini and distilbart and gtss "
38
  title = "Video Story Generator with Audio by using dalle-mini and distilbart and gtss "
@@ -59,9 +64,9 @@ def get_output_video(text):
59
  The required models will be downloaded to models_root if they are not already there.
60
  Set the dtype to torch.float16 to save GPU memory.
61
  If you have an Ampere architecture GPU you can use torch.bfloat16.
62
- Set the device to either "cuda" or "cpu". Once everything has finished initializing,
63
  float32 is faster than float16 but uses more GPU memory.
64
- '''
65
 
66
  def generate_image(
67
  is_mega: bool,
@@ -123,7 +128,7 @@ def get_output_video(text):
123
  y_text = text_start_height
124
  lines = textwrap.wrap(text, width=40)
125
  for line in lines:
126
- line_width, line_height = font.getsize(line)
127
  draw.text(((image_width - line_width) / 2, y_text),
128
  line, font=font, fill=text_color)
129
  y_text += line_height
@@ -133,7 +138,7 @@ def get_output_video(text):
133
  Testing draw_multiple_line_text
134
  '''
135
  image = image_input
136
- fontsize = 13 # starting font size
137
  path_font = "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf"
138
  if not os.path.exists(path_font):
139
  # Try alternative location on different systems
@@ -239,6 +244,16 @@ def get_output_video(text):
239
  final_clip.write_videofile(outname, fps=fps)
240
 
241
  combine_audio(movie_name, export_path, movie_final) # create a new file
 
 
 
 
 
 
 
 
 
 
242
  return 'result_final.mp4'
243
 
244
 
 
10
  import textwrap
11
  import os
12
  import glob
13
+ import subprocess
14
 
15
  # Ensure 'punkt' is downloaded for nltk
16
  try:
 
18
  except LookupError:
19
  nltk.download('punkt')
20
 
21
+ # Function to check and install FFmpeg if not found
22
+ def ensure_ffmpeg_installed():
 
 
 
 
 
23
  try:
24
+ # Check if FFmpeg is installed by running ffmpeg -version
25
+ subprocess.run(['ffmpeg', '-version'], check=True, capture_output=True, text=True)
26
+ print("FFmpeg is already installed.")
27
+ except (subprocess.CalledProcessError, FileNotFoundError):
28
+ print("FFmpeg not found. Attempting to install...")
29
+ try:
30
+ # Install FFmpeg using the system package manager (apt for Debian/Ubuntu)
31
+ subprocess.run(['apt', 'update'], check=True)
32
+ subprocess.run(['apt', 'install', '-y', 'ffmpeg'], check=True)
33
+ print("FFmpeg installed successfully using apt.")
34
+ except subprocess.CalledProcessError as e:
35
+ print(f"Failed to install FFmpeg using apt: {e}")
36
+ print("Please install FFmpeg manually and ensure it is in your system's PATH.")
37
+ raise
38
 
39
+ # Ensure FFmpeg is installed before proceeding
40
+ ensure_ffmpeg_installed()
41
 
42
  description = " Video Story Generator with Audio \n PS: Generation of video by using Artificial Intelligence by dalle-mini and distilbart and gtss "
43
  title = "Video Story Generator with Audio by using dalle-mini and distilbart and gtss "
 
64
  The required models will be downloaded to models_root if they are not already there.
65
  Set the dtype to torch.float16 to save GPU memory.
66
  If you have an Ampere architecture GPU you can use torch.bfloat16.
67
+ Set the device to either "cuda" or "cpu". Once everything has finished initializing,
68
  float32 is faster than float16 but uses more GPU memory.
69
+ '''
70
 
71
  def generate_image(
72
  is_mega: bool,
 
128
  y_text = text_start_height
129
  lines = textwrap.wrap(text, width=40)
130
  for line in lines:
131
+ line_width, line_height = font.getbbox(line)[2:4] # Use getbbox for better size calculation
132
  draw.text(((image_width - line_width) / 2, y_text),
133
  line, font=font, fill=text_color)
134
  y_text += line_height
 
138
  Testing draw_multiple_line_text
139
  '''
140
  image = image_input
141
+ fontsize = 20 # Increased font size
142
  path_font = "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf"
143
  if not os.path.exists(path_font):
144
  # Try alternative location on different systems
 
244
  final_clip.write_videofile(outname, fps=fps)
245
 
246
  combine_audio(movie_name, export_path, movie_final) # create a new file
247
+
248
+ # Cleanup intermediate files
249
+ for f in file_names:
250
+ os.remove(f)
251
+ for f in mp3_names:
252
+ os.remove(f)
253
+ os.remove("result_new.mp4")
254
+ os.remove("result.mp3")
255
+
256
+
257
  return 'result_final.mp4'
258
 
259