Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
F
ffmpeg.wasm-core
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Linshizhi
ffmpeg.wasm-core
Commits
7b4bbb41
Commit
7b4bbb41
authored
Sep 25, 2020
by
Jerome Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update example and tests
parent
4c5b5c51
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
91 additions
and
9 deletions
+91
-9
.gitignore
wasm/.gitignore
+1
-1
transcode.html
wasm/examples/transcode.html
+73
-0
transcode.js
wasm/examples/transcode.js
+2
-2
transcode.test.js
wasm/tests/transcode.test.js
+15
-6
No files found.
wasm/.gitignore
View file @
7b4bbb41
*.mp4
/examples/
*.mp4
/node_modules
/node_modules
/dist
/dist
/cache
/cache
wasm/transcode.html
→
wasm/
examples/
transcode.html
View file @
7b4bbb41
...
@@ -17,7 +17,7 @@
...
@@ -17,7 +17,7 @@
<h3>
Upload a video to transcode to mp4 (x264) and play!
</h3>
<h3>
Upload a video to transcode to mp4 (x264) and play!
</h3>
<video
id=
"output-video"
controls
></video><br/>
<video
id=
"output-video"
controls
></video><br/>
<input
type=
"file"
id=
"uploader"
>
<input
type=
"file"
id=
"uploader"
>
<p
id=
"message"
>
Remeber to wait for 5 seconds for ffmpeg.wasm to load
</p>
<p
id=
"message"
>
Update a video
</p>
<script
type=
"text/javascript"
>
<script
type=
"text/javascript"
>
const
readFromBlobOrFile
=
(
blob
)
=>
(
const
readFromBlobOrFile
=
(
blob
)
=>
(
new
Promise
((
resolve
,
reject
)
=>
{
new
Promise
((
resolve
,
reject
)
=>
{
...
@@ -33,43 +33,41 @@
...
@@ -33,43 +33,41 @@
);
);
const
message
=
document
.
getElementById
(
'message'
);
const
message
=
document
.
getElementById
(
'message'
);
const
transcode
=
async
({
target
:
{
files
}
})
=>
{
const
transcode
=
async
({
target
:
{
files
}
})
=>
{
const
{
name
}
=
files
[
0
];
let
resolve
=
null
;
const
Core
=
await
createFFmpegCore
({
printErr
:
(
m
)
=>
console
.
log
(
m
),
print
:
(
m
)
=>
{
console
.
log
(
m
);
if
(
m
.
startsWith
(
'FFMPEG_END'
))
{
resolve
();
}
}
});
message
.
innerHTML
=
'Writing file to MEMFS'
;
message
.
innerHTML
=
'Writing file to MEMFS'
;
const
data
=
await
readFromBlobOrFile
(
files
[
0
]);
const
data
=
await
readFromBlobOrFile
(
files
[
0
]);
Module
.
FS
.
writeFile
(
name
,
new
Uint8Array
(
data
));
Core
.
FS
.
writeFile
(
'video.avi'
,
new
Uint8Array
(
data
));
const
ffmpeg
=
Module
.
cwrap
(
'proxy_main'
,
'number'
,
[
'number'
,
'number'
]);
const
ffmpeg
=
Core
.
cwrap
(
'proxy_main'
,
'number'
,
[
'number'
,
'number'
]);
const
args
=
[
'ffmpeg'
,
'-hide_banner'
,
'-nostdin'
,
'-report'
,
'-i'
,
name
,
'out.mp4'
];
// const args = ['ffmpeg', '-hide_banner', '-nostdin', '-i', 'video.mp4', '-vf', 'scale=512:-1', '-vcodec', 'h264', '-acodec', 'copy', 'scaled.mp4'];
const
argsPtr
=
Module
.
_malloc
(
args
.
length
*
Uint32Array
.
BYTES_PER_ELEMENT
);
const
args
=
[
'ffmpeg'
,
'-hide_banner'
,
'-nostdin'
,
'-i'
,
'video.avi'
,
'-acodec'
,
'copy'
,
'video.mp4'
];
const
argsPtr
=
Core
.
_malloc
(
args
.
length
*
Uint32Array
.
BYTES_PER_ELEMENT
);
args
.
forEach
((
s
,
idx
)
=>
{
args
.
forEach
((
s
,
idx
)
=>
{
const
buf
=
Modul
e
.
_malloc
(
s
.
length
+
1
);
const
buf
=
Cor
e
.
_malloc
(
s
.
length
+
1
);
Modul
e
.
writeAsciiToMemory
(
s
,
buf
);
Cor
e
.
writeAsciiToMemory
(
s
,
buf
);
Modul
e
.
setValue
(
argsPtr
+
(
Uint32Array
.
BYTES_PER_ELEMENT
*
idx
),
buf
,
'i32'
);
Cor
e
.
setValue
(
argsPtr
+
(
Uint32Array
.
BYTES_PER_ELEMENT
*
idx
),
buf
,
'i32'
);
});
});
const
d
=
Date
.
now
();
console
.
time
(
`[
${
d
}
]
${
files
[
0
].
name
}
execution time`
);
message
.
innerHTML
=
'Start to transcode'
;
message
.
innerHTML
=
'Start to transcode'
;
console
.
time
(
'execution time'
);
ffmpeg
(
args
.
length
,
argsPtr
);
ffmpeg
(
args
.
length
,
argsPtr
);
await
new
Promise
((
_resolve
)
=>
{
resolve
=
_resolve
});
/*
const
output
=
Core
.
FS
.
readFile
(
'video.mp4'
);
* The execution of ffmpeg is not synchronized,
Core
.
FS
.
unlink
(
'video.mp4'
);
* so we need to parse the log file to check if completed.
const
video
=
document
.
getElementById
(
'output-video'
);
*/
video
.
src
=
URL
.
createObjectURL
(
new
Blob
([
output
.
buffer
],
{
type
:
'video/mp4'
}));
const
timer
=
setInterval
(()
=>
{
console
.
timeEnd
(
`[
${
d
}
]
${
files
[
0
].
name
}
execution time`
);
const
logFileName
=
Module
.
FS
.
readdir
(
'.'
).
find
(
name
=>
name
.
endsWith
(
'.log'
));
if
(
typeof
logFileName
!==
'undefined'
)
{
const
log
=
String
.
fromCharCode
.
apply
(
null
,
Module
.
FS
.
readFile
(
logFileName
));
if
(
log
.
includes
(
"frames successfully decoded"
))
{
clearInterval
(
timer
);
message
.
innerHTML
=
'Finish transcoding'
;
const
out
=
Module
.
FS
.
readFile
(
'out.mp4'
);
const
video
=
document
.
getElementById
(
'output-video'
);
video
.
src
=
URL
.
createObjectURL
(
new
Blob
([
out
.
buffer
],
{
type
:
'video/mp4'
}));
console
.
timeEnd
(
'execution time'
);
}
}
},
500
);
};
};
document
.
getElementById
(
'uploader'
).
addEventListener
(
'change'
,
transcode
);
document
.
getElementById
(
'uploader'
).
addEventListener
(
'change'
,
transcode
);
</script>
</script>
<script
type=
"text/javascript"
src=
".
/dist-old
/ffmpeg-core.js"
></script>
<script
type=
"text/javascript"
src=
".
./dist
/ffmpeg-core.js"
></script>
</body>
</body>
</html>
</html>
wasm/
ffmpeg
.js
→
wasm/
examples/transcode
.js
View file @
7b4bbb41
const
path
=
require
(
'path'
);
const
path
=
require
(
'path'
);
const
fs
=
require
(
'fs'
);
const
fs
=
require
(
'fs'
);
const
createFFmpegCore
=
require
(
'./dist/ffmpeg-core'
);
const
createFFmpegCore
=
require
(
'.
.
/dist/ffmpeg-core'
);
(
async
()
=>
{
(
async
()
=>
{
let
resolve
=
null
;
let
resolve
=
null
;
...
@@ -13,7 +13,7 @@ const createFFmpegCore = require('./dist/ffmpeg-core');
...
@@ -13,7 +13,7 @@ const createFFmpegCore = require('./dist/ffmpeg-core');
}
}
}
}
});
});
const
filePath
=
path
.
join
(
__dirname
,
'tests'
,
'data'
,
'video-3s.avi'
);
const
filePath
=
path
.
join
(
__dirname
,
'
..'
,
'
tests'
,
'data'
,
'video-3s.avi'
);
const
data
=
Uint8Array
.
from
(
fs
.
readFileSync
(
filePath
));
const
data
=
Uint8Array
.
from
(
fs
.
readFileSync
(
filePath
));
Core
.
FS
.
writeFile
(
'video.avi'
,
data
);
Core
.
FS
.
writeFile
(
'video.avi'
,
data
);
...
...
wasm/tests/transcode.test.js
View file @
7b4bbb41
...
@@ -3,13 +3,15 @@ const path = require('path');
...
@@ -3,13 +3,15 @@ const path = require('path');
const
createFFmpegCore
=
require
(
'../dist/ffmpeg-core'
);
const
createFFmpegCore
=
require
(
'../dist/ffmpeg-core'
);
const
{
parseArgs
,
ffmpeg
}
=
require
(
'./utils'
);
const
{
parseArgs
,
ffmpeg
}
=
require
(
'./utils'
);
const
filePath
=
path
.
join
(
__dirname
,
'data'
,
'video-3s.avi'
);
const
filePath
=
path
.
join
(
__dirname
,
'data'
,
'video-3s.avi'
);
let
Core
=
null
;
let
resolve
=
null
;
let
data
=
null
;
let
data
=
null
;
beforeAll
(
async
()
=>
{
beforeAll
(
async
()
=>
{
data
=
Uint8Array
.
from
(
fs
.
readFileSync
(
filePath
));
data
=
Uint8Array
.
from
(
fs
.
readFileSync
(
filePath
));
Core
=
await
createFFmpegCore
({
});
test
(
'transcode avi to x264 mp4'
,
async
()
=>
{
let
resolve
=
null
;
const
Core
=
await
createFFmpegCore
({
printErr
:
()
=>
{},
printErr
:
()
=>
{},
print
:
(
m
)
=>
{
print
:
(
m
)
=>
{
if
(
m
.
startsWith
(
'FFMPEG_END'
))
{
if
(
m
.
startsWith
(
'FFMPEG_END'
))
{
...
@@ -18,9 +20,6 @@ beforeAll(async () => {
...
@@ -18,9 +20,6 @@ beforeAll(async () => {
},
},
});
});
Core
.
FS
.
writeFile
(
'video.avi'
,
data
);
Core
.
FS
.
writeFile
(
'video.avi'
,
data
);
});
test
(
'transcode avi to x264 mp4'
,
async
()
=>
{
ffmpeg
(
Core
,
[
'-i'
,
'video.avi'
,
'video.mp4'
]);
ffmpeg
(
Core
,
[
'-i'
,
'video.avi'
,
'video.mp4'
]);
await
new
Promise
((
_resolve
)
=>
{
resolve
=
_resolve
});
await
new
Promise
((
_resolve
)
=>
{
resolve
=
_resolve
});
const
fileSize
=
Core
.
FS
.
readFile
(
'video.mp4'
).
length
;
const
fileSize
=
Core
.
FS
.
readFile
(
'video.mp4'
).
length
;
...
@@ -30,6 +29,16 @@ test('transcode avi to x264 mp4', async () => {
...
@@ -30,6 +29,16 @@ test('transcode avi to x264 mp4', async () => {
test
(
'transcode avi to x264 mp4 twice'
,
async
()
=>
{
test
(
'transcode avi to x264 mp4 twice'
,
async
()
=>
{
for
(
let
i
=
0
;
i
<
2
;
i
++
)
{
for
(
let
i
=
0
;
i
<
2
;
i
++
)
{
let
resolve
=
null
;
const
Core
=
await
createFFmpegCore
({
printErr
:
()
=>
{},
print
:
(
m
)
=>
{
if
(
m
.
startsWith
(
'FFMPEG_END'
))
{
resolve
();
}
},
});
Core
.
FS
.
writeFile
(
'video.avi'
,
data
);
ffmpeg
(
Core
,
[
'-i'
,
'video.avi'
,
'video.mp4'
]);
ffmpeg
(
Core
,
[
'-i'
,
'video.avi'
,
'video.mp4'
]);
await
new
Promise
((
_resolve
)
=>
{
resolve
=
_resolve
});
await
new
Promise
((
_resolve
)
=>
{
resolve
=
_resolve
});
const
fileSize
=
Core
.
FS
.
readFile
(
'video.mp4'
).
length
;
const
fileSize
=
Core
.
FS
.
readFile
(
'video.mp4'
).
length
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment