- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 MediaCodec
和 MediaMuxer
的帮助下对视频进行编码。结果我有 mp4 视频文件。如何为此 mp4 文件设置元数据(创建时间)? MediaMetadataRetriever只能读取元数据,不能更改。我不想使用 ffmpeg。我试过 mp4parser库 ( this class ),但它对我不起作用。
最佳答案
在 MP4 文件中设置元数据不是一项明确的任务,因为没有 generally supported specification ,但大多数视频播放器都支持 Apple 规范。
这是在 MP4 元数据中设置标题和创建日期的代码(基于 MetaDataInsert.java
示例):
import com.coremedia.iso.IsoFile;
import com.coremedia.iso.boxes.*;
import com.coremedia.iso.boxes.apple.AppleItemListBox;
import com.googlecode.mp4parser.boxes.apple.AppleNameBox;
import com.googlecode.mp4parser.boxes.apple.AppleRecordingYear2Box;
import com.googlecode.mp4parser.util.Path;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.util.List;
public class Mp4MetadataWriter {
public FileChannel splitFileAndInsert(File f, long pos, long length) throws IOException {
FileChannel read = new RandomAccessFile(f, "r").getChannel();
File tmp = File.createTempFile("ChangeMetaData", "splitFileAndInsert");
FileChannel tmpWrite = new RandomAccessFile(tmp, "rw").getChannel();
read.position(pos);
tmpWrite.transferFrom(read, 0, read.size() - pos);
read.close();
FileChannel write = new RandomAccessFile(f, "rw").getChannel();
write.position(pos + length);
tmpWrite.position(0);
long transferred = 0;
while ((transferred += tmpWrite.transferTo(0, tmpWrite.size() - transferred, write)) != tmpWrite.size()) {
System.out.println(transferred);
}
System.out.println(transferred);
tmpWrite.close();
tmp.delete();
return write;
}
private boolean needsOffsetCorrection(IsoFile isoFile) {
if (Path.getPath(isoFile, "moov[0]/mvex[0]") != null) {
// Fragmented files don't need a correction
return false;
} else {
// no correction needed if mdat is before moov as insert into moov want change the offsets of mdat
for (Box box : isoFile.getBoxes()) {
if ("moov".equals(box.getType())) {
return true;
}
if ("mdat".equals(box.getType())) {
return false;
}
}
throw new RuntimeException("I need moov or mdat. Otherwise all this doesn't make sense");
}
}
public void writeMetadata(String videoFilePath, String theTitle, String theDate) throws IOException {
File videoFile = new File(videoFilePath);
if (!videoFile.exists()) {
throw new FileNotFoundException("File " + videoFilePath + " not exists");
}
if (!videoFile.canWrite()) {
throw new IllegalStateException("No write permissions to file " + videoFilePath);
}
IsoFile isoFile = new IsoFile(videoFilePath);
MovieBox moov = isoFile.getBoxes(MovieBox.class).get(0);
FreeBox freeBox = findFreeBox(moov);
boolean correctOffset = needsOffsetCorrection(isoFile);
long sizeBefore = moov.getSize();
long offset = 0;
for (Box box : isoFile.getBoxes()) {
if ("moov".equals(box.getType())) {
break;
}
offset += box.getSize();
}
// Create structure or just navigate to Apple List Box.
UserDataBox userDataBox;
if ((userDataBox = Path.getPath(moov, "udta")) == null) {
userDataBox = new UserDataBox();
moov.addBox(userDataBox);
}
MetaBox metaBox;
if ((metaBox = Path.getPath(userDataBox, "meta")) == null) {
metaBox = new MetaBox();
HandlerBox hdlr;
hdlr = new HandlerBox();
hdlr.setHandlerType("mdir");
metaBox.addBox(hdlr);
userDataBox.addBox(metaBox);
}
AppleItemListBox ilst;
if ((ilst = Path.getPath(metaBox, "ilst")) == null) {
ilst = new AppleItemListBox();
metaBox.addBox(ilst);
}
if (freeBox == null) {
freeBox = new FreeBox(128 * 1024);
metaBox.addBox(freeBox);
}
// Got Apple List Box
AppleNameBox nam;
if ((nam = Path.getPath(ilst, AppleNameBox.TYPE)) == null) {
nam = new AppleNameBox();
}
nam.setDataCountry(0);
nam.setDataLanguage(0);
nam.setValue(theTitle);
ilst.addBox(nam);
AppleRecordingYear2Box day;
if ((day = Path.getPath(ilst, "©day")) == null) {
day = new AppleRecordingYear2Box();
}
day.setDataCountry(0);
day.setDataLanguage(0);
day.setValue(theDate);
ilst.addBox(day);
long sizeAfter = moov.getSize();
long diff = sizeAfter - sizeBefore;
// This is the difference of before/after
// can we compensate by resizing a Free Box we have found?
if (freeBox.getData().limit() > diff) {
// either shrink or grow!
freeBox.setData(ByteBuffer.allocate((int) (freeBox.getData().limit() - diff)));
sizeAfter = moov.getSize();
diff = sizeAfter - sizeBefore;
}
if (correctOffset && diff != 0) {
correctChunkOffsets(moov, diff);
}
BetterByteArrayOutputStream baos = new BetterByteArrayOutputStream();
moov.getBox(Channels.newChannel(baos));
isoFile.close();
FileChannel fc;
if (diff != 0) {
// this is not good: We have to insert bytes in the middle of the file
// and this costs time as it requires re-writing most of the file's data
fc = splitFileAndInsert(videoFile, offset, sizeAfter - sizeBefore);
} else {
// simple overwrite of something with the file
fc = new RandomAccessFile(videoFile, "rw").getChannel();
}
fc.position(offset);
fc.write(ByteBuffer.wrap(baos.getBuffer(), 0, baos.size()));
fc.close();
}
FreeBox findFreeBox(Container c) {
for (Box box : c.getBoxes()) {
System.err.println(box.getType());
if (box instanceof FreeBox) {
return (FreeBox) box;
}
if (box instanceof Container) {
FreeBox freeBox = findFreeBox((Container) box);
if (freeBox != null) {
return freeBox;
}
}
}
return null;
}
private void correctChunkOffsets(MovieBox movieBox, long correction) {
List<ChunkOffsetBox> chunkOffsetBoxes = Path.getPaths((Box) movieBox, "trak/mdia[0]/minf[0]/stbl[0]/stco[0]");
if (chunkOffsetBoxes.isEmpty()) {
chunkOffsetBoxes = Path.getPaths((Box) movieBox, "trak/mdia[0]/minf[0]/stbl[0]/st64[0]");
}
for (ChunkOffsetBox chunkOffsetBox : chunkOffsetBoxes) {
long[] cOffsets = chunkOffsetBox.getChunkOffsets();
for (int i = 0; i < cOffsets.length; i++) {
cOffsets[i] += correction;
}
}
}
private static class BetterByteArrayOutputStream extends ByteArrayOutputStream {
byte[] getBuffer() {
return buf;
}
}
}
用法:
new Mp4MetadataWriter().writeMetadata("/home/user/downloads/1.mp4", "Yet another video title", "2020");
关于android - 将元数据设置为 mp4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203235/
我正在使用以下代码播放声音,过一会儿它将停止播放声音,这是因为我相信有太多的Mediaplayer打开实例,所以我添加了一个额外的mp.release();,这只会使我的应用程序崩溃(目前已被注释掉)
我正在查看 XV-6 代码,它通过它识别 MP 结构。它首先在 EBDA 的第一个 kb 中搜索。代码是这样的 static struct mp* mpsearch(void) { uchar *
我在我的应用程序中使用 Mp 饼图。它显示非常小的尺寸,我试图增加它的尺寸但它没有增加它的尺寸。我无法找出问题所在。请告诉我们如何增加尺寸。 这是我的代码: public class MPpiecha
如何使用 MPAndroidChart 实现此目的? 使用版本:com.github.PhilJay:MPAndroidChart:v3.1.0-alpha 添加图例和饼图边距的代码: private
亲爱的社区,我面临以下问题,我正在使用此处提供的 MP android 图表库创建条形图:https://github.com/PhilJay/MPAndroidChart . 我想为我的条设置渐变背
我正在使用 SAS MP Connect 开发我的第一段代码,以运行同一个 sas 作业的并行线程。 我知道 MP CONNECT 仅受可用 CPU 数量的物理限制,但理想情况下我不想在我的工作中使用
我最近购买了在 Linux 服务器上运行的 Stata MP12(8 核)许可证。 有没有人写过 Stata 程序,比如说模拟研究来测试 Stata MP 的性能?我想监视在作业处理过程中实际使用的内
我将不胜感激任何“一步一步”指南,说明如何更改 PHP/HTML 页面上的动态数据库连接/连接字符串/等上的代码,使其“即插即用”工作通过 ftp 将页面和 MySQL 数据库托管在“Azure 网站
试图在我的应用程序中放置一个“暂停”按钮,以播放一些声音片段循环播放。 当我打电话mp.pause();一切都破了,我完全迷路了! 这是我正在使用的方法。 protected void man
我想使用 Mp Chart 创建折线图 我想要实现的是这张图片 但是到目前为止我已经得到了这个。 我使用的代码是这个 private fun setData() { val entries
通常,我可能会编写一个类似simd的循环: float * x = (float *) malloc(10 * sizeof(float)); float * y = (float *) malloc
在与堆栈空间、OpenMP 以及如何处理这些问题相关的其他帖子上,有很多回复。但是,我找不到信息来真正理解 OpenMP 调整编译器选项的原因: 原因是什么-fopenmp在 gfortran 中暗示
我有一段代码,可以根据漂移、波动性和随机数计算任意给定日期的股票价格。但是当我检查输出列表时 - 它们是算术级数,而不是几何级数(幂函数)。我共享的变量有问题吗? 代码如下: #include #i
我正在尝试在 C++11 中并行化动态编程算法使用这种方法: void buildBaseCases() { cout << "Building base cases" << endl
我正在 open MP 中实现并行点积 我有这个代码: #include #include #include #include #include #include #define SIZE
我有一台服务器已经将近 4 年了,直到现在我都没有遇到任何问题(主机端)。我一直在更换主机,因为 ddos 的东西试图找到最适合我的东西。现在我买了一个 VPS(这不是我的第一个)并尝试运行我的服
所以我有两个内部平行区域的外部平行区域。是否可以将 2 个线程放入外部平行线,将 4 个线程放入每个内部平行线?我做了这样的东西,但它似乎无法按照我想要的方式工作。有什么建议吗? start_r =
我希望有人指出我们遇到的问题或解决方法。 使用/MP 编译项目时,似乎只有同一文件夹中的文件会同时编译。我使用 Process Explorer 滑动命令行并确认行为。 项目过滤器似乎对并发编译的内容
本文整理了Java中me.chanjar.weixin.mp.api.WxMpMessageRouter类的一些代码示例,展示了WxMpMessageRouter类的具体用法。这些代码示例主要来源于G
我正在监视 Stata/MP(Stata/SE 的多核版本)的 CPU 和内存使用情况,但我不是 Stata 程序员(更像是 Perl 人)。 任何人都可以发布一些代码,利用公共(public)数据集
我是一名优秀的程序员,十分优秀!