|
| 1 | +# YoutubeTranscript |
| 2 | + |
| 3 | +A Swift library for fetching YouTube video transcripts (captions) programmatically. Supports both standard YouTube videos and Shorts, with robust error handling and async/await support. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Fetches transcripts for YouTube videos and Shorts |
| 8 | +- Supports language selection |
| 9 | +- Handles all major YouTube transcript error cases |
| 10 | +- Async/await API for modern Swift concurrency |
| 11 | +- Comprehensive unit tests using Swift Testing |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +Add this package to your `Package.swift` dependencies: |
| 16 | + |
| 17 | +```swift |
| 18 | +.package(url: "https://github.com/yourusername/swift-youtube-transcript.git", from: "1.0.0") |
| 19 | +``` |
| 20 | + |
| 21 | +And add `YoutubeTranscript` as a dependency for your target: |
| 22 | + |
| 23 | +```swift |
| 24 | +.target( |
| 25 | + name: "YourTarget", |
| 26 | + dependencies: [ |
| 27 | + .product(name: "YoutubeTranscript", package: "swift-youtube-transcript") |
| 28 | + ] |
| 29 | +) |
| 30 | +``` |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +```swift |
| 35 | +import YoutubeTranscript |
| 36 | + |
| 37 | +Task { |
| 38 | + do { |
| 39 | + let transcript = try await YoutubeTranscript.fetchTranscript(for: "YOUR_VIDEO_ID") |
| 40 | + for entry in transcript { |
| 41 | + print("\(entry.offset)s: \(entry.text)") |
| 42 | + } |
| 43 | + } catch { |
| 44 | + print("Failed to fetch transcript: \(error)") |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### Fetching in a Specific Language |
| 50 | + |
| 51 | +```swift |
| 52 | +let config = TranscriptConfig(lang: "en") |
| 53 | +let transcript = try await YoutubeTranscript.fetchTranscript(for: "YOUR_VIDEO_ID", config: config) |
| 54 | +``` |
| 55 | + |
| 56 | +## Error Handling |
| 57 | + |
| 58 | +The library throws `YoutubeTranscriptError` for all error cases: |
| 59 | + |
| 60 | +- `.tooManyRequests`: YouTube is rate-limiting your IP |
| 61 | +- `.videoUnavailable`: The video is not available |
| 62 | +- `.disabled`: Transcripts are disabled for this video |
| 63 | +- `.notAvailable`: No transcripts are available |
| 64 | +- `.notAvailableLanguage`: No transcript in the requested language |
| 65 | +- `.emptyTranscript`: The transcript is empty |
| 66 | +- `.invalidVideoId`: The video ID is invalid |
| 67 | +- `.networkError`: Network error occurred |
| 68 | +- `.parsingError`: Failed to parse YouTube response |
| 69 | + |
| 70 | +## Testing |
| 71 | + |
| 72 | +This project uses [Swift Testing](https://github.com/apple/swift-testing) for its test suite. |
| 73 | + |
| 74 | +To run the tests: |
| 75 | + |
| 76 | +```sh |
| 77 | +swift test |
| 78 | +``` |
| 79 | + |
| 80 | +The tests cover all major success and error cases for `fetchTranscript`. You can provide your own video IDs in `Tests/YoutubeTranscriptTests.swift` to verify real-world scenarios. |
| 81 | + |
| 82 | +## License |
| 83 | + |
| 84 | +MIT |
0 commit comments