soiz commited on
Commit
035c6aa
·
verified ·
1 Parent(s): c6c6efe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -37
app.py CHANGED
@@ -1,47 +1,23 @@
1
  import gradio as gr
2
- from PIL import Image, ImageSequence
3
- import imageio
4
 
5
  def edit_file(file, loop_count, speed_factor, frame_skip):
6
  file_name = file.name.split("/")[-1]
7
  base_name = file_name.rsplit(".", 1)[0]
8
-
9
  # 動画またはGIFの読み込み
10
- if file.name.endswith((".mp4", ".avi", ".mov", ".mkv")):
11
- gif_path = f"{base_name}.gif"
12
- video = imageio.get_reader(file.name)
13
- frames = []
14
- for i, frame in enumerate(video):
15
- if i % frame_skip == 0: # フレーム間引き
16
- frames.append(frame)
17
- imageio.mimsave(gif_path, frames, duration=1/speed_factor)
18
- gif = Image.open(gif_path)
19
- else:
20
- gif = Image.open(file.name)
21
-
22
- # GIF編集
23
- frames = []
24
- durations = []
25
- original_duration = gif.info.get("duration", 100) # フレーム間の時間(ms)
26
- for i, frame in enumerate(ImageSequence.Iterator(gif)):
27
- if i % frame_skip == 0: # フレーム間引き
28
- frames.append(frame.copy())
29
- durations.append(original_duration // speed_factor) # 再生速度調整
30
-
31
- # Pillowのループ設定の仕様に合わせて調整
32
- adjusted_loop_count = 0 if loop_count == 0 else loop_count - 1
33
-
34
- # 保存
35
  output_path = f"{base_name}_edited.gif"
36
- frames[0].save(
37
- output_path,
38
- save_all=True,
39
- append_images=frames[1:],
40
- loop=adjusted_loop_count, # 修正されたループ回数
41
- duration=durations,
42
- disposal=2
43
- )
44
-
45
  return output_path, output_path
46
 
47
  # Gradioインターフェース
 
1
  import gradio as gr
2
+ from moviepy.editor import VideoFileClip
 
3
 
4
  def edit_file(file, loop_count, speed_factor, frame_skip):
5
  file_name = file.name.split("/")[-1]
6
  base_name = file_name.rsplit(".", 1)[0]
7
+
8
  # 動画またはGIFの読み込み
9
+ clip = VideoFileClip(file.name)
10
+
11
+ # フレームスキップと速度調整
12
+ if frame_skip > 1:
13
+ clip = clip.subclip(0, clip.duration).set_fps(clip.fps / frame_skip)
14
+ if speed_factor != 1:
15
+ clip = clip.fx(VideoFileClip.speedx, factor=speed_factor)
16
+
17
+ # GIFとして書き出し
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  output_path = f"{base_name}_edited.gif"
19
+ clip.write_gif(output_path, loop=loop_count)
20
+
 
 
 
 
 
 
 
21
  return output_path, output_path
22
 
23
  # Gradioインターフェース