Skip to content

Commit

Permalink
Merge pull request #2 from shiguredo/feature/delete_intermediate_file
Browse files Browse the repository at this point in the history
FaststartWriter で中間ファイルを明示的をに削除する
  • Loading branch information
haruyama authored Jan 12, 2021
2 parents 2112570 + a4b6d5e commit 175b489
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 2 additions & 0 deletions include/shiguredo/mp4/writer/faststart_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class FaststartWriter : public Writer {
void appendTrakAndUdtaBoxInfo(const std::vector<shiguredo::mp4::track::Track*>&) override;
void writeMdatHeader();
void copyMdatData();
void deleteIntermediateFile();
std::filesystem::path getIntermediateFilePath();

private:
std::ostream& m_os;
Expand Down
27 changes: 19 additions & 8 deletions src/writer/faststart_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <limits>
#include <stdexcept>
Expand Down Expand Up @@ -141,29 +143,26 @@ std::uint64_t FaststartWriter::getMdatHeaderSize() const {
}

void FaststartWriter::copyMdatData() {
if (auto ret = ::fseek(m_mdat_fd, 0, SEEK_SET); ret == -1) {
if (auto ret = std::fseek(m_mdat_fd, 0, SEEK_SET); ret == -1) {
throw std::runtime_error("fseek failed");
}

char buffer[COPY_MDAT_DATA_BUFFER_SIZE];
while (!::feof(m_mdat_fd)) {
std::size_t size = ::fread(buffer, 1, COPY_MDAT_DATA_BUFFER_SIZE, m_mdat_fd);
while (!std::feof(m_mdat_fd)) {
std::size_t size = std::fread(buffer, 1, COPY_MDAT_DATA_BUFFER_SIZE, m_mdat_fd);
std::copy_n(buffer, size, std::ostreambuf_iterator<char>(m_os));
if (!m_os.good()) {
throw std::runtime_error(
fmt::format("FaststartWriter::copyMdatData(): copying mdat data failed: rdstate={}", m_os.rdstate()));
}
}

if (auto ret = ::fclose(m_mdat_fd); ret == EOF) {
if (auto ret = std::fclose(m_mdat_fd); ret == EOF) {
throw std::runtime_error(
fmt::format("FaststartWriter::copyMdatData(): cannot close the intermediate file: {}", m_mdat_path.string()));
}

if (bool result = std::filesystem::remove(m_mdat_path); !result) {
throw std::runtime_error(
fmt::format("FaststartWriter::copyMdatData(): cannot remove the intermediate file: {}", m_mdat_path.string()));
}
deleteIntermediateFile();
}

void FaststartWriter::appendTrakAndUdtaBoxInfo(const std::vector<shiguredo::mp4::track::Track*>& tracks) {
Expand Down Expand Up @@ -193,4 +192,16 @@ void FaststartWriter::appendTrakAndUdtaBoxInfo(const std::vector<shiguredo::mp4:
} while (prev_size != size);
}

void FaststartWriter::deleteIntermediateFile() {
if (bool result = std::filesystem::remove(m_mdat_path); !result) {
throw std::runtime_error(fmt::format(
"FaststartWriter::deleteIntermediateFile(): cannot remove the intermediate file: {}", m_mdat_path.string()));
}
}

std::filesystem::path FaststartWriter::getIntermediateFilePath() {
return m_mdat_path;
}

} // namespace shiguredo::mp4::writer

4 changes: 2 additions & 2 deletions test/integration/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ get_input_files:
[ -f input/sample-5s.mp4 ] || curl -o input/sample-5s.mp4 https://samplelib.com/lib/download/mp4/sample-5s.mp4 # "Below are sample videos available for download with no license restrictions." -- https://samplelib.com/sample-mp4.html
sha224sum -c input/check

test: get_input_files mux_test dump_test
test: mux_test dump_test
sha224sum -c output/check

mux_test:
Expand All @@ -18,7 +18,7 @@ mux_test:
faketime -f '2020-12-01 00:00:00' ../../release/mp4-muxer aacvp9 -f output/aacvp9_test.mp4 --aac resources/aac.csv --vp9 resources/vp9.csv
faketime -f '2020-12-01 00:00:00' ../../release/mp4-muxer aacvp9_faststart -f output/aacvp9_faststart_test.mp4 --aac resources/aac.csv --vp9 resources/vp9.csv

dump_test:
dump_test: get_input_files
for f in input/*.mp4; do \
base=$$(basename $${f}); \
../../release/mp4-tool dump -f $${f} > output/$${base}.dump; \
Expand Down

0 comments on commit 175b489

Please sign in to comment.