forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
216 changed files
with
5,149 additions
and
1,772 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Description: | ||
# Ops that process audio and/or video files using FFmpeg. | ||
# (https://www.ffmpeg.org/) | ||
|
||
licenses(["notice"]) # Apache 2.0 | ||
|
||
exports_files(["LICENSE"]) | ||
|
||
package(default_visibility = ["//tensorflow:__subpackages__"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Description: | ||
# Libraries and kernels for manipulating audio and video using FFmpeg. | ||
# (https://www.ffmpeg.org) | ||
|
||
licenses(["notice"]) # Apache 2.0 | ||
|
||
exports_files(["LICENSE"]) | ||
|
||
package(default_visibility = ["//tensorflow:__subpackages__"]) | ||
|
||
cc_library( | ||
name = "ffmpeg_lib", | ||
srcs = ["ffmpeg_lib.cc"], | ||
hdrs = ["ffmpeg_lib.h"], | ||
deps = [ | ||
"//tensorflow/core:framework", | ||
"//tensorflow/core:lib", | ||
], | ||
) | ||
|
||
cc_test( | ||
name = "ffmpeg_lib_installed_test", | ||
srcs = ["ffmpeg_lib_test.cc"], | ||
args = [ | ||
"--should_ffmpeg_be_installed=true", | ||
], | ||
data = [ | ||
":testdata/test_sound1.mp3", | ||
], | ||
tags = [ | ||
"local", | ||
"manual", | ||
], | ||
deps = [ | ||
":ffmpeg_lib", | ||
"//tensorflow/core:framework_internal", | ||
"//tensorflow/core:lib", | ||
"//tensorflow/core:test", | ||
], | ||
) | ||
|
||
cc_test( | ||
name = "ffmpeg_lib_uninstalled_test", | ||
srcs = ["ffmpeg_lib_test.cc"], | ||
args = [ | ||
"--should_ffmpeg_be_installed=false", | ||
], | ||
data = [ | ||
":testdata/test_sound1.mp3", | ||
], | ||
tags = [ | ||
"local", | ||
"manual", | ||
], | ||
deps = [ | ||
":ffmpeg_lib", | ||
"//tensorflow/core:framework_internal", | ||
"//tensorflow/core:lib", | ||
"//tensorflow/core:test", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2016 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ============================================================================= | ||
|
||
#include "tensorflow/contrib/ffmpeg/kernels/ffmpeg_lib.h" | ||
|
||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
#include <unistd.h> | ||
#include <string> | ||
#include <tuple> | ||
#include <vector> | ||
|
||
#include "tensorflow/core/lib/io/path.h" | ||
#include "tensorflow/core/lib/strings/str_util.h" | ||
|
||
using tensorflow::strings::StrCat; | ||
|
||
namespace tensorflow { | ||
namespace ffmpeg { | ||
namespace { | ||
|
||
const char kFfmpegExecutable[] = "ffmpeg"; | ||
const int32 kDefaultProbeSize = 5000000; // 5MB | ||
|
||
string GetTempFilename(const string& extension) { | ||
for (const char* dir : std::vector<const char*>( | ||
{getenv("TEST_TMPDIR"), getenv("TMPDIR"), getenv("TMP"), "/tmp"})) { | ||
if (!dir || !dir[0]) { | ||
continue; | ||
} | ||
struct stat statbuf; | ||
if (!stat(dir, &statbuf) && S_ISDIR(statbuf.st_mode)) { | ||
return io::JoinPath(dir, StrCat("tmp_file_", getpid(), ".", extension)); | ||
} | ||
} | ||
LOG(FATAL) << "No temp directory found."; | ||
} | ||
|
||
std::vector<string> FfmpegCommandLine(const string& input_filename, | ||
const string& output_filename, | ||
const string& input_format_id, | ||
int32 samples_per_second, | ||
int32 channel_count) { | ||
return {"-nostats", // No additional progress display. | ||
"-nostdin", // No interactive commands accepted. | ||
"-f", input_format_id, // eg: "mp3" | ||
"-probesize", StrCat(kDefaultProbeSize), "-i", input_filename, | ||
"-loglevel", "info", // Enable verbose logging to support debugging. | ||
"-map_metadata", "-1", // Copy global metadata from input to output. | ||
"-vn", // No video recording. | ||
"-ac:a:0", StrCat(channel_count), "-ar:a:0", | ||
StrCat(samples_per_second), | ||
// Output set (in several ways) to signed 16-bit little-endian ints. | ||
"-codec:a:0", "pcm_s16le", "-sample_fmt", "s16", "-f", "s16le", | ||
"-sn", // No subtitle recording. | ||
"-y", // Overwrite output file. | ||
StrCat(output_filename)}; | ||
} | ||
|
||
[[noreturn]] int ExecuteFfmpeg(const std::vector<string>& args) { | ||
std::vector<char*> args_chars; | ||
std::transform(args.begin(), args.end(), std::back_inserter(args_chars), | ||
[](const string& s) { return const_cast<char*>(s.c_str()); }); | ||
args_chars.push_back(nullptr); | ||
|
||
::execvp(kFfmpegExecutable, args_chars.data()); | ||
// exec only returns on error. | ||
const int error = errno; | ||
LOG(ERROR) << "FFmpeg could not be executed: " << error; | ||
::_exit(error); | ||
} | ||
|
||
} // namespace | ||
|
||
Status ReadAudioFile(const string& filename, const string& audio_format_id, | ||
int32 samples_per_second, int32 channel_count, | ||
std::vector<float>* output_samples) { | ||
// Create an argument list. | ||
string output_filename = GetTempFilename(audio_format_id); | ||
const std::vector<string> args = | ||
FfmpegCommandLine(filename, output_filename, audio_format_id, | ||
samples_per_second, channel_count); | ||
|
||
// Execute ffmpeg and report errors. | ||
pid_t child_pid = ::fork(); | ||
if (child_pid < 0) { | ||
return Status(error::Code::UNKNOWN, StrCat("fork failed: ", errno)); | ||
} | ||
if (child_pid == 0) { | ||
ExecuteFfmpeg(args); | ||
} else { | ||
int status_code; | ||
::waitpid(child_pid, &status_code, 0); | ||
if (!status_code) { | ||
return Status::OK(); | ||
} else { | ||
return Status(error::Code::NOT_FOUND, | ||
StrCat("FFmpeg execution failed: ", status_code)); | ||
} | ||
} | ||
} | ||
|
||
} // namespace ffmpeg | ||
} // namespace tensorflow |
Oops, something went wrong.