Debian Codegraph @

util/ffmpeg/convert.go

raw symbols SCIP
1 // Copyright (c) 2022 Sumner Evans
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package ffmpegscip-go gomod maunium.net/go/mautrix . `maunium.net/go/mautrix/util/ffmpeg`/
8
9 import (
10 "fmtscip-go gomod github.com/golang/go/src go1.17 fmt/"
11 "io/ioutilscip-go gomod github.com/golang/go/src go1.17 `io/ioutil`/"
12 "osscip-go gomod github.com/golang/go/src go1.17 os/"
13 "os/execscip-go gomod github.com/golang/go/src go1.17 `os/exec`/"
14 "path/filepathscip-go gomod github.com/golang/go/src go1.17 `path/filepath`/"
15 "stringsscip-go gomod github.com/golang/go/src go1.17 strings/"
16
17 logscip-go gomod maunium.net/go/maulogger/v2 v2.3.2 `maunium.net/go/maulogger/v2`/ "maunium.net/go/maulogger/v2scip-go gomod maunium.net/go/maulogger/v2 v2.3.2 `maunium.net/go/maulogger/v2`/"
18 "maunium.net/go/mautrix/utilscip-go gomod maunium.net/go/mautrix . `maunium.net/go/mautrix/util`/"
19 )
20
21 var ffmpegDefaultParamsscip-go gomod maunium.net/go/mautrix . `maunium.net/go/mautrix/util/ffmpeg`/ffmpegDefaultParams. = []string{"-hide_banner", "-loglevel", "warning"}
22
23 // Convert a media file on the disk using ffmpeg.
24 //
25 // Args:
26 // * inputFile: The full path to the file.
27 // * outputExtension: The extension that the output file should be.
28 // * inputArgs: Arguments to tell ffmpeg how to parse the input file.
29 // * outputArgs: Arguments to tell ffmpeg how to convert the file to reach the wanted output.
30 // * removeInput: Whether the input file should be removed after converting.
31 //
32 // Returns: the path to the converted file.
33 func ConvertPathscip-go gomod maunium.net/go/mautrix . `maunium.net/go/mautrix/util/ffmpeg`/ConvertPath().(inputFile string, outputExtension string, inputArgs []string, outputArgs []string, removeInput bool) (string, error) {
34 outputFilename := stringsscip-go gomod github.com/golang/go/src go1.17 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.17 strings/TrimSuffix().(inputFile, filepathscip-go gomod github.com/golang/go/src go1.17 `path/filepath`/.Extscip-go gomod github.com/golang/go/src go1.17 `path/filepath`/Ext().(inputFile)) + outputExtension
35
36 args := []string{}
37 args = append(args, ffmpegDefaultParamsscip-go gomod maunium.net/go/mautrix . `maunium.net/go/mautrix/util/ffmpeg`/ffmpegDefaultParams....)
38 args = append(args, inputArgs...)
39 args = append(args, "-i", inputFile)
40 args = append(args, outputArgs...)
41 args = append(args, outputFilename)
42
43 cmd := execscip-go gomod github.com/golang/go/src go1.17 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.17 `os/exec`/Command().("ffmpeg", args...)
44 vcLog := logscip-go gomod maunium.net/go/maulogger/v2 v2.3.2 `maunium.net/go/maulogger/v2`/.Subscip-go gomod maunium.net/go/maulogger/v2 v2.3.2 `maunium.net/go/maulogger/v2`/Sub().("ffmpeg").Writerscip-go gomod maunium.net/go/maulogger/v2 v2.3.2 `maunium.net/go/maulogger/v2`/Logger#Writer().(logscip-go gomod maunium.net/go/maulogger/v2 v2.3.2 `maunium.net/go/maulogger/v2`/.LevelWarnscip-go gomod maunium.net/go/maulogger/v2 v2.3.2 `maunium.net/go/maulogger/v2`/LevelWarn.)
45 cmd.Stdoutscip-go gomod github.com/golang/go/src go1.17 `os/exec`/Cmd#Stdout. = vcLog
46 cmd.Stderrscip-go gomod github.com/golang/go/src go1.17 `os/exec`/Cmd#Stderr. = vcLog
47 err := cmd.Runscip-go gomod github.com/golang/go/src go1.17 `os/exec`/Cmd#Run().()
48 if err != nil {
49 return "", fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.17 fmt/Errorf().("ffmpeg error: %+v", err)
50 }
51
52 if removeInput {
53 osscip-go gomod github.com/golang/go/src go1.17 os/.Removescip-go gomod github.com/golang/go/src go1.17 os/Remove().(inputFile)
54 }
55
56 return outputFilename, nil
57 }
58
59 // Convert media data using ffmpeg.
60 //
61 // Args:
62 // * data: The media data to convert
63 // * outputExtension: The extension that the output file should be.
64 // * inputArgs: Arguments to tell ffmpeg how to parse the input file.
65 // * outputArgs: Arguments to tell ffmpeg how to convert the file to reach the wanted output.
66 // * inputMime: The mimetype of the input data.
67 //
68 // Returns: the converted data
69 func ConvertBytesscip-go gomod maunium.net/go/mautrix . `maunium.net/go/mautrix/util/ffmpeg`/ConvertBytes().(data []byte, outputExtension string, inputArgs []string, outputArgs []string, inputMime string) ([]byte, error) {
70 tempdir, err := ioutilscip-go gomod github.com/golang/go/src go1.17 `io/ioutil`/.TempDirscip-go gomod github.com/golang/go/src go1.17 `io/ioutil`/TempDir().("", "mautrix_ffmpeg_*")
71 if err != nil {
72 return nil, err
73 }
74 defer osscip-go gomod github.com/golang/go/src go1.17 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.17 os/RemoveAll().(tempdir)
75 inputFileName := fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().("%s/input%s", tempdir, utilscip-go gomod maunium.net/go/mautrix . `maunium.net/go/mautrix/util`/.ExtensionFromMimetypescip-go gomod maunium.net/go/mautrix . `maunium.net/go/mautrix/util`/ExtensionFromMimetype().(inputMime))
76
77 inputFile, err := osscip-go gomod github.com/golang/go/src go1.17 os/.OpenFilescip-go gomod github.com/golang/go/src go1.17 os/OpenFile().(inputFileName, osscip-go gomod github.com/golang/go/src go1.17 os/.O_CREATEscip-go gomod github.com/golang/go/src go1.17 os/O_CREATE.|osscip-go gomod github.com/golang/go/src go1.17 os/.O_EXCLscip-go gomod github.com/golang/go/src go1.17 os/O_EXCL.|osscip-go gomod github.com/golang/go/src go1.17 os/.O_WRONLYscip-go gomod github.com/golang/go/src go1.17 os/O_WRONLY., 0600)
78 if err != nil {
79 return nil, fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.17 fmt/Errorf().("failed to open input file: %w", err)
80 }
81 _, err = inputFile.Writescip-go gomod github.com/golang/go/src go1.17 os/File#Write().(data)
82 if err != nil {
83 inputFile.Closescip-go gomod github.com/golang/go/src go1.17 os/File#Close().()
84 return nil, fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.17 fmt/Errorf().("failed to write data to input file: %w", err)
85 }
86 inputFile.Closescip-go gomod github.com/golang/go/src go1.17 os/File#Close().()
87
88 outputPath, err := ConvertPathscip-go gomod maunium.net/go/mautrix . `maunium.net/go/mautrix/util/ffmpeg`/ConvertPath().(inputFileName, outputExtension, inputArgs, outputArgs, false)
89 if err != nil {
90 return nil, err
91 }
92 return ioutilscip-go gomod github.com/golang/go/src go1.17 `io/ioutil`/.ReadFilescip-go gomod github.com/golang/go/src go1.17 `io/ioutil`/ReadFile().(outputPath)
93 }