HSL流被划分为多个ts文件,现在我想通过调用ffmpeg API将这些文件和m3u8文件保存到源代码中的本地磁盘。
ffmpeg支持这个功能吗?如果是,如何实现该功能?否则,有人能给我一些关于如何实现这一目标的建议吗?
请从视频帮助中尝试。
ffmpeg -i http://hls.twit.tv:1935/flosoft/smil:twitStream.smil/playlist.m3u8 -t 30 -c copy output1.mkv
试试这个:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
#include <time.h>
#include <curl/curl.h>
#include <direct.h>
#include <stdbool.h>
#include <errno.h>
char url[1000][100];
char channelname[1000][100];
char playlist[1000][50];
int totalURL;
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
void downloadFile(char *link, char *outfilename) {
CURL *curl;
FILE *fp;
CURLcode res;
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename, "wb");
if (!fp) {
printf("File "%s" Can't open. Error: %d[%s]n", outfilename, errno, strerror(errno));
curl_easy_cleanup(curl);
return;
}
curl_easy_setopt(curl, CURLOPT_URL, link);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
}
void getNewLink(int myIndex, FILE *fp) {
char line[1000], link[1000], outfilename[256];
int i, j = 0, len;
FILE *fp1;
int flag = 0;
memset(line, 0, sizeof(line));
while (!feof(fp)) {
if (fgets(line, 1000, fp) == 0) {
if (!feof(fp)) {
printf("Error in File reading.n");
fclose(fp);
}
return;
}
len = strlen(line);
if (line[len - 1] == 'n') {
line[len - 1] = ' ';
len--;
}
for (i = 0; line[i] != ' '; i++) {
if (line[i] != 'n' && line[i] != 't') {
break;
}
}
if (line[i] == ' ') {
continue;
}
if (!strncmp(line, "#EXT-X-STREAM-INF:", 15)) {
flag = 1;
continue;
}
if(flag == 1){
flag = 0;
memset(link, 0, sizeof(link));
strcpy(link, url[myIndex]);
strcat(link, channelname[myIndex]);
strcat(link, "/");
strcat(link, line);
sprintf(outfilename, "chunk.m3u8");
downloadFile(link, outfilename);
fp1 = fopen(outfilename, "r");
if (!fp1) {
printf("File "%s" Can't open. Error: %d[%s]n", outfilename, errno, strerror(errno));
continue;
}
getNewLink(myIndex, fp1);
fclose(fp1);
continue;
}
if (!strncmp(line, "#EXTINF:", 8)) {
flag = 2;
continue;
}
if (flag == 2) {
j++;
memset(link, 0, sizeof(link));
strcpy(link, url[myIndex]);
strcat(link, channelname[myIndex]);
strcat(link, "/");
strcat(link, line);
memset(outfilename, 0, sizeof(outfilename));
sprintf(outfilename, "%s\chunk%d.ts", channelname[i], j);
downloadFile(link, outfilename);
flag = 0;
continue;
}
}
}
void readFile(char *filename) {
FILE *fp;
char line[1000];
int i, j, len;
memset(line, 0, sizeof(line));
fp = fopen(filename, "r");
if (!fp) {
printf("File "%s" Can't open. Error: %d[%s]n", filename, errno, strerror(errno));
return;
}
while (!feof(fp)) {
if (fgets(line, 1000, fp) == 0) {
if (!feof(fp)) {
printf("Error in File reading.n");
fclose(fp);
}
return;
}
len = strlen(line);
if (line[len - 1] == 'n') {
line[len - 1] = ' ';
len--;
}
for (i = 0; line[i] != ' '; i++) {
if (line[i] != 'n' && line[i] != 't') {
break;
}
}
if (line[i] == ' ') {
continue;
}
if (strncmp(line, "http", 4)) {
printf("Ignoring comment line.n");
continue;
}
if (!strncmp(line, "http", 4)) {
for (j = 0; i < 100 && j < 100 && line[i] != 't' && line[i] != ' '; i++, j++) {
url[totalURL][j] = line[i];
}
url[totalURL][j] = ' ';
printf("url: %st", url[totalURL]);
}
for (j = 0, ++i; j < 100 && line[i] != 't' && line[i] != ' '; i++, j++) {
channelname[totalURL][j] = line[i];
}
channelname[totalURL][j] = ' ';
printf("%st", channelname[totalURL]);
for (j = 0, ++i; j < 50 && line[i] != 't' && line[i] != ' '; i++, j++) {
playlist[totalURL][j] = line[i];
}
playlist[totalURL][j] = ' ';
printf("%sn", playlist[totalURL]);
totalURL++;
}
}
int main() {
FILE *fp;
char link[200];
int i;
char outfilename[FILENAME_MAX];
//analyzer file format should be like this: "http://devimages.apple.com/iphone/samples/ bipbop bipbopall.m3u8"
// http_linktsub_path(OR some folder)t*.m3u8
readFile("analyzer.txt");
printf("nnn");
for (i = 0; i < totalURL; i++) {
memset(link, 0, sizeof(link));
strcpy(link, url[i]);
strcat(link, channelname[i]);
strcat(link, "/");
strcat(link, playlist[i]);
printf("URL: %sn", link);
mkdir(channelname[i]);
memset(outfilename, 0, sizeof(outfilename));
sprintf(outfilename, "%s\%s", channelname[i], playlist[i]);
printf("outputfile: %sn", outfilename);
downloadFile(link, outfilename);
fp = fopen(outfilename, "r");
if (!fp) {
printf("File "%s" Can't open. Error: %d[%s]n", outfilename, errno, strerror(errno));
break;
}
getNewLink(i, fp);
fclose(fp);
}
return 0;
}