-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.R
184 lines (157 loc) · 4.84 KB
/
functions.R
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
checkPackages <- function(packages){
for(i in 1:length(packages)){
package = packages[i]
if (!requireNamespace(package, quietly = TRUE)){
suppressMessages(install.packages(package, quiet = T))
}
}
}
getHelp <- function(){
cat("\n------>>> Enter 'Rscript bilidown.R -h' to see details of parameters setting! <<<------\n\n")
}
checKFolderExists <- function(folders){
for(i in 1:length(folders)){
if(!dir.exists(folders[i])) dir.create(folders[i])
}
}
download <- function(URL){
system(paste("you-get --no-caption -o tmp", URL))
}
downList <- function(URL){
system(paste("you-get -l --no-caption -o tmp", URL))
}
flvToMp4 <- function(flv, mp4){
for(i in 1:length(flv)){
if(flv[i] != mp4[i]) system(paste("ffmpeg -i", flv[i], mp4[i]), ignore.stderr = TRUE)
}
}
mp4ToMp3 <- function(mp4, mp3){
for(i in 1:length(mp4)){
system(paste("ffmpeg -i", mp4[i], mp3[i]), ignore.stderr = TRUE)
}
}
countSpan <- function(start, end){
extractClock <- function(time){
clock = strsplit(time, ":")[[1]]
hor = as.integer(clock[1])
min = as.integer(clock[2])
sec = as.integer(clock[3])
return(list = c(hor, min, sec))
}
timeM = sapply(c(start, end), extractClock)
hor_ST = timeM[1, 1]
min_ST = timeM[2, 1]
sec_ST = timeM[3, 1]
hor_ED = timeM[1, 2]
min_ED = timeM[2, 2]
sec_ED = timeM[3, 2]
if(sec_ED >= sec_ST){
sec_span = sec_ED - sec_ST
if(min_ED >= min_ST){
min_span = min_ED - min_ST
hor_span = hor_ED - hor_ST
} else{
min_span = min_ED + 60 - min_ST
hor_span = (hor_ED - 1) - hor_ST
}
} else{
sec_span = sec_ED + 60 - sec_ST
if((min_ED - 1) >= min_ST){
min_span = (min_ED - 1) - min_ST
hor_span = hor_ED - hor_ST
} else{
min_span = (min_ED - 1) + 60 - min_ST
hor_span = (hor_ED - 1) - hor_ST
}
}
checkTen <-function(n){
if(n < 10){
n = paste0("0", n)
} else{
n = as.character(n)
}
return(n)
}
hor_span = checkTen(hor_span)
min_span = checkTen(min_span)
sec_span = checkTen(sec_span)
span = paste0(hor_span, ":", min_span, ":", sec_span)
return(span)
}
cutMp3 <- function(input_mp3, start, end, out_mp3){
span = countSpan(start, end)
cmd = paste("ffmpeg -i", input_mp3, "-vn -acodec copy")
cmd = paste(cmd, "-ss", start)
cmd = paste(cmd, "-t", span, out_mp3)
system(cmd, , ignore.stderr = TRUE)
}
checkArgs <- function(string){
if(!is.na(string))
if(string != "no"){
return(string)
} else{
return(NULL)
}
}
getDownInfos <- function(line){
infos = strsplit(line, " ")[[1]]
args$url = checkArgs(infos[1])
args$mp3 = checkArgs(infos[2])
args$list = checkArgs(infos[3])
args$name = checkArgs(infos[4])
args$start = checkArgs(infos[5])
args$end = checkArgs(infos[6])
return(args)
}
runWorkflow <- function(args){
if(dir.exists("tmp"))
unlink("tmp", recursive = TRUE)
dir.create("tmp")
cat("\nDownloading video file ...\n")
if(is.null(args$list)){
download(args$url)
} else{
downList(args$url)
}
cat("Transcoding flv file to mp4 file\n")
oldFile = list.files("tmp", full.names = TRUE)
flvFile = gsub(" ", "_", oldFile)
file.rename(oldFile, flvFile)
mp4File = gsub(".flv", ".mp4", flvFile)
flvToMp4(flvFile, mp4File)
if(!is.null(args$mp3)){
cat("Transcoding mp4 file to mp3 file\n")
mp3File = gsub(".mp4", ".mp3", mp4File)
mp4ToMp3(mp4File, mp3File)
}
if(!is.null(args$start)){
cat("Cutting mp3 file\n")
mp3File_cut = gsub(".mp3", "_cut.mp3", mp3File)
cutMp3(mp3File, args$start, args$end, mp3File_cut)
}
cat("Copying & rename files and remove tmpFolder\n")
if(!is.null(args$name)){
mp4Out = paste0(args$mp4Folder, "/", args$name, ".mp4")
file.copy(mp4File, mp4Out)
if(!is.null(args$mp3)){
mp3Out = paste0(args$mp3Folder, "/", args$name, ".mp3")
if(!is.null(args$start)){
file.copy(mp3File_cut, mp3Out)
} else{
file.copy(mp3File, mp3Out)
}
}
} else{
file.copy(mp4File, args$mp4Folder)
if(!is.null(args$mp3)){
if(!is.null(args$start)){
file.copy(mp3File_cut, args$mp3Folder)
} else{
file.copy(mp3File, args$mp3Folder)
}
}
}
# remove tmpFolder
unlink("tmp", recursive = TRUE)
cat("Done!\n")
}