Skip to content

Commit

Permalink
fix: video and audio file merging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Hetari committed Nov 19, 2024
1 parent 6d445d8 commit 74fd059
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ rm -rf dist
pip uninstall pyutube -y

# Build a wheel distribution package using the 'setup.py' file
python setup.py sdist bdist_wheel
python3 setup.py sdist bdist_wheel

# Install the wheel distribution package located in the 'dist' directory
# pip3 install dist/*
Expand Down
1 change: 0 additions & 1 deletion pyutube/services/DownloadService.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def download_video(self, video: YouTube, video_id: str, video_stream: YouTube, v
video_filename = self.file_service.generate_filename(video_stream, video_id)

# Prepend the title number and `__` to the filename if ordering is required

if self.make_playlist_in_order:
video_base_name, video_extension = os.path.splitext(video_filename)
video_filename = f"{title_number}__{video_base_name}{video_extension}"
Expand Down
5 changes: 1 addition & 4 deletions pyutube/services/FileService.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class FileService:

def save_file(self, video: YouTube, filename: str, path: str) -> None:
"""
Save the file to the specified path with the given filename.
Expand All @@ -32,12 +31,10 @@ def generate_filename(self, video, video_id, is_audio=False, filename: str = "")
"""
file_type = 'audio' if is_audio else video.resolution

extension = 'mp3' if is_audio else video.mime_type.split('/')[1]

title = filename if filename != "" else video.default_filename.split('.')[0]
title = safe_filename(title)

return f"{title} - {file_type}_-_{video_id}.{extension}"
return f"{title} - {file_type}_-_{video_id}"

def handle_existing_file(
self, video: YouTube, video_id: str, filename: str, path: str, is_audio: bool = False) -> None:
Expand Down
53 changes: 39 additions & 14 deletions pyutube/services/VideoService.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,42 +161,67 @@ def merging(self, video_name: str, audio_name: str):
Args:
video_name: The name of the video file.
audio_name: The name of the audio file.
video_id: The ID of the video.
Returns:
None
"""

output_directory = os.path.join(self.path, "output")
os.makedirs(output_directory, exist_ok=True)

output_file = os.path.join(
output_directory, os.path.basename(video_name))
# Output file path
output_file = os.path.join(output_directory, os.path.basename(
video_name).replace(os.path.splitext(video_name)[1], '.mp4'))

# Extract base names to match files
video_base_name = os.path.splitext(os.path.basename(video_name))[0]
audio_base_name = os.path.splitext(os.path.basename(audio_name))[0]

video_name = os.path.join(self.path, video_name)
audio_name = os.path.join(self.path, audio_name)
# Locate the video file
video_path = None
for file in os.listdir(self.path):
file_base, _ = os.path.splitext(file)
if file_base == video_base_name:
video_path = os.path.join(self.path, file)
break

if video_path is None:
error_console.print(f"❗ Video file not found: {video_name}")
sys.exit()

# Locate the audio file
audio_path = None
for file in os.listdir(self.path):
file_base, _ = os.path.splitext(file)
if file_base == audio_base_name:
audio_path = os.path.join(self.path, file)
break

if audio_path is None:
error_console.print(f"❗ Audio file not found: {audio_name}")
sys.exit()

# Merge video and audio
ffmpeg_merge_video_audio(
video_name,
audio_name,
video_path,
audio_path,
output_file,
vcodec='copy',
acodec='copy',
ffmpeg_output=False,
logger=None
logger=None,
)

# Remove original files
os.remove(video_name)
os.remove(audio_name)
os.remove(video_path)
os.remove(audio_path)

# Move the merged file to the current directory
if os.path.exists(output_file):
os.replace(output_file, os.path.join(os.getcwd(), video_name))
merged_file_name = os.path.basename(video_name)
os.replace(output_file, os.path.join(os.getcwd(), merged_file_name))
os.rmdir(output_directory)
else:
error_console.print(
"Merged video file not found in the output directory.")
error_console.print("❗ Merged video file not found in the output directory.")

@staticmethod
def get_video_resolutions_sizes(
Expand Down
2 changes: 1 addition & 1 deletion pyutube/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pytubefix import __version__ as pytubefix_version


__version__ = "1.3.39"
__version__ = "1.4.0"
__app__ = "pyutube"
ABORTED_PREFIX = "Aborted"
CANCEL_PREFIX = "Cancel"
Expand Down

0 comments on commit 74fd059

Please sign in to comment.