RohitCSharp commited on
Commit
729f48e
·
verified ·
1 Parent(s): 8692d18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -21
app.py CHANGED
@@ -52,11 +52,13 @@ def create_slides(text, duration, output_folder, max_lines=6):
52
  draw = ImageDraw.Draw(img)
53
 
54
  lines = slide_text.split("\n")
55
- total_height = sum([draw.textbbox((0, 0), line, font=font)[3] for line in lines]) + (len(lines)-1)*10
56
  y = (720 - total_height) // 2
57
 
58
  for line in lines:
59
- w, h = draw.textbbox((0, 0), line, font=font)[2:]
 
 
60
  draw.text(((1280 - w) // 2, y), line, font=font, fill="white")
61
  y += h + 10
62
 
@@ -82,27 +84,20 @@ def url_to_av_summary(url, duration):
82
  frame_dir = tempfile.mkdtemp()
83
  slides = create_slides(summary, duration, frame_dir)
84
 
85
- concat_file = os.path.join(frame_dir, "frames.txt")
86
- with open(concat_file, "w") as f:
87
- for path, t in slides:
88
- f.write(f"file '{path}'\n")
89
- f.write(f"duration {t}\n")
90
 
 
91
  concat_img = os.path.join(frame_dir, "video_input.mp4")
92
- ffmpeg.input(concat_file, format='concat', safe=0).output(
93
- concat_img,
94
- r=1,
95
- vcodec='libx264', pix_fmt='yuv420p'
96
- ).run(overwrite_output=True, quiet=True)
97
-
98
- video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
99
- ffmpeg.input(concat_img).output(
100
- video_path,
101
- i=audio_path,
102
- vcodec='libx264', acodec='aac', pix_fmt='yuv420p', shortest=None
103
- ).run(overwrite_output=True, quiet=True)
104
-
105
- return summary, video_path
106
 
107
  iface = gr.Interface(
108
  fn=url_to_av_summary,
 
52
  draw = ImageDraw.Draw(img)
53
 
54
  lines = slide_text.split("\n")
55
+ total_height = sum([draw.textbbox((0, 0), line, font=font)[3] - draw.textbbox((0, 0), line, font=font)[1] for line in lines]) + (len(lines)-1)*10
56
  y = (720 - total_height) // 2
57
 
58
  for line in lines:
59
+ bbox = draw.textbbox((0, 0), line, font=font)
60
+ w = bbox[2] - bbox[0]
61
+ h = bbox[3] - bbox[1]
62
  draw.text(((1280 - w) // 2, y), line, font=font, fill="white")
63
  y += h + 10
64
 
 
84
  frame_dir = tempfile.mkdtemp()
85
  slides = create_slides(summary, duration, frame_dir)
86
 
87
+ slide_inputs = []
88
+ for path, t in slides:
89
+ slide_inputs.extend(["-t", str(t), "-loop", "1", "-i", path])
 
 
90
 
91
+ filter_complex = "".join([f"[{i}:v]" for i in range(len(slides))]) + f"concat=n={len(slides)}:v=1[outv]"
92
  concat_img = os.path.join(frame_dir, "video_input.mp4")
93
+
94
+ cmd = ["ffmpeg", "-y"] + slide_inputs + ["-filter_complex", filter_complex, "-map", "[outv]", concat_img]
95
+ subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
96
+
97
+ final_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
98
+ ffmpeg.input(concat_img).output(audio_path, final_video, vcodec='libx264', acodec='aac', pix_fmt='yuv420p', shortest=None).run(overwrite_output=True, quiet=True)
99
+
100
+ return summary, final_video
 
 
 
 
 
 
101
 
102
  iface = gr.Interface(
103
  fn=url_to_av_summary,