I am trying to create a website which allows users to play video files from anywhere on their system using React.
If I use the video tag as:
<video className='myVideo' src="/home/tashif/Downloads/CSS/1.mp4" controls autoPlay> </video>
The video just won’t load!
If I use the tag as:
<video className='myVideo' src={require("/home/tashif/Downloads/CSS/1.mp4")} controls autoPlay> </video>
I get the following error:
Module not found: Error: You attempted to import
/home/tashif/Downloads/CSS/1.mp4 which falls outside of the project
src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from
project’s node_modules/.
I want to:
- Change the src prop, so, that should be a variable
- load video files from anywhere on the system and not be limited to a particular location
Is this possible with react? Or, should I switch back to Javascript?
2
Answers
I came up with a workaround.
Create an express server to copy the local file from anywhere on the drive to the project's folder.
fs.copyFileSync('from','to');
Use the Video tag as:
<video src={require("location of the video after being copied/video.mp4")} controls autoplay>
Remember: Your video can be from anywhere, but, make sure to copy it to the same location and same name, video.mp4, as mentioned in the require(...) function.
First of all, put the video file inside your project public directory
for example for a video located in the folder /public/assets/video.mp4