| 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 ffmpeg |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"io/ioutil" |
| 12 |
"os" |
| 13 |
"os/exec" |
| 14 |
"path/filepath" |
| 15 |
"strings" |
| 16 |
|
| 17 |
log "maunium.net/go/maulogger/v2" |
| 18 |
"maunium.net/go/mautrix/util" |
| 19 |
) |
| 20 |
|
| 21 |
var 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 ConvertPath(inputFile string, outputExtension string, inputArgs []string, outputArgs []string, removeInput bool) (string, error) { |
| 34 |
outputFilename := strings.TrimSuffix(inputFile, filepath.Ext(inputFile)) + outputExtension |
| 35 |
|
| 36 |
args := []string{} |
| 37 |
args = append(args, 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 := exec.Command("ffmpeg", args...) |
| 44 |
vcLog := log.Sub("ffmpeg").Writer(log.LevelWarn) |
| 45 |
cmd.Stdout = vcLog |
| 46 |
cmd.Stderr = vcLog |
| 47 |
err := cmd.Run() |
| 48 |
if err != nil { |
| 49 |
return "", fmt.Errorf("ffmpeg error: %+v", err) |
| 50 |
} |
| 51 |
|
| 52 |
if removeInput { |
| 53 |
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 ConvertBytes(data []byte, outputExtension string, inputArgs []string, outputArgs []string, inputMime string) ([]byte, error) { |
| 70 |
tempdir, err := ioutil.TempDir("", "mautrix_ffmpeg_*") |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
defer os.RemoveAll(tempdir) |
| 75 |
inputFileName := fmt.Sprintf("%s/input%s", tempdir, util.ExtensionFromMimetype(inputMime)) |
| 76 |
|
| 77 |
inputFile, err := os.OpenFile(inputFileName, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600) |
| 78 |
if err != nil { |
| 79 |
return nil, fmt.Errorf("failed to open input file: %w", err) |
| 80 |
} |
| 81 |
_, err = inputFile.Write(data) |
| 82 |
if err != nil { |
| 83 |
inputFile.Close() |
| 84 |
return nil, fmt.Errorf("failed to write data to input file: %w", err) |
| 85 |
} |
| 86 |
inputFile.Close() |
| 87 |
|
| 88 |
outputPath, err := ConvertPath(inputFileName, outputExtension, inputArgs, outputArgs, false) |
| 89 |
if err != nil { |
| 90 |
return nil, err |
| 91 |
} |
| 92 |
return ioutil.ReadFile(outputPath) |
| 93 |
} |