派生自 projectDept/qhighschool

胡仁荣
2022-10-31 fe7381d6e8ec1f427408de0297ac7f41533202f6
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
package com.qxueyou.scc.teach.live.utils;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.qxueyou.scc.base.util.UUIDUtils;
import com.qxueyou.scc.teach.live.utils.FfmpegFileVO;
import com.qxueyou.scc.teach.live.utils.FfmpegVideoInfo;
 
/**
 * ÊÓÆµ×ªÂëÀà
 * @author cyq
 *
 */
public class FfmpegMediaHelper {
    /**
     * ÊµÀý»¯log
     */
    private static final Logger log = LogManager.getLogger(FfmpegMediaHelper.class);
 
    /**
     * ¸ßÇåÂëÂÊ
     */
    private static int bitRateHd = 800 * 1024;
    /**
     * ±êÇåÂëÂÊ
     */
    private static int bitRateSd = 400 * 1024;
    /**
     * µÍÇåÂëÂÊ
     */
    private static int bitRateLd = 200 * 1024;
    /**
     * ×ªmp4µÄ¹Ì¶¨ÃüÁî
     */
    private static List<String> commandMp4 = new ArrayList<String>(10);
    /**
     * ×ªm3u8µÄ¹Ì¶¨ÃüÁî
     */
    private static String commandM3u8;
 
    /**
     * ÊÇ·ñΪÊÓÆµ
     */
    private static Boolean videoFlag = false;
 
    /**
     * ×ªmp4
     * @param fileVO
     * @param videoInfo
     * @return
     */
    public static boolean convertMp4(FfmpegFileVO fileVO, FfmpegVideoInfo videoInfo) throws Exception {
        if (!checkfile(fileVO.getInputPath())) {
            return false;
        }
        try {
            System.out.println("FfmpegMediaHelper--תÂëmp4ÖÐ...");
            commandMp4.clear();
            commandMp4.add("ffmpeg");
            commandMp4.add("-i");
            commandMp4.add(fileVO.getInputPath());
            commandMp4.add("-ab");        // ÉèÖÃÒôƵÂëÂÊ
            commandMp4.add("128*1024");
            commandMp4.add("-r");
            commandMp4.add("23");
    
            if (videoInfo.getVideoHeight() >= 1000) {
                convertMp4Hd(fileVO, videoInfo);
                convertMp4Sd(fileVO, videoInfo);
                convertMp4Ld(fileVO, videoInfo);
            } else if (videoInfo.getVideoHeight() >= 700) {
                convertMp4Sd(fileVO, videoInfo);
                convertMp4Ld(fileVO, videoInfo);
            } else {
                convertMp4Ld(fileVO, videoInfo);
            }
            System.out.println("FfmpegMediaHelper--תÂëmp4Íê³É...");
            return true;
        } catch (Exception e) {
            System.out.println("FfmpegMediaHelper--תÂëmp4Òì³£...");
            log.error("תÂëmp4Òì³£:" + e.getMessage());
            throw e;
        }
    }
    /**
     * ×ªm3u8
     * @param fileVO
     * @param videoInfo
     * @return
     */
    public static boolean convertM3u8(FfmpegFileVO fileVO, FfmpegVideoInfo videoInfo) throws Exception {
        if (!checkfile(fileVO.getInputPath())) {
            return false;
        }
        try {
            System.out.println("FfmpegMediaHelper--תÂëm3h8ÖÐ...");
            commandM3u8 = "ffmpeg -i " + fileVO.getInputPath() + " -strict -2 -c:v libx264 -c:a aac -f hls -hls_time 10 -hls_list_size 0";
    
            if (videoInfo.getVideoHeight() >= 1000) {
                convertM3u8Hd(fileVO, videoInfo);
                convertM3u8Sd(fileVO, videoInfo);
                convertM3u8Ld(fileVO, videoInfo);
            } else if (videoInfo.getVideoHeight() >= 700) {
                convertM3u8Sd(fileVO, videoInfo);
                convertM3u8Ld(fileVO, videoInfo);
            } else {
                convertM3u8Ld(fileVO, videoInfo);
            }
            System.out.println("FfmpegMediaHelper--תÂëm3u8Íê³É...");
            return true;
        } catch (Exception e) {
            System.out.println("FfmpegMediaHelper--תÂëm3u8Òì³£...");
            log.error("תÂëm3u8Òì³£:" + e.getMessage());
            throw e;
        }
    }
    
    /**
     * ×ª¸ßÇåmp4
     */
    private static void convertMp4Hd(FfmpegFileVO fileVO, FfmpegVideoInfo videoInfo) throws Exception {
        try {
            String resolution = (int) (1080 * videoInfo.getAspectRatio() / 2) * 2 + "*1080"; // ¿í¸ß±ØÐëÊÇżÊý
            List<String> command = new ArrayList<String>(commandMp4);
            if (videoInfo.getVideoBitRate() == null || videoInfo.getVideoBitRate() > bitRateHd) {
                command.add("-vb"); // ÊÓÆµÂëÂÊ
                command.add(String.valueOf(bitRateHd));
            }
            command.add("-s"); // ³ß´ç£¨·Ö±æÂÊ£©
            command.add(resolution);
            command.add(fileVO.getOutputPath() + videoInfo.getUuid() + "mp4-hd.mp4");
            convertMp4(command);
            videoInfo.setMp4HdUrl(fileVO.getOutputPath() + videoInfo.getUuid() + "mp4-hd.mp4");    // ×ªÂë³É¹¦£¬½«Â·¾¶ÉèÖõ½videoInfoÖÐ
        } catch (Exception e) {
            throw e;
        }
    }
    /**
     * ×ª¸ßÇåm3u8
     */
    private static void convertM3u8Hd(FfmpegFileVO fileVO, FfmpegVideoInfo videoInfo) throws Exception {
        try {
            String resolution = (int) (1080 * videoInfo.getAspectRatio() / 2) * 2 + "*1080"; // ¿í¸ß±ØÐëÊÇżÊý
            
            StringBuffer commandBuffer = new StringBuffer(commandM3u8);
            if (videoInfo.getVideoBitRate() == null || videoInfo.getVideoBitRate() > bitRateHd) {
                commandBuffer.append(" -vb ").append(String.valueOf(bitRateHd)).append(' ');
            }
            commandBuffer.append(" -s ").append(resolution).append(' ');
            commandBuffer.append(fileVO.getOutputPath());
            commandBuffer.append(videoInfo.getUuid()).append("m3u8-hd.m3u8");
            convertM3u8(commandBuffer.toString());
            videoInfo.setM3u8HdUrl(fileVO.getOutputPath() + videoInfo.getUuid() + "m3u8-hd.m3u8");
        } catch (Exception e) {
            throw e;
        }
    }
    
    /**
     * ×ª±êÇåmp4
     */
    private static void convertMp4Sd(FfmpegFileVO fileVO, FfmpegVideoInfo videoInfo) throws Exception {
        try {
            String resolution = (int) (720 * videoInfo.getAspectRatio() / 2) * 2 + "*720";
            List<String> command = new ArrayList<String>(commandMp4);
            if (videoInfo.getVideoBitRate() == null || videoInfo.getVideoBitRate() > bitRateSd) {
                command.add("-vb");
                command.add(String.valueOf(bitRateSd));
            }
            command.add("-s");
            command.add(resolution);
            command.add(fileVO.getOutputPath() + videoInfo.getUuid() + "mp4-sd.mp4");
            convertMp4(command);
            videoInfo.setMp4SdUrl(fileVO.getOutputPath() + videoInfo.getUuid() + "mp4-sd.mp4");
        } catch (Exception e) {
            throw e;
        }
    }
    /**
     * ×ª±êÇåm3u8
     */
    private static void convertM3u8Sd(FfmpegFileVO fileVO, FfmpegVideoInfo videoInfo) throws Exception {
        try {
            String resolution = (int) (720 * videoInfo.getAspectRatio() / 2) * 2 + "*720";
            StringBuffer commandBuffer = new StringBuffer(commandM3u8);
            if (videoInfo.getVideoBitRate() == null || videoInfo.getVideoBitRate() > bitRateSd) {
                commandBuffer.append(" -vb ").append(String.valueOf(bitRateSd)).append(' ');
            }
            commandBuffer.append(" -s ").append(resolution).append(' ');
            commandBuffer.append(fileVO.getOutputPath()).append(videoInfo.getUuid()).append("m3u8-sd.m3u8");
            convertM3u8(commandBuffer.toString());
            videoInfo.setM3u8SdUrl(fileVO.getOutputPath() + videoInfo.getUuid() + "m3u8-sd.m3u8");
        } catch (Exception e) {
            throw e;
        }
    }
 
    /**
     * ×ªµÍÇåmp4
     */
    private static void convertMp4Ld(FfmpegFileVO fileVO, FfmpegVideoInfo videoInfo) throws Exception {
        try    {
            String resolution = (int) (480 * videoInfo.getAspectRatio() / 2) * 2 + "*480";
            List<String> command = new ArrayList<String>(commandMp4);
            if (videoInfo.getVideoBitRate() == null || videoInfo.getVideoBitRate() > bitRateLd) {
                command.add("-vb");
                command.add(String.valueOf(bitRateLd));
            }
            command.add("-s");
            command.add(resolution);
            command.add(fileVO.getOutputPath() + videoInfo.getUuid() + "mp4-ld.mp4");
            convertMp4(command);
            videoInfo.setMp4LdUrl(fileVO.getOutputPath() + videoInfo.getUuid() + "mp4-ld.mp4");
        } catch (Exception e) {
            throw e;
        }
    }
    /**
     * ×ªµÍÇåm3u8
     */
    private static void convertM3u8Ld(FfmpegFileVO fileVO, FfmpegVideoInfo videoInfo) throws Exception {
        try {
            String resolution = (int) (480 * videoInfo.getAspectRatio() / 2) * 2 + "*480";
            StringBuffer commandBuffer = new StringBuffer(commandM3u8);
            if (videoInfo.getVideoBitRate() == null || videoInfo.getVideoBitRate() > bitRateLd) {
                commandBuffer.append(" -vb ").append(String.valueOf(bitRateSd)).append(' ');
            }
            commandBuffer.append(" -s ").append(resolution).append(' ');
            commandBuffer.append(fileVO.getOutputPath()).append(videoInfo.getUuid()).append("m3u8-ld.m3u8");
            convertM3u8(commandBuffer.toString());
            videoInfo.setM3u8LdUrl(fileVO.getOutputPath() + videoInfo.getUuid() + "m3u8-ld.m3u8");
        } catch (Exception e) {
            throw e;
        }
    }
 
    /**
     * »ñȡýÌåÐÅÏ¢
     * 
     * @param fileVO
     * @param videoInfo
     * @return
     */
    public static boolean mediaInfo(FfmpegFileVO fileVO, FfmpegVideoInfo videoInfo) throws Exception {
        if (!checkfile(fileVO.getInputPath())) {
            return false;
        }
        BufferedReader buf = null; // ±£´æffmpegµÄÊä³ö½á¹ûÁ÷
        String currLine = null;
        String line = null;
        try {
 
            log.debug("»ñȡýÌåÐÅÏ¢...");
            List<String> command = new ArrayList<String>();
 
            // ffprobe ÃüÁî»ñȡýÌåÐÅÏ¢
            command.add("ffprobe");
            command.add("-print_format");
 
            // json¸ñʽÊä³ö
            command.add("json");
 
            command.add("-show_streams");
 
            command.add(fileVO.getInputPath());
            ProcessBuilder process = new ProcessBuilder();
            process.command(command);
            process.redirectErrorStream(true);
            Process p = process.start();
 
            buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((currLine = buf.readLine()) != null) {
                // ´¦Àí¶àÓà×Ö·û
                line = currLine.trim().replaceAll("\"", "").replaceAll(",", "").replace(" ", "");
                handleLine(line, videoInfo);
 
                // »ñÈ¡Îļþʱ³¤(flvÐèÒªÕâôȡ)
                if(currLine.trim().startsWith("Duration:") && currLine.contains(",") ){
                    String strFirst = currLine.trim().split(",")[0].substring(9).trim().substring(0,8);
                    String[] arrTime = strFirst.split(":");
                    if(null != arrTime && arrTime.length == 3 ){
                        Integer iHour = Integer.parseInt(arrTime[0]);
                        Integer iMinute = Integer.parseInt(arrTime[1]);
                        Integer iSecond = Integer.parseInt(arrTime[2]);
                        
                        videoInfo.setPlayTime(iHour * 60 * 60 + iMinute * 60 + iSecond);
                        log.debug("ÊÓÆµÊ±³¤£º" + videoInfo.getPlayTime());
                    }
                }
            }
 
            p.waitFor();// ÕâÀïÏß³Ì×èÈû£¬½«µÈ´ýÍⲿת»»½ø³ÌÔËÐгɹ¦ÔËÐнáÊøºó£¬²ÅÍùÏÂÖ´ÐÐ
 
            log.debug("»ñȡýÌåÐÅÏ¢Íê³É");
            return true;
 
        } catch (Exception e) {
            log.debug("»ñȡýÌåÐÅÏ¢Òì³£:" + e.getMessage());
            throw e;
        }
    }
    private static void handleLine(String line, FfmpegVideoInfo videoInfo) {
        if (line.contains(":")) {
            String[] arr = line.split(":");
            if (null != arr && arr.length == 2) {
                // 1. Èç¹ûÊDZê־λ
                if ("codec_type".equals(arr[0].trim())) {
                    // ÊÓÆµ
                    if ("video".equals(arr[1].trim())) {
                        videoFlag = true;
                    }
                    // ÒôƵ
                    if ("audio".equals(arr[1].trim())) {
                        videoFlag = false;
                    }
                }
                // ÊÓÆµ¿í¶È
                if (videoFlag && "width".equals(arr[0])) {
                    videoInfo.setVideoWidth(Integer.valueOf(arr[1]));
                    if (videoInfo.getVideoHeight() != null) {
                        videoInfo.setAspectRatio((float) (videoInfo.getVideoWidth().floatValue() / videoInfo.getVideoHeight().floatValue()));
                        log.debug("¼ÆËã¿í¸ß±È£º" + videoInfo.getVideoWidth() + "/" + videoInfo.getVideoHeight() + ":" + videoInfo.getAspectRatio());
                    }
                }
                // ÊÓÆµ¸ß¶È
                if (videoFlag && "height".equals(arr[0])) {
                    videoInfo.setVideoHeight(Integer.valueOf(arr[1]));
                    if (videoInfo.getVideoWidth() != null) {
                        videoInfo.setAspectRatio((float) (videoInfo.getVideoWidth().floatValue() / videoInfo.getVideoHeight().floatValue()));
                        log.debug("¼ÆËã¿í¸ß±È£º" + videoInfo.getVideoWidth() + "/" + videoInfo.getVideoHeight() + ":" + videoInfo.getAspectRatio());
                    }
                }
                
                //»ñÈ¡ÊÓÆÁÖ¡Âʱí´ïʽ
                if (videoFlag && "avg_frame_rate".equals(arr[0])) {
                    log.info("¿ªÊ¼¼ÆËãÖ¡ÂÊ£¬"+arr[1]);
                    if(StringUtils.isNotEmpty(arr[1])){
                        String [] strFrameArr = arr[1].split("/");
                        if(strFrameArr!=null && strFrameArr.length==2 && Double.valueOf(strFrameArr[1].trim())>0){
                            int frameRate = (int) Math.ceil((Double.valueOf(strFrameArr[0].trim())/Double.valueOf(strFrameArr[1].trim())));
                            videoInfo.setFrameRate(frameRate);    
                            videoInfo.setFrameRateExp(arr[1]);
                            log.info("¼ÆËãÖ¡ÂÊ£ºframeRate="+frameRate+" frameRateExp="+arr[1]);
                        }
                    }
                }
                
                
                // ÊÓÆµÂëÂÊ
                if (videoFlag && "bit_rate".equals(arr[0])) {
                    videoInfo.setVideoBitRate(Integer.valueOf(arr[1]));
                    log.debug("ÊÓÆµÂëÂÊ£º" + videoInfo.getVideoBitRate());
                }
                // Îļþʱ³¤
                if (videoFlag && "duration".equals(arr[0])) {
                    if (arr[1].contains(".")) {
                        //videoInfo.setPlayTime(Integer.valueOf(arr[1].indexOf('.')));
                        videoInfo.setPlayTime(Integer.parseInt(arr[1].substring(0, arr[1].indexOf('.'))));
                    } else {
                        videoInfo.setPlayTime(Integer.valueOf(arr[1]));
                    }
                    log.debug("ÊÓÆµÊ±³¤£º" + videoInfo.getPlayTime());
                }
            }
        }
    }
 
 
    /**
     * ½ØÍ¼
     * 
     * @param fileVO
     * @param outputPath
     * @return
     */
    public static boolean snapshot(FfmpegFileVO fileVO) throws Exception {
        if (!checkfile(fileVO.getInputPath())) {
            return false;
        }
        try {
 
            log.debug("FfmpegMediaHelper--ÊÓÆµ½ØÆÁ...");
            String comm = "ffmpeg -ss 00:00:01 -i " + fileVO.getInputPath() + " -t 0.1 -f mjpeg -y " + fileVO.getOutputPath() + "snapshot.jpg";
    
            Process videoProcess = Runtime.getRuntime().exec(comm);
            new PrintStreamThread(videoProcess.getErrorStream()).start();
    
            videoProcess.waitFor();
            log.debug("FfmpegMediaHelper--ÊÓÆµ½ØÆÁÍê³É");
            return true;
        } catch (Exception e) {
            log.error("ÊÓÆµ½ØÆÁÒì³£" + e.getMessage());
            throw e;
        }
    }
    
    /**
     * ¸ù¾ÝÖ¡ÂÊ½ØÆÁ
     * @param fileVO ´ý½ØÍ¼ÊÓÆµÎļþÐÅÏ¢
     * @param imgPrefixName ½ØÈ¡Í¼Æ¬Ãû³ÆµÄǰ׺ÐÅÏ¢
     * @param framePerSecond Ã¿ÃëͼƬ֡Êý
     * @param width  ½ØÈ¡Í¼Æ¬¿í¶È(ps£ºµ¥Î»ÏñËØµã)
     * @param height ½ØÈ¡Í¼Æ¬¸ß¶È(ps£ºµ¥Î»ÏñËØµã)
     * @return ÊÇ·ñ½ØÈ¡³É¹¦
     * @throws Exception
     */
    
    /**
     * 
     * @param fileVO  ÎļþÐÅÏ¢
     * @param imgPrefixName  ½ØÈ¡Í¼Æ¬µÄǰ׺ÐÅÏ¢
     * @param framePerSecond  Ã¿Ãë¶àÉÙÖ¡
     * @return
     * @throws Exception
     */
    
    public static boolean screenShot(FfmpegFileVO fileVO,String imgPrefixName,int framePerSecond,int width,int height) throws Exception {
        if (!checkfile(fileVO.getInputPath())) {
            return false;
        }
 
        if(framePerSecond<=0 || width * height<=0){
            return false;
        }
        
        Process process = null;
        try {
            
//            ffmpeg  -ss 00:00:00.000  -i D:/usr/qxueyou/huifang/test.mp4 -f image2 -vf fps=fps=1/5 D:/usr/qxueyou/huifang/pic/new_%d.jpg
            
            String comm = "ffmpeg -ss 00:00:00.000 -i " + fileVO.getInputPath() + " -f image2 -s " + width + "*" + height + " -vf fps=fps=1/"+ framePerSecond +" "+ fileVO.getOutputPath()+"/" + imgPrefixName + "%d.jpg";
            log.info("screenShotÃüÁî, "+ comm);
            process = Runtime.getRuntime().exec(comm);
            new PrintStreamThread(process.getErrorStream()).start();
            process.waitFor();
            return true;
        } catch (Exception e) {
            log.error("ÊÓÆµ½ØÆÁÒì³£" + e.getMessage());
            throw e;
        }finally {
            closeProcess(process);
        }
    }
    
    
    
 
    public static boolean dividVideo(FfmpegFileVO fileVO,int statTime,int duration) throws Exception {
        if (!checkfile(fileVO.getInputPath())) {
            return false;
        }
        Process process = null;
        try {
            String comm = "ffmpeg -ss "+ statTime + " -t " + duration + " -i "+ fileVO.getInputPath() + " -y -vcodec copy -acodec copy " + fileVO.getOutputPath();
            log.info("dividVideoÃüÁî, "+ comm);
            process = Runtime.getRuntime().exec(comm);
            new PrintStreamThread(process.getErrorStream()).start();
            process.waitFor();
            return true;
        } catch (Exception e) {
            log.error("ÇиîÊÓÆµÊ§°Ü" + e.getMessage());
            throw e;
        }finally {
            closeProcess(process);
        }
    }
    
    
    
    /**
     * ºÏ²¢ÊÓÆµ
     * 
     * @param fileVO
     * @return
     * @throws Exception
     */
    public static Process mergeVideo(FfmpegFileVO fileVO) throws Exception {
        if (!checkfile(fileVO.getInputPath())) {
            return null;
        }
        
        String comm = "ffmpeg -f concat -safe 0 -i " + fileVO.getInputPath() + " -c copy " + fileVO.getOutputPath();
        log.info("mergeVideoÃüÁî, "+ comm);
        
        Process videoProcess = Runtime.getRuntime().exec(comm);
        new PrintStreamThread(videoProcess.getErrorStream()).start();
 
        videoProcess.waitFor();
        return videoProcess;
    }
    
    
    
    /**
     * ºÏ²¢ÊÓÆµ
     * 
     * @param fileVO
     * @return
     * @throws Exception
     */
    public static boolean concatVideo(FfmpegFileVO fileVO) throws Exception {
        Process process = null;
        try {
            List<String> command = new ArrayList<String>(20);
            command.add("ffmpeg");
            command.add("-f");
            command.add("concat");
            command.add("-safe");
            command.add("0");
            command.add("-i");
            command.add( fileVO.getInputPath());
            command.add( "-y");
            command.add( "-c");
            command.add( "copy");
            command.add( "-bsf:a");
            command.add( "aac_adtstoasc");
            command.add( "-f");
            command.add( "mp4");
            command.add(fileVO.getOutputPath());
            ProcessBuilder pBuilder = new ProcessBuilder(command);
            pBuilder.directory(new File(fileVO.getInputPath()).getParentFile());
            log.info("ProcessBuilder directory:"+pBuilder.directory().getAbsolutePath());
            process = pBuilder.start();
            new PrintStreamThread(process.getErrorStream()).start();
            process.waitFor();
            return true;
        } catch (Exception e) {
            throw e;
        } finally {
            closeProcess(process);
        }
    }
    
    
    public static String converUploadVideoToLiveMp4(FfmpegFileVO fileVO,FfmpegVideoInfo videoInfo) throws Exception{
        String outputPath = null;
        try {
//            log.info("FfmpegMediaHelper.TransferUploadVideoToWyMp4,Ö±²¥ÉÏ´«...");
            List<String> command = new ArrayList<String>();
            String name = UUIDUtils.generateUUID().replace("-", "");
            outputPath = fileVO.getOutputPath() +name + ".mp4";
            command.add("ffmpeg");
            command.add("-i");
            command.add(fileVO.getInputPath());
            command.add("-q:v");
            command.add("6");
//            command.add(" -r ");
//            command.add(videoInfo.getFrameRateExp());
            command.add("-pix_fmt");
            command.add("yuv420p");
            command.add(outputPath);
            convertMp4(command);
            log.error(command.toString());
        } catch (Exception e) {
            throw e;
        }
        return outputPath;
     }
    
    /**
     * mp4תtsÎļþ
     * ffmpeg -i 1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts out1.ts
     * 
     * @param fileVO
     * @return
     * @throws Exception
     */
    public static boolean mp4toannexb(FfmpegFileVO fileVO) throws Exception {
        Process process= null;
        try {
            String comm = "ffmpeg -i " + fileVO.getInputPath() + " -c copy -bsf:v h264_mp4toannexb -y -f mpegts " + fileVO.getOutputPath();
            log.info("mp4toannexbÃüÁî, "+ comm);
            process = Runtime.getRuntime().exec(comm);
            new PrintStreamThread(process.getErrorStream()).start();
            process.waitFor();
            return true;
        } catch (Exception e) {
            throw e;
        }finally {
            closeProcess(process);
        }
    }
    
    private static void closeProcess(Process process) throws Exception {
        try {
            if (process != null) {
                if (process.getInputStream() != null) {
                    process.getInputStream().close();
                }
                if (process.getOutputStream() != null) {
                    process.getOutputStream().close();
                }
                if (process.getErrorStream() != null) {
                    process.getErrorStream().close();
                }
                process.destroy();
            }
        } catch (IOException e) {
            log.error("¹Ø±Õprocessʧ°Ü¡£¡£", e);
        }
    }
    
 
    /**
     * ¹Ø±Õ½ø³Ì
     * 
     * @param process
     * @return
     */
    public static boolean stop(Process process) {  
        if (process != null) {  
            process.destroy();  
            return true;  
        }  
        return false;  
    } 
 
    /**
     * ½âÎö³ÉMP4¸ñʽ
     * 
     * @param oldfilepath
     * @return
     */
    private static boolean convertMp4(List<String> command) throws Exception {
        Process process = null;
        try {
            process = new ProcessBuilder(command).redirectErrorStream(true).start();
            new PrintStreamThread(process.getInputStream()).start();
            process.waitFor();
            return true;
        } catch (Exception e) {
            throw e;
        } finally {
            if (process != null) {
                try {
                    process.getInputStream().close();
                    process.getOutputStream().close();
                    process.getErrorStream().close();
                } catch (IOException e) {
                    throw e;
                }
            }
        }
    }
 
    /**
     * ½âÎöm3u8¸ñʽ
     * 
     * @param oldfilepath
     * @return
     */
    private static boolean convertM3u8(String command) throws Exception {
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(command);
 
            new PrintStreamThread(process.getErrorStream()).start();
 
            process.waitFor();
 
            return true;
        } catch (Exception e) {
            throw e;
        } finally {
            if (process != null) {
                try {
                    process.getInputStream().close();
                    process.getOutputStream().close();
                    process.getErrorStream().close();
                } catch (IOException e) {
                    throw e;
                }
            }
        }
    }
    
    /**
     * ¼ì²éתÂëÊäÈëÎļþÊÇ·ñ´æÔÚ
     * @param path
     * @return
     */
    private static boolean checkfile(String path) {
        File file = new File(path);
        return file.isFile();
    }
}
/**
 * ´òÓ¡Ïß³Ì
 * @author cyq
 *
 */
class PrintStreamThread extends Thread {
    /**
     * ÊäÈëÁ÷
     */
    java.io.InputStream __is;
 
    /**
     * ¹¹Ôì·½·¨
     * @param is
     */
    public PrintStreamThread(java.io.InputStream is) {
        __is = is;
    }
 
    /**
     * Ïß³ÌÖ´Ðз½·¨
     */
    public void run() {
        try {
            while (this != null) {
                if (__is.read() == -1) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}