better-ffmpeg-progress 2.2.0__tar.gz → 2.2.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: better-ffmpeg-progress
3
- Version: 2.2.0
3
+ Version: 2.2.2
4
4
  Summary: Run FFmpeg & see percentage progress + ETA.
5
5
  Home-page: https://github.com/CrypticSignal/better-ffmpeg-progress
6
6
  Author: GitHub.com/CrypticSignal
@@ -80,9 +80,9 @@ process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "out
80
80
 
81
81
  # Use the run method to run the FFmpeg command.
82
82
  process.run(
83
- output_file="ffmpeg_output.txt",
84
83
  progress_bar_description="Converting file...",
85
84
  progress_handler=handle_progress_info,
85
+ log_file="ffmpeg_log.txt",
86
86
  success_handler=handle_success,
87
87
  error_handler=handle_error,
88
88
  )
@@ -103,7 +103,7 @@ The `run` method takes the following **optional** arguments:
103
103
 
104
104
  The values will be `None` if unknown. The function will receive the current values of these metrics as arguments, every 0.1s.
105
105
 
106
- - `output_file` - A string path to define where you want the output of FFmpeg to be saved. By default, this is saved in a folder named "ffmpeg_output", with the filename `[<input_filename>].txt`.
106
+ - `log_file` - The filepath to save the FFmpeg log to.
107
107
 
108
108
  - `success_handler` - A function to run if the FFmpeg process completes successfully.
109
109
 
@@ -157,3 +157,9 @@ The `run` method takes the following **optional** arguments:
157
157
  [20/10/2024]
158
158
  - [v2.1.9] Do not clear the terminal before showing the progress bar.
159
159
  - [v2.2.0] Add the ability to set a custom description for the progress bar.
160
+
161
+ [22/10/2024]
162
+ - [v2.2.1] Only create a log file if the `log_file` parameter is specified and always create a log file if the FFmpeg process fails.
163
+
164
+ [28/10/2024]
165
+ - [v2.2.2] Make printing the detected duration of the input file optional.
@@ -63,9 +63,9 @@ process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "out
63
63
 
64
64
  # Use the run method to run the FFmpeg command.
65
65
  process.run(
66
- output_file="ffmpeg_output.txt",
67
66
  progress_bar_description="Converting file...",
68
67
  progress_handler=handle_progress_info,
68
+ log_file="ffmpeg_log.txt",
69
69
  success_handler=handle_success,
70
70
  error_handler=handle_error,
71
71
  )
@@ -86,7 +86,7 @@ The `run` method takes the following **optional** arguments:
86
86
 
87
87
  The values will be `None` if unknown. The function will receive the current values of these metrics as arguments, every 0.1s.
88
88
 
89
- - `output_file` - A string path to define where you want the output of FFmpeg to be saved. By default, this is saved in a folder named "ffmpeg_output", with the filename `[<input_filename>].txt`.
89
+ - `log_file` - The filepath to save the FFmpeg log to.
90
90
 
91
91
  - `success_handler` - A function to run if the FFmpeg process completes successfully.
92
92
 
@@ -140,3 +140,9 @@ The `run` method takes the following **optional** arguments:
140
140
  [20/10/2024]
141
141
  - [v2.1.9] Do not clear the terminal before showing the progress bar.
142
142
  - [v2.2.0] Add the ability to set a custom description for the progress bar.
143
+
144
+ [22/10/2024]
145
+ - [v2.2.1] Only create a log file if the `log_file` parameter is specified and always create a log file if the FFmpeg process fails.
146
+
147
+ [28/10/2024]
148
+ - [v2.2.2] Make printing the detected duration of the input file optional.
@@ -42,13 +42,19 @@ class FfmpegProcess:
42
42
 
43
43
  WANTED_METRICS = frozenset({"out_time_ms", "total_size", "speed"})
44
44
 
45
- def __init__(self, command: List[str], ffmpeg_loglevel: str = "verbose"):
45
+ def __init__(
46
+ self,
47
+ command: List[str],
48
+ ffmpeg_loglevel: str = "verbose",
49
+ print_detected_duration: bool = True,
50
+ ):
46
51
  if "-i" not in command:
47
52
  raise ValueError("FFmpeg command must include '-i'")
48
53
 
49
54
  self._ffmpeg_args = command + ["-hide_banner", "-loglevel", ffmpeg_loglevel]
50
55
  self._ffmpeg_stderr: List[str] = []
51
56
  self._output_filepath = Path(command[-1])
57
+ self._print_detected_duration = print_detected_duration
52
58
  self._metrics = Metrics()
53
59
  self._duration_secs: Optional[float] = None
54
60
  self._current_size: int = 0
@@ -64,9 +70,12 @@ class FfmpegProcess:
64
70
  self._duration_secs = float(
65
71
  probe(self._input_filepath)["format"]["duration"]
66
72
  )
67
- print(
68
- f"The duration of {self._input_filepath.name} has been detected as {self._duration_secs} seconds"
69
- )
73
+
74
+ if self._print_detected_duration:
75
+ print(
76
+ f"The duration of {self._input_filepath.name} has been detected as {self._duration_secs} seconds"
77
+ )
78
+
70
79
  # -progress pipe:1 sends progress metrics to stdout AND -stats_period sets the period at which encoding progress/statistics are updated
71
80
  # -nostats disables encoding progress/statistics in stderr
72
81
  self._ffmpeg_args.extend(
@@ -143,24 +152,23 @@ class FfmpegProcess:
143
152
  pipe.close()
144
153
 
145
154
  def _write_ffmpeg_output(
146
- self, stderr_output: str, output_file: Optional[Union[str, Path]]
155
+ self,
156
+ stderr_output: str,
157
+ log_file: Optional[Union[str, Path]],
158
+ is_error: bool = False,
147
159
  ) -> None:
148
160
  """Write FFmpeg stderr to a txt file."""
149
- output_dir = Path("ffmpeg_output")
150
- output_dir.mkdir(exist_ok=True)
161
+ Path(log_file).write_text(stderr_output)
151
162
 
152
- log_file = output_file or output_dir / f"[{self._input_filepath.name}].txt"
153
- log_file = Path(log_file)
163
+ if is_error:
164
+ return
154
165
 
155
- log_file.write_text(stderr_output)
156
- print(f"Done! FFmpeg log file: {log_file}")
166
+ print(f"FFmpeg log written to: {log_file}")
157
167
 
158
168
  def _start_process(
159
169
  self,
160
170
  progress_bar: Optional[Progress],
161
171
  task_id: Optional[int],
162
- success_handler: Optional[Callable],
163
- error_handler: Optional[Callable],
164
172
  progress_handler: Optional[Callable],
165
173
  ) -> None:
166
174
  process = subprocess.Popen(
@@ -213,18 +221,11 @@ class FfmpegProcess:
213
221
  except Empty:
214
222
  pass
215
223
 
216
- if process.returncode != 0:
217
- if error_handler:
218
- error_handler()
219
- else:
220
- sys.exit("\nFFmpeg process failed")
221
-
222
- if success_handler:
223
- success_handler()
224
+ return process.returncode
224
225
 
225
226
  def run(
226
227
  self,
227
- output_file: Optional[Union[str, Path]] = None,
228
+ log_file: Optional[Union[str, Path]] = None,
228
229
  progress_bar_description: str = None,
229
230
  progress_handler: Optional[Callable] = None,
230
231
  success_handler: Optional[Callable] = None,
@@ -232,7 +233,7 @@ class FfmpegProcess:
232
233
  ) -> None:
233
234
  """
234
235
  Args:
235
- output_file: Optional filepath to write FFmpeg output to
236
+ log_file: Optional filepath to write FFmpeg output to
236
237
  progress_bar_description: Optional string to set a custom description for the progress bar
237
238
  progress_handler: Optional function which receives progress metrics
238
239
  success_handler: Optional function to handle successful completion of the FFmpeg process
@@ -260,20 +261,41 @@ class FfmpegProcess:
260
261
  total=self._duration_secs,
261
262
  )
262
263
 
263
- self._start_process(
264
+ return_code = self._start_process(
264
265
  progress_bar,
265
266
  task_id,
266
- success_handler,
267
- error_handler,
268
- progress_handler,
267
+ None,
269
268
  )
270
269
  else:
271
- self._start_process(
270
+ return_code = self._start_process(
272
271
  None,
273
272
  None,
274
- success_handler,
275
- error_handler,
276
273
  progress_handler,
277
274
  )
278
275
 
279
- self._write_ffmpeg_output("\n".join(self._ffmpeg_stderr), output_file)
276
+ self._handle_process_ended(
277
+ return_code, error_handler, success_handler, log_file
278
+ )
279
+
280
+ def _handle_process_ended(
281
+ self, return_code, error_handler, success_handler, log_file
282
+ ):
283
+ if return_code != 0:
284
+ if error_handler:
285
+ error_handler()
286
+
287
+ ffmpeg_log_file = "ffmpeg_log.txt"
288
+
289
+ self._write_ffmpeg_output(
290
+ "\n".join(self._ffmpeg_stderr), ffmpeg_log_file, is_error=True
291
+ )
292
+
293
+ sys.exit(f"FFmpeg process failed. Check out {ffmpeg_log_file} for details")
294
+
295
+ if log_file:
296
+ self._write_ffmpeg_output("\n".join(self._ffmpeg_stderr), log_file)
297
+
298
+ if success_handler:
299
+ success_handler()
300
+ else:
301
+ print("FFmpeg process completed.")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: better-ffmpeg-progress
3
- Version: 2.2.0
3
+ Version: 2.2.2
4
4
  Summary: Run FFmpeg & see percentage progress + ETA.
5
5
  Home-page: https://github.com/CrypticSignal/better-ffmpeg-progress
6
6
  Author: GitHub.com/CrypticSignal
@@ -80,9 +80,9 @@ process = FfmpegProcess(["ffmpeg", "-i", "input.mp4", "-c:a", "libmp3lame", "out
80
80
 
81
81
  # Use the run method to run the FFmpeg command.
82
82
  process.run(
83
- output_file="ffmpeg_output.txt",
84
83
  progress_bar_description="Converting file...",
85
84
  progress_handler=handle_progress_info,
85
+ log_file="ffmpeg_log.txt",
86
86
  success_handler=handle_success,
87
87
  error_handler=handle_error,
88
88
  )
@@ -103,7 +103,7 @@ The `run` method takes the following **optional** arguments:
103
103
 
104
104
  The values will be `None` if unknown. The function will receive the current values of these metrics as arguments, every 0.1s.
105
105
 
106
- - `output_file` - A string path to define where you want the output of FFmpeg to be saved. By default, this is saved in a folder named "ffmpeg_output", with the filename `[<input_filename>].txt`.
106
+ - `log_file` - The filepath to save the FFmpeg log to.
107
107
 
108
108
  - `success_handler` - A function to run if the FFmpeg process completes successfully.
109
109
 
@@ -157,3 +157,9 @@ The `run` method takes the following **optional** arguments:
157
157
  [20/10/2024]
158
158
  - [v2.1.9] Do not clear the terminal before showing the progress bar.
159
159
  - [v2.2.0] Add the ability to set a custom description for the progress bar.
160
+
161
+ [22/10/2024]
162
+ - [v2.2.1] Only create a log file if the `log_file` parameter is specified and always create a log file if the FFmpeg process fails.
163
+
164
+ [28/10/2024]
165
+ - [v2.2.2] Make printing the detected duration of the input file optional.
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as f:
5
5
 
6
6
  setuptools.setup(
7
7
  name="better-ffmpeg-progress",
8
- version="2.2.0",
8
+ version="2.2.2",
9
9
  author="GitHub.com/CrypticSignal",
10
10
  author_email="theaudiophile@outlook.com",
11
11
  description="Run FFmpeg & see percentage progress + ETA.",