Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native generate thumbnail for video url

I have videos that I'd like to present as thumbnails before a user clicks on them for the full video. They are not local, I only have the url. Is there a RN component to do this? The RN Image component does not take videos urls as sources.

like image 849
omriki Avatar asked Jan 05 '23 23:01

omriki


2 Answers

It's possible using Expo Video Thumbnail library

just like this example

import React from 'react';
import { StyleSheet, Button, View, Image, Text } from 'react-native';
import * as VideoThumbnails from 'expo-video-thumbnails';

export default class App extends React.Component {
  state = {
    image: null,
  };

  generateThumbnail = async () => {
    try {
      const { uri } = await VideoThumbnails.getThumbnailAsync(
        'http://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4',
        {
          time: 15000,
        }
      );
      this.setState({ image: uri });
    } catch (e) {
      console.warn(e);
    }
  };

  render() {
    const { image } = this.state;
    return (
      <View style={styles.container}>
        <Button onPress={this.generateThumbnail} title="Generate thumbnail" />
        {image && <Image source={{ uri: image }} style={{ width: 200, height: 200 }} />}
        <Text>{image}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
});

-UPDATE-

another way to do this is using video library without playing the video and the controllers

example:

npm install --save react-native-video



import Video from 'react-native-video';

// Within your render function, assuming you have a file called
// "background.mp4" in your project. You can include multiple videos
// on a single screen if you like.

<Video source={{uri: "background"}}   // Can be a URL or a local file.
       paused={true}
controls={false}
/>

in this example, it will display the video without playing it, so basically will give you the thumbnail.

P.S: Not recommended if you have multiple videos in the same view more than 10.

like image 114
Balalen Avatar answered Jan 07 '23 14:01

Balalen


Not possible. Video thumbnail cannot be auto-generated from video URL. It should be created and stored along with the video in backend database and when RN app receives video URL, it should receive thumbnail URL too. Then you can use Image and TouchableOpacity components to add pressing behavior on the thumbnail.

However, if you use Youtube videos only, then react-native-thumbnail-video might be a quick solution to your problem.

like image 38
blaz Avatar answered Jan 07 '23 14:01

blaz