如何通过ffmpeg在源代码中将HLS流保存到本地磁盘?

时间:2013-11-25 06:23:08

标签: ffmpeg

HSL流分为多个ts文件,现在我想通过调用ffmpeg API将这些文件和m3u8文件保存到源代码中的本地磁盘上。

ffmpeg是否支持此功能?

如果是这样,如何实现这个功能?

否则,任何人都可以就如何实现这一目标向我提出一些建议吗?

非常感谢。

2 个答案:

答案 0 :(得分:2)

请从videohelp尝试此操作。

ffmpeg -i http://hls.twit.tv:1935/flosoft/smil:twitStream.smil/playlist.m3u8 -t 30 -c copy output1.mkv

答案 1 :(得分:0)

试试这个:

#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] = '\0';
            len--;
        }

        for (i = 0; line[i] != '\0'; i++) {
            if (line[i] != '\n' && line[i] != '\t') {
                break;
            }
        }

        if (line[i] == '\0') {
            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] = '\0';
            len--;
        }

        for (i = 0; line[i] != '\0'; i++) {
            if (line[i] != '\n' && line[i] != '\t') {
                break;
            }
        }

        if (line[i] == '\0') {
            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] != '\0'; i++, j++) {
                url[totalURL][j] = line[i];
            }
            url[totalURL][j] = '\0';
            printf("url: %s\t", url[totalURL]);
        }

        for (j = 0, ++i; j < 100 && line[i] != '\t' && line[i] != '\0'; i++, j++) {
            channelname[totalURL][j] = line[i];
        }
        channelname[totalURL][j] = '\0';
        printf("%s\t", channelname[totalURL]);

        for (j = 0, ++i; j < 50 && line[i] != '\t' && line[i] != '\0'; i++, j++) {
            playlist[totalURL][j] = line[i];
        }
        playlist[totalURL][j] = '\0';
        printf("%s\n", 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_link\tsub_path(OR some folder)\t*.m3u8
    readFile("analyzer.txt");


    printf("\n\n\n");
    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: %s\n", link);

        mkdir(channelname[i]);
        memset(outfilename, 0, sizeof(outfilename));
        sprintf(outfilename, "%s\\%s", channelname[i], playlist[i]);
        printf("outputfile: %s\n", 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;
}