Skip to content

Commit 89b2be5

Browse files
committed
init project
0 parents  commit 89b2be5

File tree

5 files changed

+503
-0
lines changed

5 files changed

+503
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

Package.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// swift-tools-version: 6.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "swift-youtube-transcript",
8+
platforms: [
9+
.macOS(.v12),
10+
.iOS(.v15),
11+
.tvOS(.v15),
12+
.macCatalyst(.v15),
13+
.watchOS(.v8),
14+
],
15+
products: [
16+
// Products define the executables and libraries a package produces, making them visible to other packages.
17+
.library(
18+
name: "YoutubeTranscript",
19+
targets: ["YoutubeTranscript"])
20+
],
21+
targets: [
22+
// Targets are the basic building blocks of a package, defining a module or a test suite.
23+
// Targets can depend on other targets in this package and products from dependencies.
24+
.target(
25+
name: "YoutubeTranscript",
26+
dependencies: []),
27+
.testTarget(
28+
name: "YoutubeTranscriptTests",
29+
dependencies: ["YoutubeTranscript"],
30+
path: "Tests"
31+
),
32+
]
33+
)

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)