videopython 0.2.1__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of videopython might be problematic. Click here for more details.

Files changed (28) hide show
  1. {videopython-0.2.1 → videopython-0.4.0}/.gitignore +4 -1
  2. {videopython-0.2.1 → videopython-0.4.0}/PKG-INFO +13 -25
  3. {videopython-0.2.1 → videopython-0.4.0}/README.md +10 -10
  4. {videopython-0.2.1 → videopython-0.4.0}/pyproject.toml +32 -32
  5. {videopython-0.2.1/src/videopython → videopython-0.4.0/src/videopython/ai}/generation/audio.py +25 -13
  6. {videopython-0.2.1/src/videopython → videopython-0.4.0/src/videopython/ai}/generation/image.py +0 -3
  7. videopython-0.4.0/src/videopython/ai/understanding/transcribe.py +37 -0
  8. {videopython-0.2.1 → videopython-0.4.0}/src/videopython/base/effects.py +3 -3
  9. videopython-0.4.0/src/videopython/base/transcription.py +13 -0
  10. {videopython-0.2.1 → videopython-0.4.0}/src/videopython/base/transforms.py +0 -2
  11. {videopython-0.2.1 → videopython-0.4.0}/src/videopython/base/transitions.py +2 -2
  12. videopython-0.4.0/src/videopython/base/video.py +440 -0
  13. videopython-0.4.0/src/videopython/py.typed +0 -0
  14. videopython-0.4.0/src/videopython/utils/__init__.py +3 -0
  15. videopython-0.4.0/src/videopython/utils/image.py +47 -0
  16. videopython-0.4.0/src/videopython/utils/text.py +727 -0
  17. videopython-0.2.1/src/videopython/base/video.py +0 -358
  18. videopython-0.2.1/src/videopython/utils/image.py +0 -275
  19. {videopython-0.2.1 → videopython-0.4.0}/LICENSE +0 -0
  20. {videopython-0.2.1 → videopython-0.4.0}/src/videopython/__init__.py +0 -0
  21. {videopython-0.2.1/src/videopython/base → videopython-0.4.0/src/videopython/ai}/__init__.py +0 -0
  22. {videopython-0.2.1/src/videopython → videopython-0.4.0/src/videopython/ai}/generation/__init__.py +0 -0
  23. {videopython-0.2.1/src/videopython → videopython-0.4.0/src/videopython/ai}/generation/video.py +0 -0
  24. {videopython-0.2.1/src/videopython/utils → videopython-0.4.0/src/videopython/ai/understanding}/__init__.py +0 -0
  25. /videopython-0.2.1/src/videopython/py.typed → /videopython-0.4.0/src/videopython/base/__init__.py +0 -0
  26. {videopython-0.2.1 → videopython-0.4.0}/src/videopython/base/compose.py +0 -0
  27. {videopython-0.2.1 → videopython-0.4.0}/src/videopython/base/exceptions.py +0 -0
  28. {videopython-0.2.1 → videopython-0.4.0}/src/videopython/utils/common.py +0 -0
@@ -137,4 +137,7 @@ dmypy.json
137
137
  # Data directories
138
138
  data/downloaded/*.mp4
139
139
  data/exported/*.mp4
140
- !data/exported/example.mp4
140
+ !data/exported/example.mp4
141
+
142
+ # Mac
143
+ *.DS_Store
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: videopython
3
- Version: 0.2.1
3
+ Version: 0.4.0
4
4
  Summary: Minimal video generation and processing library.
5
5
  Project-URL: Homepage, https://github.com/bartwojtowicz/videopython/
6
6
  Project-URL: Repository, https://github.com/bartwojtowicz/videopython/
@@ -19,20 +19,8 @@ Requires-Dist: numpy>=1.25.2
19
19
  Requires-Dist: opencv-python>=4.9.0.80
20
20
  Requires-Dist: pillow>=10.3.0
21
21
  Requires-Dist: pydub>=0.25.1
22
+ Requires-Dist: soundpython>=0.1.11
22
23
  Requires-Dist: tqdm>=4.66.3
23
- Provides-Extra: dev
24
- Requires-Dist: black==24.3.0; extra == 'dev'
25
- Requires-Dist: isort==5.12.0; extra == 'dev'
26
- Requires-Dist: mypy==1.8.0; extra == 'dev'
27
- Requires-Dist: pydub-stubs==0.25.1.1; extra == 'dev'
28
- Requires-Dist: pytest==7.4.0; extra == 'dev'
29
- Requires-Dist: types-pillow==10.2.0.20240213; extra == 'dev'
30
- Requires-Dist: types-tqdm==4.66.0.20240106; extra == 'dev'
31
- Provides-Extra: generation
32
- Requires-Dist: accelerate>=0.29.2; extra == 'generation'
33
- Requires-Dist: diffusers>=0.26.3; extra == 'generation'
34
- Requires-Dist: torch>=2.1.0; extra == 'generation'
35
- Requires-Dist: transformers>=4.38.1; extra == 'generation'
36
24
  Description-Content-Type: text/markdown
37
25
 
38
26
  # About
@@ -51,10 +39,10 @@ sudo apt-get install ffmpeg
51
39
 
52
40
  ### Install with pip
53
41
  ```bash
54
- pip install videopython[generation]
42
+ pip install videopython[ai]
55
43
  ```
56
- > You can install without `[generation]` dependencies for basic video handling and processing.
57
- > The funcionalities found in `videopython.generation` won't work.
44
+ > You can install without `[ai]` dependencies for basic video handling and processing.
45
+ > The funcionalities found in `videopython.ai` won't work.
58
46
 
59
47
  ## Basic Usage
60
48
 
@@ -64,10 +52,10 @@ pip install videopython[generation]
64
52
  from videopython.base.video import Video
65
53
 
66
54
  # Load videos and print metadata
67
- video1 = Video.from_path("tests/test_data/fast_benchmark.mp4")
55
+ video1 = Video.from_path("tests/test_data/small_video.mp4")
68
56
  print(video1)
69
57
 
70
- video2 = Video.from_path("tests/test_data/slow_benchmark.mp4")
58
+ video2 = Video.from_path("tests/test_data/big_video.mp4")
71
59
  print(video2)
72
60
 
73
61
  # Define the transformations
@@ -91,18 +79,18 @@ savepath = video.save()
91
79
 
92
80
  ### Video Generation
93
81
 
94
- > Using Nvidia A40 or better is recommended for the `videopython.generation` module.
82
+ > Using Nvidia A40 or better is recommended for the `videopython.ai` module.
95
83
  ```python
96
84
  # Generate image and animate it
97
- from videopython.generation import ImageToVideo
98
- from videopython.generation import TextToImage
99
- from videopython.generation import TextToMusic
85
+ from videopython.ai.generation import ImageToVideo
86
+ from videopython.ai.generation import TextToImage
87
+ from videopython.ai.generation import TextToMusic
100
88
 
101
89
  image = TextToImage().generate_image(prompt="Golden Retriever playing in the park")
102
90
  video = ImageToVideo().generate_video(image=image, fps=24)
103
91
 
104
92
  # Video generation directly from prompt
105
- from videopython.generation import TextToVideo
93
+ from videopython.ai.generation import TextToVideo
106
94
  video_gen = TextToVideo()
107
95
  video = video_gen.generate_video("Dogs playing in the snow")
108
96
  for _ in range(10):
@@ -14,10 +14,10 @@ sudo apt-get install ffmpeg
14
14
 
15
15
  ### Install with pip
16
16
  ```bash
17
- pip install videopython[generation]
17
+ pip install videopython[ai]
18
18
  ```
19
- > You can install without `[generation]` dependencies for basic video handling and processing.
20
- > The funcionalities found in `videopython.generation` won't work.
19
+ > You can install without `[ai]` dependencies for basic video handling and processing.
20
+ > The funcionalities found in `videopython.ai` won't work.
21
21
 
22
22
  ## Basic Usage
23
23
 
@@ -27,10 +27,10 @@ pip install videopython[generation]
27
27
  from videopython.base.video import Video
28
28
 
29
29
  # Load videos and print metadata
30
- video1 = Video.from_path("tests/test_data/fast_benchmark.mp4")
30
+ video1 = Video.from_path("tests/test_data/small_video.mp4")
31
31
  print(video1)
32
32
 
33
- video2 = Video.from_path("tests/test_data/slow_benchmark.mp4")
33
+ video2 = Video.from_path("tests/test_data/big_video.mp4")
34
34
  print(video2)
35
35
 
36
36
  # Define the transformations
@@ -54,18 +54,18 @@ savepath = video.save()
54
54
 
55
55
  ### Video Generation
56
56
 
57
- > Using Nvidia A40 or better is recommended for the `videopython.generation` module.
57
+ > Using Nvidia A40 or better is recommended for the `videopython.ai` module.
58
58
  ```python
59
59
  # Generate image and animate it
60
- from videopython.generation import ImageToVideo
61
- from videopython.generation import TextToImage
62
- from videopython.generation import TextToMusic
60
+ from videopython.ai.generation import ImageToVideo
61
+ from videopython.ai.generation import TextToImage
62
+ from videopython.ai.generation import TextToMusic
63
63
 
64
64
  image = TextToImage().generate_image(prompt="Golden Retriever playing in the park")
65
65
  video = ImageToVideo().generate_video(image=image, fps=24)
66
66
 
67
67
  # Video generation directly from prompt
68
- from videopython.generation import TextToVideo
68
+ from videopython.ai.generation import TextToVideo
69
69
  video_gen = TextToVideo()
70
70
  video = video_gen.generate_video("Dogs playing in the snow")
71
71
  for _ in range(10):
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "videopython"
3
- version = "0.2.1"
3
+ version = "0.4.0"
4
4
  description = "Minimal video generation and processing library."
5
5
  authors = [
6
6
  { name = "Bartosz Wójtowicz", email = "bartoszwojtowicz@outlook.com" },
@@ -24,24 +24,27 @@ dependencies = [
24
24
  "opencv-python>=4.9.0.80",
25
25
  "pillow>=10.3.0",
26
26
  "pydub>=0.25.1",
27
+ "soundpython>=0.1.11",
27
28
  "tqdm>=4.66.3",
29
+
28
30
  ]
29
31
 
30
- [project.optional-dependencies]
32
+ [dependency-groups]
31
33
  dev = [
32
- "black==24.3.0",
33
- "isort==5.12.0",
34
- "mypy==1.8.0",
35
- "pytest==7.4.0",
36
- "types-Pillow==10.2.0.20240213",
37
- "types-tqdm==4.66.0.20240106",
38
- "pydub-stubs==0.25.1.1",
34
+ "ruff>=0.1.14",
35
+ "mypy>=1.8.0",
36
+ "pytest>=7.4.0",
37
+ "types-Pillow>=10.2.0.20240213",
38
+ "types-tqdm>=4.66.0.20240106",
39
+ "pytest-cov>=6.1.1",
39
40
  ]
40
- generation = [
41
+ ai = [
41
42
  "accelerate>=0.29.2",
42
43
  "diffusers>=0.26.3",
43
44
  "torch>=2.1.0",
44
45
  "transformers>=4.38.1",
46
+ "openai-whisper>=20240930",
47
+ "numba>=0.61.0",
45
48
  ]
46
49
 
47
50
  [project.urls]
@@ -49,25 +52,8 @@ Homepage = "https://github.com/bartwojtowicz/videopython/"
49
52
  Repository = "https://github.com/bartwojtowicz/videopython/"
50
53
  Documentation = "https://github.com/bartwojtowicz/videopython/"
51
54
 
52
- [tool.rye]
53
- managed = true
54
- dev-dependencies = [
55
- "black==24.3.0",
56
- "isort==5.12.0",
57
- "mypy==1.8.0",
58
- "pytest==7.4.0",
59
- "types-Pillow==10.2.0.20240213",
60
- "types-tqdm==4.66.0.20240106",
61
- "pydub-stubs==0.25.1.1",
62
- ]
63
-
64
- [tool.rye.scripts]
65
- test-unit = "pytest"
66
- test-type = "mypy src"
67
- test-static = { chain = [
68
- "black src -l 120 --check",
69
- "isort src --profile black --check"
70
- ]}
55
+ [tool.mypy]
56
+ mypy_path = "stubs"
71
57
 
72
58
  [build-system]
73
59
  requires = ["hatchling"]
@@ -79,10 +65,24 @@ packages = ["src/videopython"]
79
65
  [tool.hatch.build.targets.sdist]
80
66
  include = ["src/videopython", "src/videopython/py.typed"]
81
67
 
82
- [tool.mypy]
83
- mypy_path = "stubs"
84
-
85
68
  [tool.pytest]
69
+ pythonpath = [".src/"]
86
70
  testpaths = ["src/tests"]
87
71
  python_files = ["test_*.py"]
88
72
  addopts = "-v --tb=short"
73
+
74
+ [tool.ruff]
75
+ line-length = 120
76
+ target-version = "py310"
77
+
78
+ [tool.ruff.lint]
79
+ select = [
80
+ "E", # pycodestyle errors
81
+ "F", # pyflakes
82
+ "I", # isort
83
+ ]
84
+ isort.known-first-party = ["videopython"]
85
+
86
+ [tool.ruff.format]
87
+ indent-style = "space"
88
+ quote-style = "double"
@@ -1,6 +1,5 @@
1
- import numpy as np
2
1
  import torch
3
- from pydub import AudioSegment
2
+ from soundpython import Audio, AudioMetadata
4
3
  from transformers import (
5
4
  AutoProcessor,
6
5
  AutoTokenizer,
@@ -17,15 +16,24 @@ class TextToSpeech:
17
16
  self.pipeline = VitsModel.from_pretrained(TEXT_TO_SPEECH_MODEL)
18
17
  self.tokenizer = AutoTokenizer.from_pretrained(TEXT_TO_SPEECH_MODEL)
19
18
 
20
- def generate_audio(self, text: str) -> AudioSegment:
19
+ def generate_audio(self, text: str) -> Audio:
21
20
  tokenized = self.tokenizer(text, return_tensors="pt")
22
21
 
23
22
  with torch.no_grad():
24
23
  output = self.pipeline(**tokenized).waveform
25
24
 
26
- output = (output.T.float().numpy() * (2**31 - 1)).astype(np.int32)
27
- audio = AudioSegment(data=output, frame_rate=self.pipeline.config.sampling_rate, sample_width=4, channels=1)
28
- return audio
25
+ # Convert to float32 and normalize to [-1, 1]
26
+ audio_data = output.T.float().numpy()
27
+
28
+ metadata = AudioMetadata(
29
+ sample_rate=self.pipeline.config.sampling_rate,
30
+ channels=1,
31
+ sample_width=4,
32
+ duration_seconds=len(audio_data) / self.pipeline.config.sampling_rate,
33
+ frame_count=len(audio_data),
34
+ )
35
+
36
+ return Audio(audio_data, metadata)
29
37
 
30
38
 
31
39
  class TextToMusic:
@@ -37,7 +45,7 @@ class TextToMusic:
37
45
  self.processor = AutoProcessor.from_pretrained(MUSIC_GENERATION_MODEL_SMALL)
38
46
  self.model = MusicgenForConditionalGeneration.from_pretrained(MUSIC_GENERATION_MODEL_SMALL)
39
47
 
40
- def generate_audio(self, text: str, max_new_tokens: int) -> AudioSegment:
48
+ def generate_audio(self, text: str, max_new_tokens: int) -> Audio:
41
49
  inputs = self.processor(
42
50
  text=[text],
43
51
  padding=True,
@@ -45,12 +53,16 @@ class TextToMusic:
45
53
  )
46
54
  audio_values = self.model.generate(**inputs, max_new_tokens=max_new_tokens)
47
55
  sampling_rate = self.model.config.audio_encoder.sampling_rate
48
- output = (audio_values[0, 0].float().numpy() * (2**31 - 1)).astype(np.int32)
49
56
 
50
- audio = AudioSegment(
51
- data=output.tobytes(),
52
- frame_rate=sampling_rate,
53
- sample_width=4,
57
+ # Convert to float32 and normalize to [-1, 1]
58
+ audio_data = audio_values[0, 0].float().numpy()
59
+
60
+ metadata = AudioMetadata(
61
+ sample_rate=sampling_rate,
54
62
  channels=1,
63
+ sample_width=4,
64
+ duration_seconds=len(audio_data) / sampling_rate,
65
+ frame_count=len(audio_data),
55
66
  )
56
- return audio
67
+
68
+ return Audio(audio_data, metadata)
@@ -1,6 +1,3 @@
1
- import io
2
- import os
3
-
4
1
  import torch
5
2
  from diffusers import DiffusionPipeline
6
3
  from PIL import Image
@@ -0,0 +1,37 @@
1
+ from typing import Literal
2
+
3
+ import whisper
4
+
5
+ from videopython.base.transcription import Transcription, TranscriptionSegment
6
+ from videopython.base.video import Video
7
+
8
+
9
+ class VideoTranscription:
10
+ def __init__(self, model_name: Literal["tiny", "base", "small", "medium", "large", "turbo"] = "small") -> None:
11
+ self.model = whisper.load_model(name=model_name)
12
+
13
+ def transcribe_video(self, video: Video) -> Transcription:
14
+ """Transcribes video to text.
15
+
16
+ Args:
17
+ video: Video to transcribe.
18
+
19
+ Returns:
20
+ List of dictionaries with segments of text and their start and end times.
21
+ """
22
+ if video.audio.is_silent:
23
+ return Transcription(segments=[])
24
+
25
+ audio = video.audio.to_mono()
26
+ audio = audio.resample(whisper.audio.SAMPLE_RATE)
27
+ audio_data = audio.data
28
+
29
+ transcription = self.model.transcribe(audio=audio_data, word_timestamps=True)
30
+
31
+ transcription_segments = [
32
+ TranscriptionSegment(start=segment["start"], end=segment["end"], text=segment["text"])
33
+ for segment in transcription["segments"]
34
+ ]
35
+ result = Transcription(segments=transcription_segments)
36
+
37
+ return result
@@ -156,13 +156,13 @@ class Zoom(Effect):
156
156
 
157
157
  width = video.metadata.width
158
158
  height = video.metadata.height
159
- crop_sizes_w, crop_sizes_h = np.linspace(width // self.zoom_factor, width, n_frames), np.linspace(
160
- height // self.zoom_factor, height, n_frames
159
+ crop_sizes_w, crop_sizes_h = (
160
+ np.linspace(width // self.zoom_factor, width, n_frames),
161
+ np.linspace(height // self.zoom_factor, height, n_frames),
161
162
  )
162
163
 
163
164
  if self.mode == "in":
164
165
  for frame, w, h in tqdm(zip(video.frames, reversed(crop_sizes_w), reversed(crop_sizes_h))):
165
-
166
166
  x = width / 2 - w / 2
167
167
  y = height / 2 - h / 2
168
168
 
@@ -0,0 +1,13 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class TranscriptionSegment:
6
+ start: float
7
+ end: float
8
+ text: str
9
+
10
+
11
+ @dataclass
12
+ class Transcription:
13
+ segments: list[TranscriptionSegment]
@@ -1,7 +1,6 @@
1
1
  from abc import ABC, abstractmethod
2
2
  from enum import Enum
3
3
  from multiprocessing import Pool
4
- from typing import Literal
5
4
 
6
5
  import cv2
7
6
  import numpy as np
@@ -154,7 +153,6 @@ class CropMode(Enum):
154
153
 
155
154
 
156
155
  class Crop(Transformation):
157
-
158
156
  def __init__(self, width: int, height: int, mode: CropMode = CropMode.CENTER):
159
157
  self.width = width
160
158
  self.height = height
@@ -67,7 +67,7 @@ class FadeTransition(Transition):
67
67
  ],
68
68
  fps=video_fps,
69
69
  )
70
- faded_videos.audio = videos[0].audio.append(videos[1].audio, crossfade=(effect_time_fps / video_fps) * 1000)
70
+ faded_videos.audio = videos[0].audio.concat(videos[1].audio, crossfade=(effect_time_fps / video_fps))
71
71
  return faded_videos
72
72
 
73
73
 
@@ -102,5 +102,5 @@ class BlurTransition(Transition):
102
102
  ],
103
103
  fps=video_fps,
104
104
  )
105
- blurred_videos.audio = videos[0].audio.append(videos[1].audio)
105
+ blurred_videos.audio = videos[0].audio.concat(videos[1].audio)
106
106
  return blurred_videos