-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add corrupted video detection and re encode function to potential fix…
… broken video
- Loading branch information
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class VideoCorruptedError(RuntimeError): | ||
"""Error raised when a video file is corrupted.""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import subprocess | ||
from pathlib import Path | ||
|
||
def reencode_video(input_file:str|Path, output_file:str|Path): | ||
""" | ||
Re-encode a video file using ffmpeg. (this may fix some issues with corrupted video file) | ||
Parameters | ||
---------- | ||
input_file : str or Path file that will be re-encoded | ||
output_file: str or Path file where the re-encoded video will be saved | ||
Returns | ||
------- | ||
""" | ||
# Convert input and output files to Path objects | ||
input_path = Path(input_file).resolve() | ||
output_path = Path(output_file).resolve() | ||
|
||
# Check if the input file exists | ||
if not input_path.exists(): | ||
raise FileNotFoundError(f"Input file '{input_file}' not found.") | ||
|
||
if output_path.exists(): | ||
raise FileExistsError(f"Output file '{output_file}' already exists.") | ||
|
||
try: | ||
# Construct the ffmpeg command | ||
command = [ | ||
"ffmpeg", | ||
"-i", str(input_path), | ||
"-c", "copy", # Copy audio without re-encoding | ||
str(output_path) | ||
] | ||
|
||
# Run the command | ||
proc=subprocess.run(command, check=True) | ||
proc.check_returncode() | ||
except subprocess.CalledProcessError as e: | ||
print(f"Error: ffmpeg command failed with error {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters