Skip to content

Commit 9f5f7da

Browse files
committed
Add image2video example
1 parent 6a8d4bb commit 9f5f7da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+80
-2
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"rules": {
44
"no-underscore-dangle": 0,
55
"linebreak-style": 0,
6-
"global-require": 0
6+
"global-require": 0,
7+
"no-await-in-loop": 0,
78
},
89
"env": {
910
"browser": true,

examples/browser/image2video.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<html>
2+
<head>
3+
<script src="/dist/ffmpeg.dev.js"></script>
4+
<style>
5+
html, body {
6+
margin: 0;
7+
width: 100%;
8+
height: 100%
9+
}
10+
body {
11+
display: flex;
12+
flex-direction: column;
13+
align-items: center;
14+
}
15+
</style>
16+
</head>
17+
<body>
18+
<h3>Click start to transcode images to mp4 (x264) and play!</h3>
19+
<video id="output-video" controls></video><br/>
20+
<button id="start-btn">Start</button>
21+
<p id="message"></p>
22+
<a href="https://github.com/ffmpegjs/ffmpeg.js/tree/master/tests/assets/triangle">Data Set</a>
23+
<script>
24+
const { createWorker } = FFmpeg;
25+
const worker = createWorker({
26+
corePath: '../../node_modules/@ffmpeg/core/ffmpeg-core.js',
27+
progress: (p) => console.log(p),
28+
});
29+
30+
const image2video = async () => {
31+
const message = document.getElementById('message');
32+
message.innerHTML = 'Loading ffmpeg-core.js';
33+
await worker.load();
34+
message.innerHTML = 'Loading data';
35+
await worker.write('audio.ogg', '../../tests/assets/triangle/audio.ogg');
36+
for (let i = 0; i < 60; i += 1) {
37+
const num = `00${i}`.slice(-3);
38+
await worker.write(`tmp.${num}.png`, `../../tests/assets/triangle/tmp.${num}.png`);
39+
}
40+
message.innerHTML = 'Start transcoding';
41+
await worker.run('-framerate 30 -pattern_type glob -i *.png -i audio.ogg -c:a copy -shortest -c:v libx264 -pix_fmt yuv420p out.mp4');
42+
const { data } = await worker.read('out.mp4');
43+
44+
const video = document.getElementById('output-video');
45+
video.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }));
46+
}
47+
const elm = document.getElementById('start-btn');
48+
elm.addEventListener('click', image2video);
49+
</script>
50+
</body>
51+
</html>

examples/node/image2video.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const fs = require('fs');
2+
const { createWorker } = require('../../src');
3+
4+
const worker = createWorker({
5+
progress: (p) => console.log(p),
6+
});
7+
8+
(async () => {
9+
console.log('Loading ffmpeg-core.js');
10+
await worker.load();
11+
console.log('Loading data');
12+
await worker.write('audio.ogg', '../../tests/assets/triangle/audio.ogg');
13+
for (let i = 0; i < 60; i += 1) {
14+
const num = `00${i}`.slice(-3);
15+
await worker.write(`tmp.${num}.png`, `../../tests/assets/triangle/tmp.${num}.png`);
16+
}
17+
console.log('Start transcoding');
18+
await worker.run('-framerate 30 -pattern_type glob -i *.png -i audio.ogg -c:a copy -shortest -c:v libx264 -pix_fmt yuv420p out.mp4');
19+
const { data } = await worker.read('out.mp4');
20+
console.log('Complete transcoding');
21+
fs.writeFileSync('out.mp4', Buffer.from(data));
22+
process.exit(0);
23+
})();

src/utils/extractProgress.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const ts2sec = (ts) => {
88
module.exports = ({ message }, progress) => {
99
if (message.startsWith(' Duration')) {
1010
const ts = message.split(', ')[0].split(': ')[1];
11-
duration = ts2sec(ts);
11+
const d = ts2sec(ts);
12+
if (duration === 0 || duration > d) {
13+
duration = d;
14+
}
1215
} else if (message.startsWith('frame')) {
1316
const ts = message.split('time=')[1].split(' ')[0];
1417
const t = ts2sec(ts);

tests/assets/triangle/audio.ogg

463 KB
Binary file not shown.

tests/assets/triangle/tmp.000.png

18.5 KB

tests/assets/triangle/tmp.001.png

25 KB

tests/assets/triangle/tmp.002.png

28.7 KB

tests/assets/triangle/tmp.003.png

31 KB

tests/assets/triangle/tmp.004.png

32.6 KB

0 commit comments

Comments
 (0)