This's a simple video duration check program built with Electron. However, it doesn't work as expected; I also tried to use Electron IPC communication in others projects, but always fails. I can upload a video successfully and submit it, after that nothings happens, any errors or advices. Debugger shows nothing too. I built a new project from zero and have same issue. Path value is also not showed on console
main.js:
const electron = require('electron'),
app = electron.app,
BrowserWindow = electron.BrowserWindow
const ffmpeg = require('fluent-ffmpeg')
const ipc = require('electron').ipcMain
let mainWindow
app.on('ready', () => {
mainWindow = new BrowserWindow({})
mainWindow.loadFile('./index.html')
})
ipc.on('video:submit', (event, path) => {
ffmpeg.ffprobe(path, (metadata) => {
event.returnValue = metadata.format.duration
})
})
index.html:
<html>
<head>
</head>
<body>
<form id="form">
<h1>Video Info</h1>
<div>
<label>Select a video</label>
<input type="file" accept="video/*" id="input">
</div>
<button type="submit" id="sb">Get info</button>
<div id="result"></div>
</form>
</body>
<script>
require('./renderer.js')
</script>
</html>
renderer.js:
const ipc = require('electron').ipcRenderer,
form = document.querySelector('#form')
let result = document.querySelector('#result')
console.log(path)
form.addEventListener('submit', () => {
const path = document.querySelector('#input').files[0].path
let reply = ipc.sendSync('video:submit', path)
result.innerHTML = 'Video is' + reply + 'seconds!'
})
EDIT
I made some changes on main and renderer to use asynchronous send and reply. I don't get what I want, but after submit some content, it's name is replaced with "No file chosen". Path value stills not printed.
changes on main.js:
ipc.on('video:submit', (event, path) => {
ffmpeg.ffprobe(path, (metadata) => {
let duration = metadata.format.duration
event.reply('duration', duration)
})
})
changes on renderer.js:
form.addEventListener('submit', () => {
const path = document.querySelector('#input').files[0].path
ipc.on('duration', (event, duration) => {
console.log(duration)
})
ipc.send('video:submit', path)
result.innerHTML = 'Video is' + duration + 'seconds!'
})