-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathApp.tsx
152 lines (128 loc) · 3.68 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {StatusBar} from 'expo-status-bar';
import {StyleSheet, Text, View, Button, ActivityIndicator} from 'react-native';
import * as React from 'react';
import {FFmpegKit, FFmpegKitConfig, ReturnCode} from 'ffmpeg-kit-react-native';
import {makeDirectoryAsync, getInfoAsync, cacheDirectory} from 'expo-file-system';
import {launchImageLibraryAsync, MediaTypeOptions} from 'expo-image-picker';
import {Video, AVPlaybackStatus} from 'expo-av';
const getResultPath = async () => {
const videoDir = `${cacheDirectory}video/`;
// Checks if gif directory exists. If not, creates it
async function ensureDirExists() {
const dirInfo = await getInfoAsync(videoDir);
if (!dirInfo.exists) {
console.log("tmp directory doesn't exist, creating...");
await makeDirectoryAsync(videoDir, { intermediates: true });
}
}
await ensureDirExists();
return `${videoDir}file2.mp4`;
}
const getSourceVideo = async () => {
console.log('select video')
const result = await launchImageLibraryAsync({
mediaTypes: MediaTypeOptions.Videos
})
return (result.canceled) ? null : result.assets[0].uri
}
export default function App() {
const [result, setResult] = React.useState('');
const [source, setSource] = React.useState('');
const [isLoading, setLoading] = React.useState(false);
React.useEffect(() => {
FFmpegKitConfig.init();
}, []);
const onPress = async () => {
setLoading(() => true);
setResult(() => '');
const resultVideo = await getResultPath();
const sourceVideo = await getSourceVideo();
if (!sourceVideo) {
setLoading(() => false);
return;
}
setSource(() => sourceVideo)
const ffmpegSession = await FFmpegKit
.execute(`-i ${sourceVideo} -c:v mpeg4 -y ${resultVideo}`);
const result = await ffmpegSession.getReturnCode();
if (ReturnCode.isSuccess(result)) {
setLoading(() => false);
setResult(() => resultVideo);
} else {
setLoading(() => false);
console.error(result);
}
console.log(sourceVideo)
}
return (
<View style={styles.container}>
<StatusBar style="auto" />
<Button
onPress={onPress}
title="Select video"
color="#841584"
/>
{isLoading && <ActivityIndicator size="large" color="#ff0033" />}
<Plyr uri={source} title={'Source'} />
{result &&
<Plyr uri={result} title={'Result'} />
}
</View>
);
}
const Plyr = (props: {
title: string,
uri: string,
}) => {
const video = React.useRef(null);
const [status, setStatus] = React.useState<AVPlaybackStatus | {}>({});
return (
<View style={styles.videoContainer}>
<Text>{props.title}</Text>
<Video
ref={video}
style={styles.video}
source={{
uri: props.uri,
}}
useNativeControls
resizeMode="contain"
isLooping
onPlaybackStatusUpdate={(status: AVPlaybackStatus) => setStatus(() => status)}
/>
<View style={styles.buttons}>
<Button
title={status?.isPlaying ? 'Pause' : 'Play'}
disabled={(props.uri == '')}
onPress={() =>
status.isPlaying ? video?.current.pauseAsync() : video?.current.playAsync()
}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
videoContainer: {
backgroundColor: '#ecf0f1',
marginTop: 20,
textAlign: 'center',
padding: 10,
},
video: {
alignSelf: 'center',
width: 320,
height: 200,
},
buttons: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
});