Skip to main content

getStaticFiles()

从 v3.3.26 版本开始可用。

note

此 API 正在迁移到 @remotion/studio 包中。建议从 @remotion/studio 导入 API,而不是从 remotion 导入。

获取包含 public/ 文件夹中所有文件的数组。
您可以通过使用 staticFile() 来引用它们。

warning

此功能仅在 Remotion Studio 中以及在渲染期间有效,否则将返回一个空数组。

note

在 Linux 上,从 Node.js v19.1.0 开始,才支持监视子目录中的更改。如果您使用早于该版本的版本,则需要手动刷新 Remotion Studio 浏览器选项卡。

example.ts
tsx
import { getStaticFiles, StaticFile, Video } from "remotion";
 
const files = getStaticFiles();
/*
[
{
"name": "video.mp4",
"src": "/static-7n5spa/video.mp4",
"sizeInBytes": 432944,
"lastModified": 1670170466865
},
{
"name": "assets/data.json",
"src": "/static-7n5spa/assets/data.json",
"sizeInBytes": 1311,
"lastModified": 1670170486089
},
]
*/
 
// ❗ Don't pass the `name` directly to the `src` of a media element
const videoName = files[0].name;
 
// ✅ Wrap it in staticFile() instead or use `src`
const videoSrc = files[0].src;
 
// Find a file by it's name and import it
const data = files.find((f) => {
return f.name === "video.mp4";
}) as StaticFile; // Use `as StaticFile` to assert the file exists
 
// Use the `src` property to get a src to pass to a media element
<Video src={data.src} />;
example.ts
tsx
import { getStaticFiles, StaticFile, Video } from "remotion";
 
const files = getStaticFiles();
/*
[
{
"name": "video.mp4",
"src": "/static-7n5spa/video.mp4",
"sizeInBytes": 432944,
"lastModified": 1670170466865
},
{
"name": "assets/data.json",
"src": "/static-7n5spa/assets/data.json",
"sizeInBytes": 1311,
"lastModified": 1670170486089
},
]
*/
 
// ❗ Don't pass the `name` directly to the `src` of a media element
const videoName = files[0].name;
 
// ✅ Wrap it in staticFile() instead or use `src`
const videoSrc = files[0].src;
 
// Find a file by it's name and import it
const data = files.find((f) => {
return f.name === "video.mp4";
}) as StaticFile; // Use `as StaticFile` to assert the file exists
 
// Use the `src` property to get a src to pass to a media element
<Video src={data.src} />;

API

不接受任何参数,并返回一个对象数组,每个对象都有四个条目:

  • name: 相对于 public 文件夹的路径。

    note

    即使在 Windows 上也包含正斜杠 /

  • src: 带有前缀的路径。每当 Studio 服务器重新启动时,前缀都会更改。

  • sizeInBytes: 文件大小。如果是符号链接,则报告原始文件的文件大小。

  • lastModified: 最后修改日期,以毫秒为单位的 Unix 时间戳。

最大文件数

为了性能考虑,只获取和显示前 10000 个项目。

变更日志

在 v4.0.64 之前,仅显示前 1000 个项目。

另请参阅