【问题标题】:Video to image sequence with Handbrake CLI使用 Handbrake CLI 的视频到图像序列
【发布时间】:2012-10-23 19:24:36
【问题描述】:

我正在尝试创建一个供内部使用的 Mac 应用程序,它获取用户指定的电影文件,将其转换为特定格式并将每一帧作为图像保存在硬盘上。 我已经使用了很棒的 Handbrake CLI 完成了转换部分。

现在我正在尝试找到一种方法将每一帧保存为图像,但我找不到这样做的方法。

似乎 ffmpeg 有一个命令可以将帧提取到图像中。以下内容在 5 秒内拉出一帧:

ffmpeg -i "infile.mp4" -r 1 -ss 00:00:05 -t 00:00:01 -vframes 1 -f image2 -y "image.jpg"

但是,我宁愿使用 QTKit 或 Handbrake CLI,这样我就不必同时将 ffmpeg 和 Handbrake 添加到应用程序中。

任何想法将不胜感激!

【问题讨论】:

    标签: xcode cocoa command-line video-processing


    【解决方案1】:

    试试这个(用视频中每秒的帧数替换 25)

    ffmpeg -i infile.mp4 -r 25 -f image2 /tmp/image-%4d.jpg
    

    然后使用 NSTask 从您的 xcode 项目中运行此命令

    Download FFMPEG O.11.1 for mac osx

    Download FFMPEG git version for mac osx

    【讨论】:

    • 不确定你明白我在问什么?我说我可以用 ffmpeg 做到这一点,但想找到一种方法来使用 Handbrake(因为我已经在使用它进行视频转换)或 QTKit。有任何想法吗? :)
    【解决方案2】:

    所以,在他们的 IRC 频道讨论之后,我确定你不能用 Handbrake 做到这一点。

    不过,这里有一种使用 ffmpeg 的相对轻松的方法:

    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    outputPipe = [NSPipe pipe];
    taskOutput = [outputPipe fileHandleForReading];
    current_task = [[NSTask alloc] init];
    
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    
    NSString *stillName = [NSString stringWithFormat:@"%@_still%d.png", [MY_FILE substringToIndex:([draggedFile length]-4)], MY_STILL_NUMBER];
    
    [current_task setLaunchPath: [NSString stringWithFormat:@"%@/ffmpeg", resourcePath]];
    
    // save just frame at current time
    // by leaving -ss before the -i, we enable direct indexing within ffmpeg, which saves a still in about a second, rather than a minute or so on a long video file
    NSArray *arguments = [NSArray arrayWithObjects:@"-ss", currentFrameTime, @"-i", draggedFile, @"-vframes", @"1", @"-f", @"image2", stillName, nil];
    
    [current_task setArguments:arguments];
    [current_task setStandardInput:[NSPipe pipe]];
    [current_task setStandardOutput:outputPipe];
    [current_task setStandardError:outputPipe];
    
    [defaultCenter addObserver:self selector:@selector(saveTaskCompleted:) name:NSTaskDidTerminateNotification object:current_task];
    
    [current_task launch];
    [taskOutput readInBackgroundAndNotify];
    

    所以,我正在使用 Handbrake 任务(转换视频),然后使用另一个任务来保存静止图像。 我可以只使用 ffmpeg,但我喜欢 Handbrake,所以我会以这种方式利用 2。

    希望这对任何人都有帮助。

    【讨论】:

      猜你喜欢
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 2014-05-02
      • 1970-01-01
      相关资源
      最近更新 更多