diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2cf0a69 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +./video \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e6dee2b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:latest + +WORKDIR /usr/src/app + +COPY go.mod go.sum ./ +RUN go mod download && go mod verify + +COPY . . +RUN go build + +CMD [ "sh", "-c", "./chaturbate-dvr -u $USERNAME" ] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..721b562 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: "3.0" + +services: + chaturbate-dvr: + build: . + environment: + - USERNAME=my_lovely_channel_name + volumes: + - ./video/my_lovely_channel_name:/usr/src/app/video \ No newline at end of file diff --git a/main.go b/main.go index f2cffdb..2be92ff 100644 --- a/main.go +++ b/main.go @@ -26,15 +26,15 @@ const chaturbateURL = "https://chaturbate.com/" // retriesAfterOnlined tells the retries for stream when disconnected but not really offlined. var retriesAfterOnlined = 0 -// lastCheckOnline logs the last check time. -var lastCheckOnline = time.Now() - // bucket stores the used segment to prevent fetched the duplicates. var bucket []string // segmentIndex is current stored segment index. var segmentIndex int +// path save video +const savePath = "video" + // var ( errInternal = errors.New("err") @@ -131,9 +131,9 @@ func capture(username string) { // Get the best resolution m3u8 by parsing the HLS source table. m3u8Source := parseHLSSource(hlsSource, baseURL) // Create the master video file. - masterFile, _ := os.OpenFile(filename+".ts", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0777) + masterFile, _ := os.OpenFile("./"+savePath+"/"+filename+".ts", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0777) // - log.Printf("the video will be saved as \"%s\".", filename+".ts") + log.Printf("the video will be saved as \"./"+savePath+"/%s\".", filename+".ts") go combineSegment(masterFile, filename) watchStream(m3u8Source, username, masterFile, filename, baseURL) @@ -199,7 +199,7 @@ func combineSegment(master *os.File, filename string) { continue } - if !pathx.Exists(fmt.Sprintf("%s~%d.ts", filename, index)) { + if !pathx.Exists(fmt.Sprintf("./%s/%s~%d.ts", savePath, filename, index)) { if retry >= 5 { index++ retry = 0 @@ -216,11 +216,11 @@ func combineSegment(master *os.File, filename string) { retry = 0 } // - b, _ := ioutil.ReadFile(fmt.Sprintf("%s~%d.ts", filename, index)) + b, _ := ioutil.ReadFile(fmt.Sprintf("./%s/%s~%d.ts", savePath, filename, index)) master.Write(b) log.Printf("inserting %d segment to the master file. (total: %d)", index, segmentIndex) // - os.Remove(fmt.Sprintf("%s~%d.ts", filename, index)) + os.Remove(fmt.Sprintf("./%s/%s~%d.ts", savePath, filename, index)) // index++ } @@ -235,7 +235,7 @@ func fetchSegment(master *os.File, segment *m3u8.MediaSegment, baseURL string, f return } // - f, err := os.OpenFile(fmt.Sprintf("%s~%d.ts", filename, index), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0777) + f, err := os.OpenFile(fmt.Sprintf("./%s/%s~%d.ts", savePath, filename, index), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0777) if err != nil { panic(err) } @@ -264,6 +264,11 @@ func endpoint(c *cli.Context) error { fmt.Println("Y8888D' YP 88 YD") fmt.Println("---") + // Mkdir video folder + if _, err := os.Stat("./" + savePath); os.IsNotExist(err) { + os.Mkdir("./"+savePath, 0777) + } + for { // Capture the stream if the user is currently online. if getOnlineStatus(c.String("username")) {