Trim Video in ReactJS
By Akanksha Yadav — A quick learner!
In some scenarios, when you upload a video on a web page, you want to trim the video for some specific start and end time on the client side before uploading the video to the server.
Trimming the video helps to keep the required content only. Here, we will use nouislider-react to get the start and end time from the video and ffmpeg to trim the video according to that time.
First off, create your React App.
Then we will be using the following package for the slider to get the start and end time from the video:
“nouislider-react”: “³.4.1”
ffmpeg -i input.mp4 -ss 00:05:10 -to 00:15:30 -acodec copy -vcodec copy out.mp4
The above command will take the input video input.mp4, and cut it. The cut video will be from 00:05:10 to 00:15:30, resulting in 10 minutes and 20 seconds video.
We are using WebAssembly to run ffmpeg, so we need to add some Cross-Origin headers to our app, for this create a file called setupProxy.js in your src directory or add the following in your server.js file.
https://gist.github.com/hiakku/d491e587bd5815812930e39d5758b286The above will automatically add headers when we run our create react app.
In our App.js file:
Now, you could test the application. Run npm start to start your react application and upload a video. You can play with the slider to set the range of the video and then trim it. It may take some seconds to update, you could check the ffmpeg progress log in the console. Once successful, you could use the trimmed URL obtained to upload to the server. You can view the entire project using this github link. This is just a starting point, you could transcode and reduce video quality to support lower bandwidth users, multiple resolutions, and so on!!
References:
[embed]https://www.npmjs.com/package/nouislider-react[/embed]Author
Editor
We at CaratLane are solving some of the most intriguing challenges to make our mark in the relatively uncharted omnichannel jewellery industry. If you are interested in tackling such obstacles, feel free to drop your updated resume/CV to careers@caratlane.com!
Leave a Reply