Skip to content
Open
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 @@ -37,6 +37,24 @@
class YouTubeConverter(DocumentConverter):
"""Handle YouTube specially, focusing on the video title, description, and transcript."""

def _get_video_id(self, url: str) -> Union[str, None]:
"""Extract a YouTube video ID from supported URL formats."""
parsed_url = urlparse(url)
hostname = parsed_url.netloc.lower()
path_parts = [part for part in parsed_url.path.split("/") if part]

if hostname in {"youtu.be", "www.youtu.be"}:
return path_parts[0] if path_parts else None

if hostname in {"youtube.com", "www.youtube.com", "m.youtube.com"}:
if path_parts[:1] == ["watch"]:
params = parse_qs(parsed_url.query)
return params.get("v", [None])[0]
if path_parts[:1] in (["shorts"], ["embed"]):
return path_parts[1] if len(path_parts) > 1 else None

return None

def accepts(
self,
file_stream: BinaryIO,
Expand All @@ -53,7 +71,7 @@ def accepts(
url = unquote(url)
url = url.replace(r"\?", "?").replace(r"\=", "=")

if not url.startswith("https://www.youtube.com/watch?"):
if not self._get_video_id(url):
# Not a YouTube URL
return False

Expand Down Expand Up @@ -147,10 +165,8 @@ def convert(
if IS_YOUTUBE_TRANSCRIPT_CAPABLE:
ytt_api = YouTubeTranscriptApi()
transcript_text = ""
parsed_url = urlparse(stream_info.url) # type: ignore
params = parse_qs(parsed_url.query) # type: ignore
if "v" in params and params["v"][0]:
video_id = str(params["v"][0])
video_id = self._get_video_id(stream_info.url or "")
if video_id:
transcript_list = ytt_api.list(video_id)
languages = ["en"]
for transcript in transcript_list:
Expand Down
33 changes: 33 additions & 0 deletions packages/markitdown/tests/test_module_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
FileConversionException,
StreamInfo,
)
from markitdown.converters import YouTubeConverter

# This file contains module tests that are not directly tested by the FileTestVectors.
# This includes things like helper functions and runtime conversion options
Expand Down Expand Up @@ -179,6 +180,38 @@ def test_stream_info_operations() -> None:
assert updated_stream_info.url == "url.1"


@pytest.mark.parametrize(
("url", "video_id"),
[
("https://www.youtube.com/watch?v=V2qZ_lgxTzg", "V2qZ_lgxTzg"),
("https://youtu.be/V2qZ_lgxTzg", "V2qZ_lgxTzg"),
("https://www.youtube.com/shorts/V2qZ_lgxTzg", "V2qZ_lgxTzg"),
("https://www.youtube.com/embed/V2qZ_lgxTzg", "V2qZ_lgxTzg"),
],
)
def test_youtube_converter_extracts_supported_video_ids(
url: str, video_id: str
) -> None:
converter = YouTubeConverter()
assert converter._get_video_id(url) == video_id


@pytest.mark.parametrize(
"url",
[
"https://www.youtube.com/watch?v=V2qZ_lgxTzg",
"https://youtu.be/V2qZ_lgxTzg",
"https://www.youtube.com/shorts/V2qZ_lgxTzg",
],
)
def test_youtube_converter_accepts_supported_url_formats(url: str) -> None:
converter = YouTubeConverter()
assert converter.accepts(
io.BytesIO(b"<html></html>"),
StreamInfo(url=url, extension=".html"),
)


def test_data_uris() -> None:
# Test basic parsing of data URIs
data_uri = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="
Expand Down