Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ def convert(
# Start preparing the page
webpage_text = "# YouTube\n"

title = self._get(metadata, ["title", "og:title", "name"]) # type: ignore
assert isinstance(title, str)
title = self._get(metadata, ["title", "og:title", "name"]) or ""

if title:
webpage_text += f"\n## {title}\n"
Expand Down Expand Up @@ -188,9 +187,6 @@ def convert(
if transcript_text:
webpage_text += f"\n### Transcript\n{transcript_text}\n"

title = title if title else (soup.title.string if soup.title else "")
assert isinstance(title, str)

return DocumentConverterResult(
markdown=webpage_text,
title=title,
Expand Down
40 changes: 40 additions & 0 deletions packages/markitdown/tests/test_module_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,46 @@ def test_markitdown_llm() -> None:
validate_strings(result, PPTX_TEST_STRINGS)


def test_youtube_converter_missing_title_metadata() -> None:
"""Test that YouTubeConverter converts streams with and without title metadata without raising AssertionError."""
from unittest.mock import patch
from markitdown.converters._youtube_converter import YouTubeConverter

converter = YouTubeConverter()
stream_info = StreamInfo(
mimetype="text/html",
extension=".html",
url="https://www.youtube.com/watch?v=12345",
)

with patch(
"markitdown.converters._youtube_converter.IS_YOUTUBE_TRANSCRIPT_CAPABLE",
False,
):
# Case 1: Stream with no title metadata or title tag
html_content_no_title = b"<html><head></head><body>Video Content</body></html>"
stream_no_title = io.BytesIO(html_content_no_title)
result_no_title = converter.convert(stream_no_title, stream_info)
assert result_no_title.title == ""
assert "# YouTube" in result_no_title.markdown

# Case 2: Stream with an empty <title> tag
html_content_empty_title = (
b"<html><head><title></title></head><body>Video Content</body></html>"
)
stream_empty_title = io.BytesIO(html_content_empty_title)
result_empty_title = converter.convert(stream_empty_title, stream_info)
assert result_empty_title.title == ""
assert "# YouTube" in result_empty_title.markdown

# Case 3: Stream whose title is only available from the <title> tag
html_content_title_tag = b"<html><head><title>Fallback Title</title></head><body>Video Content</body></html>"
stream_title_tag = io.BytesIO(html_content_title_tag)
result_title_tag = converter.convert(stream_title_tag, stream_info)
assert result_title_tag.title == "Fallback Title"
assert "# YouTube" in result_title_tag.markdown


def test_zip_stream_no_filename_header() -> None:
"""Regression test: ZipConverter must not render the literal string 'None'
in the output header when the stream has no associated URL, local path, or
Expand Down
Loading