Tuesday, April 11, 2017

FFmpeg: Clip Audio Files

Introduction

剪辑音频文件,提取出文件特定时间的音频资料,并且周而复始。
文献[1]示例代码:
1
ffmpeg -ss 0 -t 30 -i file.mp3 file.wav
代码
 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
% Created by LI Xu
% Version 1.0
% April 11, 2017

% Description:
% 

% If you have any question about this code,
% please do not hesitate to contact me via E-mail: 
% jeremy456@163.com

% Blog:
% http://blog.sciencenet.cn/u/lixujeremy
% http://lixuworld.blogspot.com/

% Reference
% http://stackoverflow.com/questions/7945747/how-can-you-only-extract-30-seconds-of-audio-using-ffmpeg

clear;
clc;

timebegin=tic;
cur_data=date;
cur_time=fix(clock);
str1=sprintf('%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6)); 
fprintf('Time Begin: ');
fprintf(str1);
fprintf('\n');

% XLS
xlspath='AudioInfo.xlsx';
[~, conaud]=xlsread(xlspath);
conaud(1, :)=[];

% Source Directory
SouDir='input';
% Destination Directory
DesDir='output';

% Loop
for ii=1:size(conaud, 1)
    rowrd=conaud(ii, :);
    filepath=GetFilePath(SouDir, rowrd{1});
    if filepath==0
        continue;
    end
    [~, filename, ext]=fileparts(filepath);
    inpath=fullfile(SouDir, [filename, ext]);
    otpath=fullfile(DesDir, [filename, ext]);
    if exist(otpath, 'file')
        delete(otpath);
    end
    % Start
    sttm=rowrd{2};
    % Finish
    fitm=rowrd{3};
    % Construct Sentence
    Constr=['ffmpeg -ss ', sttm, ' -t ', fitm, ' -i ', inpath, ' ', otpath];
    disp(['...Start to process ', filename, ext, '...']);
    [stuat, cmdout]=system(Constr);
    disp(['...Finish ', filename, ext, '...']);

end

fprintf('Time Begin: ');
fprintf(str1);
fprintf('\n');

cur_data=date;
cur_time=fix(clock);
str2=sprintf('%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6)); 
fprintf('Time End: ');
disp(str2);
timespan=toc(timebegin);
fprintf('Time Span: %.4f s\n', timespan);

disp('******************************************');

References

No comments:

Post a Comment