ffmpeg-tools 0.1.4__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.
- ffmpeg_tools-0.1.4/LICENSE +21 -0
- ffmpeg_tools-0.1.4/PKG-INFO +77 -0
- ffmpeg_tools-0.1.4/README.md +52 -0
- ffmpeg_tools-0.1.4/ffmpeg_tools/__init__.py +12 -0
- ffmpeg_tools-0.1.4/ffmpeg_tools/commands.py +314 -0
- ffmpeg_tools-0.1.4/ffmpeg_tools/downloader.py +48 -0
- ffmpeg_tools-0.1.4/ffmpeg_tools/media.py +95 -0
- ffmpeg_tools-0.1.4/ffmpeg_tools.egg-info/PKG-INFO +77 -0
- ffmpeg_tools-0.1.4/ffmpeg_tools.egg-info/SOURCES.txt +12 -0
- ffmpeg_tools-0.1.4/ffmpeg_tools.egg-info/dependency_links.txt +1 -0
- ffmpeg_tools-0.1.4/ffmpeg_tools.egg-info/requires.txt +1 -0
- ffmpeg_tools-0.1.4/ffmpeg_tools.egg-info/top_level.txt +1 -0
- ffmpeg_tools-0.1.4/setup.cfg +4 -0
- ffmpeg_tools-0.1.4/setup.py +22 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sudeep Sudhevan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ffmpeg_tools
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Summary: A Python package for high-quality FFmpeg command generation and video utilities.
|
|
5
|
+
Home-page: https://github.com/sudeepsudhevan/FFmpeg_pip
|
|
6
|
+
Author: Sudeep Sudhevan
|
|
7
|
+
Author-email: sudeepsudhevan66@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.7
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: yt-dlp
|
|
15
|
+
Dynamic: author
|
|
16
|
+
Dynamic: author-email
|
|
17
|
+
Dynamic: classifier
|
|
18
|
+
Dynamic: description
|
|
19
|
+
Dynamic: description-content-type
|
|
20
|
+
Dynamic: home-page
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
Dynamic: requires-dist
|
|
23
|
+
Dynamic: requires-python
|
|
24
|
+
Dynamic: summary
|
|
25
|
+
|
|
26
|
+
# FFmpeg Tools
|
|
27
|
+
|
|
28
|
+
A Python package for simplified FFmpeg command generation, video downloading, and file management.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install ffmpeg-tools
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
### 1. Generating FFmpeg Commands
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from ffmpeg_tools import build_command
|
|
42
|
+
|
|
43
|
+
# Generate a high-quality compression command
|
|
44
|
+
cmd = build_command("compress_high_quality", input="video.mp4", output="compressed.mp4")
|
|
45
|
+
print(cmd)
|
|
46
|
+
# Output: ['ffmpeg', '-y', '-i', 'video.mp4', ...]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### 2. Downloading Videos
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from pathlib import Path
|
|
53
|
+
from ffmpeg_tools import download_video
|
|
54
|
+
|
|
55
|
+
# Download a video from YouTube
|
|
56
|
+
download_video(
|
|
57
|
+
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
|
58
|
+
output_folder=Path("downloads"),
|
|
59
|
+
clear_folder=False
|
|
60
|
+
)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 3. Cleaning Filenames
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from pathlib import Path
|
|
67
|
+
from ffmpeg_tools import clean_filename
|
|
68
|
+
|
|
69
|
+
file_path = Path("downloads/My Video (2024).mp4")
|
|
70
|
+
new_path = clean_filename(file_path)
|
|
71
|
+
print(new_path)
|
|
72
|
+
# Output: downloads/My_Video_2024.mp4
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# FFmpeg Tools
|
|
2
|
+
|
|
3
|
+
A Python package for simplified FFmpeg command generation, video downloading, and file management.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install ffmpeg-tools
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### 1. Generating FFmpeg Commands
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from ffmpeg_tools import build_command
|
|
17
|
+
|
|
18
|
+
# Generate a high-quality compression command
|
|
19
|
+
cmd = build_command("compress_high_quality", input="video.mp4", output="compressed.mp4")
|
|
20
|
+
print(cmd)
|
|
21
|
+
# Output: ['ffmpeg', '-y', '-i', 'video.mp4', ...]
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### 2. Downloading Videos
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from pathlib import Path
|
|
28
|
+
from ffmpeg_tools import download_video
|
|
29
|
+
|
|
30
|
+
# Download a video from YouTube
|
|
31
|
+
download_video(
|
|
32
|
+
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
|
33
|
+
output_folder=Path("downloads"),
|
|
34
|
+
clear_folder=False
|
|
35
|
+
)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 3. Cleaning Filenames
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from pathlib import Path
|
|
42
|
+
from ffmpeg_tools import clean_filename
|
|
43
|
+
|
|
44
|
+
file_path = Path("downloads/My Video (2024).mp4")
|
|
45
|
+
new_path = clean_filename(file_path)
|
|
46
|
+
print(new_path)
|
|
47
|
+
# Output: downloads/My_Video_2024.mp4
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
MIT
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ffmpeg_tools
|
|
3
|
+
~~~~~~~~~~~~
|
|
4
|
+
|
|
5
|
+
A simple Python package for FFmpeg command generation, video downloading, and file management.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .commands import build_command, get_all_commands, run_command
|
|
9
|
+
from .media import clean_filename, has_video_stream, remove_video_files
|
|
10
|
+
from .downloader import download_video
|
|
11
|
+
|
|
12
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ffmpeg_tools.commands
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~
|
|
4
|
+
|
|
5
|
+
High-quality FFmpeg command templates and building utilities.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
import logging
|
|
10
|
+
import os
|
|
11
|
+
from copy import deepcopy
|
|
12
|
+
|
|
13
|
+
# Setup logger
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
# Hardcoded Baseline Commands
|
|
17
|
+
FFMPEG_COMMANDS = {
|
|
18
|
+
# =========================
|
|
19
|
+
# 🎯 BASELINE (GPU ACCELERATED)
|
|
20
|
+
# =========================
|
|
21
|
+
"base_gpu_quality": {
|
|
22
|
+
"command": [
|
|
23
|
+
"ffmpeg", "-y",
|
|
24
|
+
"-hwaccel", "cuda",
|
|
25
|
+
"-i", "{input}",
|
|
26
|
+
"-c:v", "h264_nvenc",
|
|
27
|
+
"-preset", "p6",
|
|
28
|
+
"-rc", "vbr",
|
|
29
|
+
"-cq", "19",
|
|
30
|
+
"-pix_fmt", "yuv420p",
|
|
31
|
+
"-c:a", "aac", "-b:a", "192k",
|
|
32
|
+
"{output}"
|
|
33
|
+
],
|
|
34
|
+
"description": "GPU accelerated H.264 encoding (High Quality)"
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
"trim_gpu_reencode": {
|
|
38
|
+
"command": [
|
|
39
|
+
"ffmpeg", "-y",
|
|
40
|
+
"-hwaccel", "cuda",
|
|
41
|
+
"-ss", "{start}", "-to", "{end}",
|
|
42
|
+
"-i", "{input}",
|
|
43
|
+
"-c:v", "h264_nvenc",
|
|
44
|
+
"-preset", "p4",
|
|
45
|
+
"-cq", "19",
|
|
46
|
+
"-c:a", "aac",
|
|
47
|
+
"{output}"
|
|
48
|
+
],
|
|
49
|
+
"description": "Fast GPU-based frame-accurate trimming"
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
"compress_gpu_h265": {
|
|
53
|
+
"command": [
|
|
54
|
+
"ffmpeg", "-y",
|
|
55
|
+
"-hwaccel", "cuda",
|
|
56
|
+
"-i", "{input}",
|
|
57
|
+
"-c:v", "hevc_nvenc",
|
|
58
|
+
"-preset", "p6",
|
|
59
|
+
"-rc", "vbr",
|
|
60
|
+
"-cq", "24",
|
|
61
|
+
"-c:a", "aac",
|
|
62
|
+
"{output}"
|
|
63
|
+
],
|
|
64
|
+
"description": "Ultra-fast H.265 compression via GPU"
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
"resize_gpu": {
|
|
68
|
+
"command": [
|
|
69
|
+
"ffmpeg", "-y",
|
|
70
|
+
"-hwaccel", "cuda",
|
|
71
|
+
"-hwaccel_output_format", "cuda",
|
|
72
|
+
"-i", "{input}",
|
|
73
|
+
"-vf", "scale_cuda={width}:{height}",
|
|
74
|
+
"-c:v", "h264_nvenc",
|
|
75
|
+
"-preset", "p4",
|
|
76
|
+
"-c:a", "aac",
|
|
77
|
+
"{output}"
|
|
78
|
+
],
|
|
79
|
+
"description": "Resize video entirely on GPU (no CPU bottleneck)"
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
# =========================
|
|
83
|
+
# 🎯 BASELINE (CPU BEST QUALITY)
|
|
84
|
+
# =========================
|
|
85
|
+
"base_best_quality": {
|
|
86
|
+
"command": [
|
|
87
|
+
"ffmpeg", "-y",
|
|
88
|
+
"-i", "{input}",
|
|
89
|
+
"-map", "0:v:0",
|
|
90
|
+
"-map", "0:a:0?",
|
|
91
|
+
"-c:v", "libx264",
|
|
92
|
+
"-preset", "slow",
|
|
93
|
+
"-crf", "18",
|
|
94
|
+
"-pix_fmt", "yuv420p",
|
|
95
|
+
"-profile:v", "high",
|
|
96
|
+
"-level", "4.1",
|
|
97
|
+
"-c:a", "aac",
|
|
98
|
+
"-b:a", "192k",
|
|
99
|
+
"-movflags", "+faststart",
|
|
100
|
+
"{output}"
|
|
101
|
+
],
|
|
102
|
+
"description": "Visually lossless video + high quality AAC audio"
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
"trim_reencode": {
|
|
106
|
+
"command": [
|
|
107
|
+
"ffmpeg", "-y",
|
|
108
|
+
"-ss", "{start}",
|
|
109
|
+
"-to", "{end}",
|
|
110
|
+
"-i", "{input}",
|
|
111
|
+
"-c:v", "libx264",
|
|
112
|
+
"-preset", "slow",
|
|
113
|
+
"-crf", "18",
|
|
114
|
+
"-c:a", "aac",
|
|
115
|
+
"-b:a", "192k",
|
|
116
|
+
"-movflags", "+faststart",
|
|
117
|
+
"{output}"
|
|
118
|
+
],
|
|
119
|
+
"description": "Frame-accurate trimming with re-encoding"
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
"trim_copy": {
|
|
123
|
+
"command": [
|
|
124
|
+
"ffmpeg", "-y",
|
|
125
|
+
"-ss", "{start}",
|
|
126
|
+
"-to", "{end}",
|
|
127
|
+
"-i", "{input}",
|
|
128
|
+
"-c", "copy",
|
|
129
|
+
"{output}"
|
|
130
|
+
],
|
|
131
|
+
"description": "Fast trim without quality loss (keyframe based)"
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
"split_segments": {
|
|
135
|
+
"command": [
|
|
136
|
+
"ffmpeg", "-y",
|
|
137
|
+
"-i", "{input}",
|
|
138
|
+
"-map", "0",
|
|
139
|
+
"-c", "copy",
|
|
140
|
+
"-f", "segment",
|
|
141
|
+
"-segment_time", "{duration}",
|
|
142
|
+
"-reset_timestamps", "1",
|
|
143
|
+
"{output_pattern}"
|
|
144
|
+
],
|
|
145
|
+
"description": "Split video into equal-length segments"
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
"compress_high_quality": {
|
|
149
|
+
"command": [
|
|
150
|
+
"ffmpeg", "-y",
|
|
151
|
+
"-i", "{input}",
|
|
152
|
+
"-c:v", "libx264",
|
|
153
|
+
"-preset", "slow",
|
|
154
|
+
"-crf", "23",
|
|
155
|
+
"-c:a", "aac",
|
|
156
|
+
"-b:a", "160k",
|
|
157
|
+
"-movflags", "+faststart",
|
|
158
|
+
"{output}"
|
|
159
|
+
],
|
|
160
|
+
"description": "Balanced compression (YouTube-grade quality)"
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
"compress_ultra": {
|
|
164
|
+
"command": [
|
|
165
|
+
"ffmpeg", "-y",
|
|
166
|
+
"-i", "{input}",
|
|
167
|
+
"-c:v", "libx265",
|
|
168
|
+
"-preset", "slow",
|
|
169
|
+
"-crf", "28",
|
|
170
|
+
"-c:a", "aac",
|
|
171
|
+
"-b:a", "128k",
|
|
172
|
+
"{output}"
|
|
173
|
+
],
|
|
174
|
+
"description": "Maximum compression using H.265"
|
|
175
|
+
},
|
|
176
|
+
|
|
177
|
+
"extract_audio_wav": {
|
|
178
|
+
"command": [
|
|
179
|
+
"ffmpeg", "-y",
|
|
180
|
+
"-i", "{input}",
|
|
181
|
+
"-vn",
|
|
182
|
+
"-c:a", "pcm_s16le",
|
|
183
|
+
"{output}"
|
|
184
|
+
],
|
|
185
|
+
"description": "Extract lossless WAV audio"
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
"extract_audio_aac": {
|
|
189
|
+
"command": [
|
|
190
|
+
"ffmpeg", "-y",
|
|
191
|
+
"-i", "{input}",
|
|
192
|
+
"-vn",
|
|
193
|
+
"-c:a", "aac",
|
|
194
|
+
"-b:a", "192k",
|
|
195
|
+
"{output}"
|
|
196
|
+
],
|
|
197
|
+
"description": "Extract high-quality AAC audio"
|
|
198
|
+
},
|
|
199
|
+
|
|
200
|
+
"extract_video_only": {
|
|
201
|
+
"command": [
|
|
202
|
+
"ffmpeg", "-y",
|
|
203
|
+
"-i", "{input}",
|
|
204
|
+
"-an",
|
|
205
|
+
"-c:v", "libx264",
|
|
206
|
+
"-preset", "slow",
|
|
207
|
+
"-crf", "18",
|
|
208
|
+
"{output}"
|
|
209
|
+
],
|
|
210
|
+
"description": "Extract video stream only"
|
|
211
|
+
},
|
|
212
|
+
|
|
213
|
+
"resize_video": {
|
|
214
|
+
"command": [
|
|
215
|
+
"ffmpeg", "-y",
|
|
216
|
+
"-i", "{input}",
|
|
217
|
+
"-vf", "scale={width}:{height}:flags=lanczos",
|
|
218
|
+
"-c:v", "libx264",
|
|
219
|
+
"-preset", "slow",
|
|
220
|
+
"-crf", "18",
|
|
221
|
+
"-c:a", "aac",
|
|
222
|
+
"-b:a", "192k",
|
|
223
|
+
"{output}"
|
|
224
|
+
],
|
|
225
|
+
"description": "Resize video using high-quality Lanczos scaling"
|
|
226
|
+
},
|
|
227
|
+
|
|
228
|
+
"remux_copy": {
|
|
229
|
+
"command": [
|
|
230
|
+
"ffmpeg", "-y",
|
|
231
|
+
"-i", "{input}",
|
|
232
|
+
"-c", "copy",
|
|
233
|
+
"{output}"
|
|
234
|
+
],
|
|
235
|
+
"description": "Change container format without re-encoding"
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def get_all_commands(db_path: str = None) -> dict:
|
|
241
|
+
"""
|
|
242
|
+
Returns the complete dictionary of available FFmpeg commands.
|
|
243
|
+
Merges hardcoded commands with an optional external JSON file.
|
|
244
|
+
"""
|
|
245
|
+
all_cmds = deepcopy(FFMPEG_COMMANDS)
|
|
246
|
+
|
|
247
|
+
if db_path and os.path.exists(db_path):
|
|
248
|
+
try:
|
|
249
|
+
with open(db_path, "r", encoding="utf-8") as f:
|
|
250
|
+
external_cmds = json.load(f)
|
|
251
|
+
all_cmds.update(external_cmds)
|
|
252
|
+
except (json.JSONDecodeError, IOError) as e:
|
|
253
|
+
logger.warning(f"Failed to load external commands from {db_path}: {e}")
|
|
254
|
+
|
|
255
|
+
return all_cmds
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def build_command(profile: str, db_path: str = None, **kwargs) -> list[str]:
|
|
259
|
+
"""
|
|
260
|
+
Builds the FFmpeg command list for a given profile.
|
|
261
|
+
|
|
262
|
+
Args:
|
|
263
|
+
profile (str): The key/name of the command profile.
|
|
264
|
+
db_path (str, optional): Path to external JSON command DB.
|
|
265
|
+
**kwargs: Arguments to format the command template (e.g., input, output, start, end).
|
|
266
|
+
|
|
267
|
+
Returns:
|
|
268
|
+
list[str]: The formatted command list ready for subprocess.
|
|
269
|
+
|
|
270
|
+
Raises:
|
|
271
|
+
KeyError: If profile is not found or required arguments are missing.
|
|
272
|
+
"""
|
|
273
|
+
commands = get_all_commands(db_path)
|
|
274
|
+
|
|
275
|
+
if profile not in commands:
|
|
276
|
+
raise KeyError(f"Profile '{profile}' not found.")
|
|
277
|
+
|
|
278
|
+
template = commands[profile]["command"]
|
|
279
|
+
|
|
280
|
+
try:
|
|
281
|
+
# Format each argument in the command list
|
|
282
|
+
return [arg.format(**kwargs) for arg in template]
|
|
283
|
+
except KeyError as e:
|
|
284
|
+
missing_key = e.args[0]
|
|
285
|
+
raise KeyError(f"Missing required parameter '{missing_key}' for profile '{profile}'.")
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def run_command(command: list[str], dry_run: bool = False) -> bool:
|
|
289
|
+
"""
|
|
290
|
+
Executes the given FFmpeg command list using subprocess.
|
|
291
|
+
|
|
292
|
+
Args:
|
|
293
|
+
command (list[str]): The command list (e.g., from build_command).
|
|
294
|
+
dry_run (bool): If True, prints the command without executing.
|
|
295
|
+
|
|
296
|
+
Returns:
|
|
297
|
+
bool: True if successful, False otherwise.
|
|
298
|
+
"""
|
|
299
|
+
if dry_run:
|
|
300
|
+
print(f"Dry run: {' '.join(command)}")
|
|
301
|
+
return True
|
|
302
|
+
|
|
303
|
+
try:
|
|
304
|
+
import subprocess
|
|
305
|
+
logger.info(f"Running command: {' '.join(command)}")
|
|
306
|
+
subprocess.run(command, check=True)
|
|
307
|
+
return True
|
|
308
|
+
except subprocess.CalledProcessError as e:
|
|
309
|
+
logger.error(f"FFmpeg execution failed: {e}")
|
|
310
|
+
return False
|
|
311
|
+
except FileNotFoundError:
|
|
312
|
+
logger.error("FFmpeg not found. Please ensure ffmpeg is in your system PATH.")
|
|
313
|
+
return False
|
|
314
|
+
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ffmpeg_tools.downloader
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~
|
|
4
|
+
|
|
5
|
+
Wrapper for downloading videos using yt-dlp.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import subprocess
|
|
9
|
+
import logging
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from .media import remove_video_files
|
|
12
|
+
|
|
13
|
+
# Setup logger
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def download_video(url: str, output_folder: Path, clear_folder: bool = False) -> None:
|
|
18
|
+
"""
|
|
19
|
+
Downloads a YouTube video to the specified folder using yt-dlp.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
url (str): The YouTube URL.
|
|
23
|
+
output_folder (Path): Destination directory.
|
|
24
|
+
clear_folder (bool): If True, deletes existing video files in the folder before download.
|
|
25
|
+
"""
|
|
26
|
+
output_folder.mkdir(parents=True, exist_ok=True)
|
|
27
|
+
|
|
28
|
+
if clear_folder:
|
|
29
|
+
removed_count = remove_video_files(output_folder)
|
|
30
|
+
logger.info(f"Cleared {removed_count} video files from {output_folder}")
|
|
31
|
+
|
|
32
|
+
# Template: Folder/Title.ext
|
|
33
|
+
output_template = str(output_folder / "%(title)s.%(ext)s")
|
|
34
|
+
|
|
35
|
+
command = [
|
|
36
|
+
"yt-dlp",
|
|
37
|
+
"-f", "bestvideo+bestaudio/best",
|
|
38
|
+
"-o", output_template,
|
|
39
|
+
url,
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
logger.info(f"Starting download: {url}")
|
|
43
|
+
try:
|
|
44
|
+
subprocess.run(command, check=True)
|
|
45
|
+
logger.info("Download completed successfully.")
|
|
46
|
+
except subprocess.CalledProcessError as e:
|
|
47
|
+
logger.error(f"Download failed: {e}")
|
|
48
|
+
raise
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ffmpeg_tools.media
|
|
3
|
+
~~~~~~~~~~~~~~~~~~
|
|
4
|
+
|
|
5
|
+
Utilities for file handling, cleaning, and validation of media files.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import re
|
|
9
|
+
import mimetypes
|
|
10
|
+
import logging
|
|
11
|
+
import subprocess
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Optional, List
|
|
14
|
+
|
|
15
|
+
# Setup logger
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def has_video_stream(file_path: Path) -> bool:
|
|
20
|
+
"""
|
|
21
|
+
Checks if a file contains a video stream using ffprobe.
|
|
22
|
+
"""
|
|
23
|
+
if not file_path.exists():
|
|
24
|
+
return False
|
|
25
|
+
|
|
26
|
+
cmd = [
|
|
27
|
+
"ffprobe",
|
|
28
|
+
"-v", "error",
|
|
29
|
+
"-select_streams", "v:0",
|
|
30
|
+
"-show_entries", "stream=codec_type",
|
|
31
|
+
"-of", "csv=p=0",
|
|
32
|
+
str(file_path)
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
|
37
|
+
return result.returncode == 0 and result.stdout.strip() == "video"
|
|
38
|
+
except Exception as e:
|
|
39
|
+
logger.error(f"Error checking video stream for '{file_path}': {e}")
|
|
40
|
+
return False
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def clean_filename(file_path: Path) -> Path:
|
|
44
|
+
"""
|
|
45
|
+
Renames a file to be 'clean' (no spaces, special chars).
|
|
46
|
+
Returns the new Path object.
|
|
47
|
+
|
|
48
|
+
Example: "My Video (2024).mp4" -> "My_Video_2024.mp4"
|
|
49
|
+
"""
|
|
50
|
+
if not file_path.exists():
|
|
51
|
+
raise FileNotFoundError(f"File not found: {file_path}")
|
|
52
|
+
|
|
53
|
+
original_stem = file_path.stem
|
|
54
|
+
# Remove wrappers like [] () and replace spaces with underscores
|
|
55
|
+
clean_stem = re.sub(r"[_\[\]\(\)]", "", original_stem).replace(" ", "_")
|
|
56
|
+
|
|
57
|
+
# Ensure it's not empty, fallback to 'video' if everything was stripped
|
|
58
|
+
if not clean_stem:
|
|
59
|
+
clean_stem = "video"
|
|
60
|
+
|
|
61
|
+
new_name = f"{clean_stem}{file_path.suffix}"
|
|
62
|
+
new_path = file_path.parent / new_name
|
|
63
|
+
|
|
64
|
+
if new_path != file_path:
|
|
65
|
+
try:
|
|
66
|
+
file_path.rename(new_path)
|
|
67
|
+
logger.info(f"Renamed '{file_path.name}' -> '{new_name}'")
|
|
68
|
+
except OSError as e:
|
|
69
|
+
logger.error(f"Failed to rename '{file_path}': {e}")
|
|
70
|
+
raise
|
|
71
|
+
|
|
72
|
+
return new_path
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def remove_video_files(folder: Path) -> int:
|
|
76
|
+
"""
|
|
77
|
+
Removes all video files from the specified folder.
|
|
78
|
+
Returns the count of removed files.
|
|
79
|
+
"""
|
|
80
|
+
if not folder.exists():
|
|
81
|
+
return 0
|
|
82
|
+
|
|
83
|
+
count = 0
|
|
84
|
+
for file in folder.iterdir():
|
|
85
|
+
if file.is_file():
|
|
86
|
+
mime, _ = mimetypes.guess_type(file)
|
|
87
|
+
# Basic mime check, can be expanded or use has_video_stream for strictness
|
|
88
|
+
if mime and mime.startswith("video"):
|
|
89
|
+
try:
|
|
90
|
+
file.unlink()
|
|
91
|
+
count += 1
|
|
92
|
+
logger.debug(f"Deleted: {file.name}")
|
|
93
|
+
except OSError as e:
|
|
94
|
+
logger.error(f"Failed to delete '{file}': {e}")
|
|
95
|
+
return count
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ffmpeg_tools
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Summary: A Python package for high-quality FFmpeg command generation and video utilities.
|
|
5
|
+
Home-page: https://github.com/sudeepsudhevan/FFmpeg_pip
|
|
6
|
+
Author: Sudeep Sudhevan
|
|
7
|
+
Author-email: sudeepsudhevan66@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.7
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: yt-dlp
|
|
15
|
+
Dynamic: author
|
|
16
|
+
Dynamic: author-email
|
|
17
|
+
Dynamic: classifier
|
|
18
|
+
Dynamic: description
|
|
19
|
+
Dynamic: description-content-type
|
|
20
|
+
Dynamic: home-page
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
Dynamic: requires-dist
|
|
23
|
+
Dynamic: requires-python
|
|
24
|
+
Dynamic: summary
|
|
25
|
+
|
|
26
|
+
# FFmpeg Tools
|
|
27
|
+
|
|
28
|
+
A Python package for simplified FFmpeg command generation, video downloading, and file management.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install ffmpeg-tools
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
### 1. Generating FFmpeg Commands
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from ffmpeg_tools import build_command
|
|
42
|
+
|
|
43
|
+
# Generate a high-quality compression command
|
|
44
|
+
cmd = build_command("compress_high_quality", input="video.mp4", output="compressed.mp4")
|
|
45
|
+
print(cmd)
|
|
46
|
+
# Output: ['ffmpeg', '-y', '-i', 'video.mp4', ...]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### 2. Downloading Videos
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from pathlib import Path
|
|
53
|
+
from ffmpeg_tools import download_video
|
|
54
|
+
|
|
55
|
+
# Download a video from YouTube
|
|
56
|
+
download_video(
|
|
57
|
+
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
|
58
|
+
output_folder=Path("downloads"),
|
|
59
|
+
clear_folder=False
|
|
60
|
+
)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 3. Cleaning Filenames
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from pathlib import Path
|
|
67
|
+
from ffmpeg_tools import clean_filename
|
|
68
|
+
|
|
69
|
+
file_path = Path("downloads/My Video (2024).mp4")
|
|
70
|
+
new_path = clean_filename(file_path)
|
|
71
|
+
print(new_path)
|
|
72
|
+
# Output: downloads/My_Video_2024.mp4
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
setup.py
|
|
4
|
+
ffmpeg_tools/__init__.py
|
|
5
|
+
ffmpeg_tools/commands.py
|
|
6
|
+
ffmpeg_tools/downloader.py
|
|
7
|
+
ffmpeg_tools/media.py
|
|
8
|
+
ffmpeg_tools.egg-info/PKG-INFO
|
|
9
|
+
ffmpeg_tools.egg-info/SOURCES.txt
|
|
10
|
+
ffmpeg_tools.egg-info/dependency_links.txt
|
|
11
|
+
ffmpeg_tools.egg-info/requires.txt
|
|
12
|
+
ffmpeg_tools.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
yt-dlp
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ffmpeg_tools
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name="ffmpeg_tools",
|
|
5
|
+
version="0.1.4",
|
|
6
|
+
description="A Python package for high-quality FFmpeg command generation and video utilities.",
|
|
7
|
+
long_description=open("README.md", "r", encoding="utf-8").read(),
|
|
8
|
+
long_description_content_type="text/markdown",
|
|
9
|
+
author="Sudeep Sudhevan",
|
|
10
|
+
author_email="sudeepsudhevan66@gmail.com",
|
|
11
|
+
url="https://github.com/sudeepsudhevan/FFmpeg_pip",
|
|
12
|
+
packages=find_packages(),
|
|
13
|
+
install_requires=[
|
|
14
|
+
"yt-dlp",
|
|
15
|
+
],
|
|
16
|
+
classifiers=[
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
],
|
|
21
|
+
python_requires='>=3.7',
|
|
22
|
+
)
|