From f2895319549a207170ff006dd9d4de15c90044ec Mon Sep 17 00:00:00 2001 From: Ryan Schumacher Date: Thu, 12 Sep 2024 02:16:18 -0500 Subject: [PATCH] feat: first commit --- .github/workflows/check.yaml | 40 ++++++ .github/workflows/release.yml | 38 +++++ .gitignore | 4 + .goreleaser.yaml | 37 +++++ LICENSE | 7 + README.md | 44 ++++++ go.mod | 3 + main.go | 257 ++++++++++++++++++++++++++++++++++ 8 files changed, 430 insertions(+) create mode 100644 .github/workflows/check.yaml create mode 100644 .github/workflows/release.yml create mode 100644 .gitignore create mode 100644 .goreleaser.yaml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 main.go diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml new file mode 100644 index 0000000..797cb09 --- /dev/null +++ b/.github/workflows/check.yaml @@ -0,0 +1,40 @@ +name: Check + +permissions: + contents: read + +on: + pull_request: + branches: + - main + paths-ignore: + - example/** + - "**/*.md" + - "**/*.yaml" + push: + branches: + - main + +jobs: + job: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: 1.22 + - name: 🧹 Lint + uses: golangci/golangci-lint-action@v4 + - name: Install dependencies + run: go install gotest.tools/gotestsum@latest + - name: 🧪 Test + run: gotestsum --junitfile junit.xml --format testdox -- -race -coverprofile=coverage.out -covermode=atomic + - name: Upload coverage reports to Codecov + uses: codecov/codecov-action@v4 + with: + token: ${{ secrets.CODECOV_TOKEN }} + - name: Upload test results to Codecov + if: ${{ !cancelled() }} + uses: codecov/test-results-action@v1 + with: + token: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..2887ba4 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,38 @@ +on: + push: + branches: + - main + +permissions: + contents: write + pull-requests: write + +name: release-please + +jobs: + release-please: + runs-on: ubuntu-latest + steps: + - uses: googleapis/release-please-action@v4 + id: release + with: + token: ${{ secrets.RELEASE_PLEASE_PAT }} + release-type: simple + - uses: actions/checkout@v4 + if: ${{ steps.release.outputs.release_created }} + with: + fetch-depth: 0 + - uses: actions/setup-go@v5 + if: ${{ steps.release.outputs.release_created }} + with: + go-version: '1.20' + - uses: goreleaser/goreleaser-action@v6 + if: ${{ steps.release.outputs.release_created }} + with: + # either 'goreleaser' (default) or 'goreleaser-pro' + distribution: goreleaser + # 'latest', 'nightly', or a semver + version: '~> v2' + args: release --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..33a02a8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + +dist/ +*.mp4 +*.m3u8 \ No newline at end of file diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..f6de8ee --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,37 @@ +# This is an example .goreleaser.yml file with some sensible defaults. +# Make sure to check the documentation at https://goreleaser.com + +# The lines below are called `modelines`. See `:help modeline` +# Feel free to remove those if you don't want/need to use them. +# yaml-language-server: $schema=https://goreleaser.com/static/schema.json +# vim: set ts=2 sw=2 tw=0 fo=cnqoj + +version: 2 + +builds: + - ldflags: > + -s -w + - -X main.AppName={{.ProjectName}} + - -X main.AppVersion={{.Version}} + env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + +archives: + - format: tar.gz + # this name template makes the OS and Arch compatible with the results of `uname`. + name_template: >- + {{ .ProjectName }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + {{- if .Arm }}v{{ .Arm }}{{ end }} + # use zip for windows archives + format_overrides: + - goos: windows + format: zip + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7f12ac8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2024 Ryan Schumacher + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..dcb348c --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# m3u8-downloader - download m3u8 files and convert to mp4 + +This project downloads a m3u8 file and converts it to mp4. It uses ffmpeg to convert the file. + +## Installation + +```bash +go install github.com/jrschumacher/m3u8-downloader@latest +``` + +## Usage + +```bash +NAME + m3u8-downloader - is a tool to download videos from m3u8 manifest files. + +SYNOPSIS + m3u8-downloader [OPTIONS] + +OPTIONS + -download + Set to true to download the video + -ffmpeg string + Path to ffmpeg executable + -filename string + Filename of the downloaded video + -help + Show usage + -version + Show version + +EXAMPLES + To extact the manifest file from a URL: + m3u8-downloader https://example.com/video.m3u8 + + To download a video from a m3u8 manifest file: + m3u8-downloader -download https://example.com/video.m3u8 + + To download a video from a m3u8 manifest file with a custom filename: + m3u8-downloader -download -filename my_video https://example.com/video.m3u8 + + To download a video from a m3u8 manifest file with a custom ffmpeg path: + m3u8-downloader -download -ffmpeg /usr/local/bin/ffmpeg https://example.com/video.m3u8 +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1d7dcd3 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module lit-download + +go 1.23.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..1f5b8c3 --- /dev/null +++ b/main.go @@ -0,0 +1,257 @@ +package main + +import ( + "bufio" + "flag" + "fmt" + "io" + "log" + "net/http" + "os" + "os/exec" + "path" + "regexp" + "strconv" + "strings" + "text/template" + "time" +) + +const AppName = "m3u8-downloader" +const AppVersion = "1.0.0" +const defaultFfmpegPath = "ffmpeg" + +const usage = ` +NAME + {{ .AppName }} - is a tool to download videos from m3u8 manifest files. + +SYNOPSIS + {{ .AppName }} [OPTIONS] + +OPTIONS +` +const usageExample = ` +EXAMPLES + To extact the manifest file from a URL: + {{ .AppName }} https://example.com/video.m3u8 + + To download a video from a m3u8 manifest file: + {{ .AppName }} -download https://example.com/video.m3u8 + + To download a video from a m3u8 manifest file with a custom filename: + {{ .AppName }} -download -filename my_video https://example.com/video.m3u8 + + To download a video from a m3u8 manifest file with a custom ffmpeg path: + {{ .AppName }} -download -ffmpeg /usr/local/bin/ffmpeg https://example.com/video.m3u8 +` + +func main() { + flag.Usage = usageFunc + + help := flag.Bool("help", false, "Show usage") + version := flag.Bool("version", false, "Show version") + videoTitle := flag.String("filename", "", "Filename of the downloaded video") + shouldDownload := flag.Bool("download", false, "Set to true to download the video") + ffmpegPath := flag.String("ffmpeg", "", "Path to ffmpeg executable") + flag.Parse() + + if *help { + flag.Usage() + return + } + + if *version { + fmt.Printf("%s %s\n", AppName, AppVersion) + return + } + + if *ffmpegPath == "" { + *ffmpegPath = defaultFfmpegPath + } + + // check if ffmpeg is installed + if *shouldDownload { + _, err := exec.LookPath(*ffmpegPath) + if err != nil { + log.Printf("Error: %s is not installed.\n", *ffmpegPath) + return + } + } + + manifestURL := flag.Arg(0) + + if manifestURL == "" { + flag.Usage() + return + } + + // convert video title to a valid file name by replacing invalid characters with underscores using a regular expression + var videoTitleFilename string + + if *videoTitle == "" { + // extract the filename from the URL + fn := path.Base(manifestURL) + // remove file extension + fn = strings.TrimSuffix(fn, path.Ext(fn)) + videoTitle = &fn + } + re := regexp.MustCompile(`[^\w\d]+`) + videoTitleFilename = re.ReplaceAllString(*videoTitle, "_") + + // Open the manifest file + var file io.ReadCloser + { + resp, err := http.Get(manifestURL) + if err != nil { + log.Println("Error opening manifest URL:", err) + return + } + defer resp.Body.Close() + file = resp.Body + } + + // Create a temporary playlist to write the modified content + playlistFile, err := os.CreateTemp("", videoTitleFilename+".playlist.*.m3u8") + log.Printf("Created temporary playlist %s\n", playlistFile.Name()) + if err != nil { + log.Println("Error creating temporary playlist:", err) + return + } + defer playlistFile.Close() + + // Regular expression to match the resolution + reResolution := regexp.MustCompile(`RESOLUTION=(\d+)x(\d+)`) + // Variables to store the highest resolution and corresponding URL + var maxResolution int + var maxResolutionURL string + + log.Println("Reading manifest file...") + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "#EXT-X-STREAM-INF") { + // Extract resolution + matches := reResolution.FindStringSubmatch(line) + if len(matches) == 3 { + width, _ := strconv.Atoi(matches[1]) + height, _ := strconv.Atoi(matches[2]) + resolution := width * height + // Check if this is the highest resolution + if resolution > maxResolution { + maxResolution = resolution + // Read the next line for the URL + if scanner.Scan() { + maxResolutionURL = scanner.Text() + } + } + } + } + } + + if maxResolutionURL == "" { + log.Println("No valid resolution found in the manifest.") + return + } + + // Extract base path from the URL + basePath := path.Dir(maxResolutionURL) + if !strings.HasPrefix(basePath, "https://") { + if strings.HasPrefix(basePath, "https:/") { + basePath = "https://" + basePath[7:] + } else if strings.HasPrefix(basePath, "https:") { + basePath = "https://" + basePath[6:] + } else if strings.HasPrefix(basePath, "http:/") { + basePath = "http://" + basePath[6:] + } else if strings.HasPrefix(basePath, "http:") { + basePath = "http://" + basePath[5:] + } else { + basePath = "https://" + basePath + } + } + + // Download the video manifest + log.Println("Downloading video manifest...") + resp, err := http.Get(maxResolutionURL) + if err != nil { + log.Println("Error downloading video manifest:", err) + return + } + defer resp.Body.Close() + + manifestBody := resp.Body + + log.Println("Writing modified playlist...") + scanner = bufio.NewScanner(manifestBody) + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "#EXTINF") { + // Write the #EXTINF line + playlistFile.WriteString(line + "\n") + // Read the next line for the URL and prepend the base path + if scanner.Scan() { + urlLine := scanner.Text() + if !strings.HasPrefix(urlLine, "http") { + urlLine = basePath + "/" + urlLine + } + playlistFile.WriteString(urlLine + "\n") + } + } else { + playlistFile.WriteString(line + "\n") + } + } + + if err := scanner.Err(); err != nil { + log.Println("Error reading manifest file:", err) + } + + // Execute ffmpeg command + if *shouldDownload { + log.Println("Starting video download...") + downloadVideo(*ffmpegPath, playlistFile.Name(), *videoTitle) + } else { + log.Printf("Video download skipped. To download see usage: %s -help\n", AppName) + } +} + +func usageFunc() { + usageTmpl, err := template.New("usage").Parse(usage) + if err != nil { + log.Println("Error parsing usage template:", err) + return + } + + usageExampleTmpl, err := template.New("usageExample").Parse(usageExample) + if err != nil { + log.Println("Error parsing usage example template:", err) + return + } + + err = usageTmpl.Execute(os.Stdout, map[string]string{"AppName": AppName}) + if err != nil { + log.Println("Error executing usage template:", err) + return + } + flag.PrintDefaults() + err = usageExampleTmpl.Execute(os.Stdout, map[string]string{"AppName": AppName}) + if err != nil { + log.Println("Error executing usage example template:", err) + return + } +} + +func downloadVideo(ffmpegPath string, playlistFilename string, videoTitle string) { + log.Println("Converting video...") + cmd := exec.Command(ffmpegPath, "-protocol_whitelist", "https,file,tls,tcp", "-i", playlistFilename, "-c", "copy", videoTitle+".mp4") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + start := time.Now() // Start the timer + + if err := cmd.Run(); err != nil { + log.Println("Error executing ffmpeg command:", err) + return + } + + elapsed := time.Since(start) // Calculate the elapsed time + log.Printf("Video conversion completed in %s.\n", elapsed) +}