This repository has been archived by the owner on Feb 9, 2018. It is now read-only.
/
TSMuxerVideo.java
915 lines (807 loc) · 29.7 KB
/
TSMuxerVideo.java
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
/*
* PS3 Media Server, for streaming any medias to your PS3.
* Copyright (C) 2008 A.Brochard
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package net.pms.encoders;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import net.pms.Messages;
import net.pms.configuration.PmsConfiguration;
import net.pms.configuration.RendererConfiguration;
import net.pms.dlna.*;
import net.pms.formats.Format;
import net.pms.io.*;
import net.pms.util.CodecUtil;
import net.pms.util.FormLayoutUtil;
import net.pms.util.PlayerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.*;
import java.net.URL;
import java.util.List;
import java.util.Locale;
import javax.swing.*;
import static net.pms.formats.v2.AudioUtils.getLPCMChannelMappingForMencoder;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.startsWith;
// FIXME rename TsMuxeRVideo
public class TSMuxerVideo extends Player {
private static final Logger LOGGER = LoggerFactory.getLogger(TSMuxerVideo.class);
private static final String COL_SPEC = "left:pref, 0:grow";
private static final String ROW_SPEC = "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, 0:grow";
public static final String ID = "tsmuxer";
private PmsConfiguration configuration;
public TSMuxerVideo(PmsConfiguration configuration) {
this.configuration = configuration;
}
@Override
public boolean excludeFormat(Format extension) {
String m = extension.getMatchedId();
return m != null &&
!m.equals("mp4") &&
!m.equals("mkv") &&
!m.equals("ts") &&
!m.equals("tp") &&
!m.equals("m2ts") &&
!m.equals("m2t") &&
!m.equals("mpg") &&
!m.equals("evo") &&
!m.equals("mpeg") &&
!m.equals("vob") &&
!m.equals("m2v") &&
!m.equals("mts") &&
!m.equals("mov");
}
@Override
public int purpose() {
return VIDEO_SIMPLEFILE_PLAYER;
}
@Override
public String id() {
return ID;
}
@Override
public boolean isTimeSeekable() {
return true;
}
@Override
public String[] args() {
return null;
}
@Override
public String executable() {
return configuration.getTsmuxerPath();
}
@Override
public ProcessWrapper launchTranscode(
String fileName,
DLNAResource dlna,
DLNAMediaInfo media,
OutputParams params
) throws IOException {
setAudioAndSubs(fileName, media, params, configuration);
PipeIPCProcess ffVideoPipe;
ProcessWrapperImpl ffVideo;
PipeIPCProcess ffAudioPipe[] = null;
ProcessWrapperImpl ffAudio[] = null;
String fps = media.getValidFps(false);
int width = media.getWidth();
int height = media.getHeight();
if (width < 320 || height < 240) {
width = -1;
height = -1;
}
String videoType = "V_MPEG4/ISO/AVC";
if (startsWith(media.getCodecV(), "mpeg2")) {
videoType = "V_MPEG-2";
}
if (this instanceof TsMuxerAudio && media.getFirstAudioTrack() != null) {
String fakeFileName = writeResourceToFile("/resources/images/fake.jpg");
ffVideoPipe = new PipeIPCProcess(System.currentTimeMillis() + "fakevideo", System.currentTimeMillis() + "videoout", false, true);
String timeEndValue1 = "-t";
String timeEndValue2 = "" + params.timeend;
if (params.timeend < 1) {
timeEndValue1 = "-y";
timeEndValue2 = "-y";
}
String[] ffmpegLPCMextract = new String[] {
configuration.getFfmpegPath(),
timeEndValue1, timeEndValue2,
"-loop", "1",
"-i", fakeFileName,
"-qcomp", "0.6",
"-qmin", "10",
"-qmax", "51",
"-qdiff", "4",
"-me_range", "4",
"-f", "h264",
"-vcodec", "libx264",
"-an",
"-y",
ffVideoPipe.getInputPipe()
};
videoType = "V_MPEG4/ISO/AVC";
OutputParams ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffVideo = new ProcessWrapperImpl(ffmpegLPCMextract, ffparams);
if (
fileName.toLowerCase().endsWith(".flac") &&
media.getFirstAudioTrack().getBitsperSample() >= 24 &&
media.getFirstAudioTrack().getSampleRate() % 48000 == 0
) {
ffAudioPipe = new PipeIPCProcess[1];
ffAudioPipe[0] = new PipeIPCProcess(System.currentTimeMillis() + "flacaudio", System.currentTimeMillis() + "audioout", false, true);
String[] flacCmd = new String[] {
configuration.getFlacPath(),
"--output-name=" + ffAudioPipe[0].getInputPipe(),
"-d",
"-f",
"-F",
fileName
};
ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffAudio = new ProcessWrapperImpl[1];
ffAudio[0] = new ProcessWrapperImpl(flacCmd, ffparams);
} else {
ffAudioPipe = new PipeIPCProcess[1];
ffAudioPipe[0] = new PipeIPCProcess(System.currentTimeMillis() + "mlpaudio", System.currentTimeMillis() + "audioout", false, true);
String depth = "pcm_s16le";
String rate = "48000";
if (media.getFirstAudioTrack().getBitsperSample() >= 24) {
depth = "pcm_s24le";
}
if (media.getFirstAudioTrack().getSampleRate() > 48000) {
rate = "" + media.getFirstAudioTrack().getSampleRate();
}
String[] flacCmd = new String[] {
configuration.getFfmpegPath(),
"-i", fileName,
"-ar", rate,
"-f", "wav",
"-acodec", depth,
"-y",
ffAudioPipe[0].getInputPipe()
};
ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffAudio = new ProcessWrapperImpl[1];
ffAudio[0] = new ProcessWrapperImpl(flacCmd, ffparams);
}
} else {
params.waitbeforestart = 5000;
params.manageFastStart();
String mencoderPath = configuration.getMencoderPath();
ffVideoPipe = new PipeIPCProcess(System.currentTimeMillis() + "ffmpegvideo", System.currentTimeMillis() + "videoout", false, true);
// Special handling for evo files
String evoValue1 = "-quiet";
String evoValue2 = "-quiet";
if (fileName.toLowerCase().endsWith(".evo")) {
evoValue1 = "-psprobe";
evoValue2 = "1000000";
}
String[] ffmpegLPCMextract = new String[] {
mencoderPath,
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
params.stdin != null ? "-" : fileName,
evoValue1, evoValue2,
"-really-quiet",
"-msglevel", "statusline=2",
"-ovc", "copy",
"-nosound",
"-mc", "0",
"-noskip",
"-of", "rawvideo",
"-o", ffVideoPipe.getInputPipe()
};
InputFile newInput = new InputFile();
newInput.setFilename(fileName);
newInput.setPush(params.stdin);
if (media != null) {
boolean compat = (media.isVideoPS3Compatible(newInput) || !params.mediaRenderer.isH264Level41Limited());
if (!compat && params.mediaRenderer.isPS3()) {
LOGGER.info("The video will not play or will show a black screen on the PS3...");
}
if (media.getH264AnnexB() != null && media.getH264AnnexB().length > 0) {
StreamModifier sm = new StreamModifier();
sm.setHeader(media.getH264AnnexB());
sm.setH264AnnexB(true);
ffVideoPipe.setModifier(sm);
}
}
OutputParams ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffparams.stdin = params.stdin;
ffVideo = new ProcessWrapperImpl(ffmpegLPCMextract, ffparams);
int numAudioTracks = 1;
if (media.getAudioTracksList() != null && media.getAudioTracksList().size() > 1 && configuration.isMuxAllAudioTracks()) {
numAudioTracks = media.getAudioTracksList().size();
}
boolean singleMediaAudio = media.getAudioTracksList().size() <= 1;
if (params.aid != null) {
boolean ac3Remux;
boolean dtsRemux;
boolean pcm;
// Disable LPCM transcoding for MP4 container with non-H264 video as workaround for MEncoder's A/V sync bug
boolean mp4_with_non_h264 = (media.getContainer().equals("mp4") && !media.getCodecV().equals("h264"));
if (numAudioTracks <= 1) {
ffAudioPipe = new PipeIPCProcess[numAudioTracks];
ffAudioPipe[0] = new PipeIPCProcess(System.currentTimeMillis() + "ffmpegaudio01", System.currentTimeMillis() + "audioout", false, true);
/*
Disable AC-3 remux for stereo tracks with 384 kbits bitrate and PS3 renderer (PS3 FW bug?)
TODO check new firmwares
Commented out until we can find a way to detect when a video has an audio track that switches from 2 to 6 channels
because MEncoder can't handle those files, which are very common these days.
*/
// final boolean ps3_and_stereo_and_384_kbits = (params.mediaRenderer.isPS3() && params.aid.getAudioProperties().getNumberOfChannels() == 2)
// && (params.aid.getBitRate() > 370000 && params.aid.getBitRate() < 400000);
final boolean ps3_and_stereo_and_384_kbits = false;
ac3Remux = (params.aid.isAC3() && !ps3_and_stereo_and_384_kbits && configuration.isRemuxAC3());
dtsRemux = configuration.isDTSEmbedInPCM() && params.aid.isDTS() && params.mediaRenderer.isDTSPlayable();
pcm = configuration.isMencoderUsePcm() &&
!mp4_with_non_h264 &&
(
params.aid.isLossless() ||
(params.aid.isDTS() && params.aid.getAudioProperties().getNumberOfChannels() <= 6) ||
params.aid.isTrueHD() ||
(
!configuration.isMencoderUsePcmForHQAudioOnly() &&
(
params.aid.isAC3() ||
params.aid.isMP3() ||
params.aid.isAAC() ||
params.aid.isVorbis() ||
// params.aid.isWMA() ||
params.aid.isMpegAudio()
)
)
) && params.mediaRenderer.isLPCMPlayable();
int channels;
if (ac3Remux) {
channels = params.aid.getAudioProperties().getNumberOfChannels(); // AC-3 remux
} else if (dtsRemux) {
channels = 2;
} else if (pcm) {
channels = params.aid.getAudioProperties().getNumberOfChannels();
} else {
channels = configuration.getAudioChannelCount(); // 5.1 max for AC-3 encoding
}
if (!ac3Remux && (dtsRemux || pcm)) {
// DTS remux or LPCM
StreamModifier sm = new StreamModifier();
sm.setPcm(pcm);
sm.setDtsEmbed(dtsRemux);
sm.setNbChannels(channels);
sm.setSampleFrequency(params.aid.getSampleRate() < 48000 ? 48000 : params.aid.getSampleRate());
sm.setBitsPerSample(16);
String mixer = null;
if (pcm && !dtsRemux) {
mixer = getLPCMChannelMappingForMencoder(params.aid);
}
ffmpegLPCMextract = new String[] {
mencoderPath,
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
params.stdin != null ? "-" : fileName,
evoValue1, evoValue2,
"-really-quiet",
"-msglevel", "statusline=2",
"-channels", "" + sm.getNbChannels(),
"-ovc", "copy",
"-of", "rawaudio",
"-mc", sm.isDtsEmbed() ? "0.1" : "0",
"-noskip",
"-oac", sm.isDtsEmbed() ? "copy" : "pcm",
isNotBlank(mixer) ? "-af" : "-quiet", isNotBlank(mixer) ? mixer : "-quiet",
singleMediaAudio ? "-quiet" : "-aid", singleMediaAudio ? "-quiet" : ("" + params.aid.getId()),
"-srate", "48000",
"-o", ffAudioPipe[0].getInputPipe()
};
// Use PCM trick when media renderer does not support DTS in MPEG
if (!params.mediaRenderer.isMuxDTSToMpeg()) {
ffAudioPipe[0].setModifier(sm);
}
} else {
// AC-3 remux or encoding
ffmpegLPCMextract = new String[] {
mencoderPath,
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
params.stdin != null ? "-" : fileName,
evoValue1, evoValue2,
"-really-quiet",
"-msglevel", "statusline=2",
"-channels", "" + channels,
"-ovc", "copy",
"-of", "rawaudio",
"-mc", "0",
"-noskip",
"-oac", (ac3Remux) ? "copy" : "lavc",
params.aid.isAC3() ? "-fafmttag" : "-quiet", params.aid.isAC3() ? "0x2000" : "-quiet",
"-lavcopts", "acodec=" + (configuration.isMencoderAc3Fixed() ? "ac3_fixed" : "ac3") + ":abitrate=" + CodecUtil.getAC3Bitrate(configuration, params.aid),
"-af", "lavcresample=48000",
"-srate", "48000",
singleMediaAudio ? "-quiet" : "-aid", singleMediaAudio ? "-quiet" : ("" + params.aid.getId()),
"-o", ffAudioPipe[0].getInputPipe()
};
}
ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffparams.stdin = params.stdin;
ffAudio = new ProcessWrapperImpl[numAudioTracks];
ffAudio[0] = new ProcessWrapperImpl(ffmpegLPCMextract, ffparams);
} else {
ffAudioPipe = new PipeIPCProcess[numAudioTracks];
ffAudio = new ProcessWrapperImpl[numAudioTracks];
for (int i = 0; i < media.getAudioTracksList().size(); i++) {
DLNAMediaAudio audio = media.getAudioTracksList().get(i);
ffAudioPipe[i] = new PipeIPCProcess(System.currentTimeMillis() + "ffmpeg" + i, System.currentTimeMillis() + "audioout" + i, false, true);
/*
Disable AC-3 remux for stereo tracks with 384 kbits bitrate and PS3 renderer (PS3 FW bug?)
TODO check new firmwares
Commented out until we can find a way to detect when a video has an audio track that switches from 2 to 6 channels
because MEncoder can't handle those files, which are very common these days.
*/
// final boolean ps3_and_stereo_and_384_kbits = (params.mediaRenderer.isPS3() && audio.getAudioProperties().getNumberOfChannels() == 2)
// && (audio.getBitRate() > 370000 && audio.getBitRate() < 400000);
final boolean ps3_and_stereo_and_384_kbits = false;
ac3Remux = audio.isAC3() && !ps3_and_stereo_and_384_kbits && configuration.isRemuxAC3();
dtsRemux = configuration.isDTSEmbedInPCM() && audio.isDTS() && params.mediaRenderer.isDTSPlayable();
pcm = configuration.isMencoderUsePcm() &&
!mp4_with_non_h264 &&
(
audio.isLossless() ||
(audio.isDTS() && audio.getAudioProperties().getNumberOfChannels() <= 6) ||
audio.isTrueHD() ||
(
!configuration.isMencoderUsePcmForHQAudioOnly() &&
(
audio.isAC3() ||
audio.isMP3() ||
audio.isAAC() ||
audio.isVorbis() ||
// audio.isWMA() ||
audio.isMpegAudio()
)
)
) && params.mediaRenderer.isLPCMPlayable();
int channels;
if (ac3Remux) {
channels = audio.getAudioProperties().getNumberOfChannels(); // AC-3 remux
} else if (dtsRemux) {
channels = 2;
} else if (pcm) {
channels = audio.getAudioProperties().getNumberOfChannels();
} else {
channels = configuration.getAudioChannelCount(); // 5.1 max for AC-3 encoding
}
if (!ac3Remux && (dtsRemux || pcm)) {
// DTS remux or LPCM
StreamModifier sm = new StreamModifier();
sm.setPcm(pcm);
sm.setDtsEmbed(dtsRemux);
sm.setNbChannels(channels);
sm.setSampleFrequency(audio.getSampleRate() < 48000 ? 48000 : audio.getSampleRate());
sm.setBitsPerSample(16);
if (!params.mediaRenderer.isMuxDTSToMpeg()) {
ffAudioPipe[i].setModifier(sm);
}
String mixer = null;
if (pcm && !dtsRemux) {
mixer = getLPCMChannelMappingForMencoder(audio);
}
ffmpegLPCMextract = new String[]{
mencoderPath,
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
params.stdin != null ? "-" : fileName,
evoValue1, evoValue2,
"-really-quiet",
"-msglevel", "statusline=2",
"-channels", "" + sm.getNbChannels(),
"-ovc", "copy",
"-of", "rawaudio",
"-mc", sm.isDtsEmbed() ? "0.1" : "0",
"-noskip",
"-oac", sm.isDtsEmbed() ? "copy" : "pcm",
isNotBlank(mixer) ? "-af" : "-quiet", isNotBlank(mixer) ? mixer : "-quiet",
singleMediaAudio ? "-quiet" : "-aid", singleMediaAudio ? "-quiet" : ("" + audio.getId()),
"-srate", "48000",
"-o", ffAudioPipe[i].getInputPipe()
};
} else {
// AC-3 remux or encoding
ffmpegLPCMextract = new String[]{
mencoderPath,
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
params.stdin != null ? "-" : fileName,
evoValue1, evoValue2,
"-really-quiet",
"-msglevel", "statusline=2",
"-channels", "" + channels,
"-ovc", "copy",
"-of", "rawaudio",
"-mc", "0",
"-noskip",
"-oac", (ac3Remux) ? "copy" : "lavc",
audio.isAC3() ? "-fafmttag" : "-quiet", audio.isAC3() ? "0x2000" : "-quiet",
"-lavcopts", "acodec=" + (configuration.isMencoderAc3Fixed() ? "ac3_fixed" : "ac3") + ":abitrate=" + CodecUtil.getAC3Bitrate(configuration, audio),
"-af", "lavcresample=48000",
"-srate", "48000",
singleMediaAudio ? "-quiet" : "-aid", singleMediaAudio ? "-quiet" : ("" + audio.getId()),
"-o", ffAudioPipe[i].getInputPipe()
};
}
ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffparams.stdin = params.stdin;
ffAudio[i] = new ProcessWrapperImpl(ffmpegLPCMextract, ffparams);
}
}
}
}
File f = new File(configuration.getTempFolder(), "pms-tsmuxer.meta");
params.log = false;
PrintWriter pw = new PrintWriter(f);
pw.print("MUXOPT --no-pcr-on-video-pid");
pw.print(" --new-audio-pes");
pw.print(" --no-asyncio");
pw.print(" --vbr");
pw.println(" --vbv-len=500");
String videoparams = "level=4.1, insertSEI, contSPS, track=1";
if (this instanceof TsMuxerAudio) {
videoparams = "track=224";
}
if (configuration.isFix25FPSAvMismatch()) {
fps = "25";
}
pw.println(videoType + ", \"" + ffVideoPipe.getOutputPipe() + "\", " + (fps != null ? ("fps=" + fps + ", ") : "") + (width != -1 ? ("video-width=" + width + ", ") : "") + (height != -1 ? ("video-height=" + height + ", ") : "") + videoparams);
// Disable LPCM transcoding for MP4 container with non-H264 video as workaround for mencoder's A/V sync bug
boolean mp4_with_non_h264 = (media.getContainer().equals("mp4") && !media.getCodecV().equals("h264"));
if (ffAudioPipe != null && ffAudioPipe.length == 1) {
String timeshift = "";
boolean ac3Remux;
boolean dtsRemux;
boolean pcm;
/*
Disable AC-3 remux for stereo tracks with 384 kbits bitrate and PS3 renderer (PS3 FW bug?)
TODO check new firmwares
Commented out until we can find a way to detect when a video has an audio track that switches from 2 to 6 channels
because MEncoder can't handle those files, which are very common these days.
*/
// final boolean ps3_and_stereo_and_384_kbits = (params.mediaRenderer.isPS3() && params.aid.getAudioProperties().getNumberOfChannels() == 2)
// && (params.aid.getBitRate() > 370000 && params.aid.getBitRate() < 400000);
final boolean ps3_and_stereo_and_384_kbits = false;
ac3Remux = params.aid.isAC3() && !ps3_and_stereo_and_384_kbits && configuration.isRemuxAC3();
dtsRemux = configuration.isDTSEmbedInPCM() && params.aid.isDTS() && params.mediaRenderer.isDTSPlayable();
pcm = configuration.isMencoderUsePcm() &&
!mp4_with_non_h264 &&
(
params.aid.isLossless() ||
(params.aid.isDTS() && params.aid.getAudioProperties().getNumberOfChannels() <= 6) ||
params.aid.isTrueHD() ||
(
!configuration.isMencoderUsePcmForHQAudioOnly() &&
(
params.aid.isAC3() ||
params.aid.isMP3() ||
params.aid.isAAC() ||
params.aid.isVorbis() ||
// params.aid.isWMA() ||
params.aid.isMpegAudio()
)
)
) && params.mediaRenderer.isLPCMPlayable();
String type = "A_AC3";
if (ac3Remux) {
// AC-3 remux takes priority
type = "A_AC3";
} else {
if ( pcm || this instanceof TsMuxerAudio )
{
type = "A_LPCM";
}
if ( dtsRemux || this instanceof TsMuxerAudio )
{
type = "A_LPCM";
if (params.mediaRenderer.isMuxDTSToMpeg()) {
type = "A_DTS";
}
}
}
if (params.aid != null && params.aid.getAudioProperties().getAudioDelay() != 0 && params.timeseek == 0) {
timeshift = "timeshift=" + params.aid.getAudioProperties().getAudioDelay() + "ms, ";
}
pw.println(type + ", \"" + ffAudioPipe[0].getOutputPipe() + "\", " + timeshift + "track=2");
} else if (ffAudioPipe != null) {
for (int i = 0; i < media.getAudioTracksList().size(); i++) {
DLNAMediaAudio lang = media.getAudioTracksList().get(i);
String timeshift = "";
boolean ac3Remux;
boolean dtsRemux;
boolean pcm;
/*
Disable AC-3 remux for stereo tracks with 384 kbits bitrate and PS3 renderer (PS3 FW bug?)
TODO check new firmwares
Commented out until we can find a way to detect when a video has an audio track that switches from 2 to 6 channels
because MEncoder can't handle those files, which are very common these days.
*/
// final boolean ps3_and_stereo_and_384_kbits = (params.mediaRenderer.isPS3() && lang.getAudioProperties().getNumberOfChannels() == 2)
// && (lang.getBitRate() > 370000 && lang.getBitRate() < 400000);
final boolean ps3_and_stereo_and_384_kbits = false;
ac3Remux = lang.isAC3() && !ps3_and_stereo_and_384_kbits && configuration.isRemuxAC3();
dtsRemux = configuration.isDTSEmbedInPCM() && lang.isDTS() && params.mediaRenderer.isDTSPlayable();
pcm = configuration.isMencoderUsePcm() &&
!mp4_with_non_h264 &&
(
lang.isLossless() ||
(lang.isDTS() && lang.getAudioProperties().getNumberOfChannels() <= 6) ||
lang.isTrueHD() ||
(
!configuration.isMencoderUsePcmForHQAudioOnly() &&
(
params.aid.isAC3() ||
params.aid.isMP3() ||
params.aid.isAAC() ||
params.aid.isVorbis() ||
// params.aid.isWMA() ||
params.aid.isMpegAudio()
)
)
) && params.mediaRenderer.isLPCMPlayable();
String type = "A_AC3";
if (ac3Remux) {
// AC-3 remux takes priority
type = "A_AC3";
} else {
if (pcm)
{
type = "A_LPCM";
}
if (dtsRemux)
{
type = "A_LPCM";
if (params.mediaRenderer.isMuxDTSToMpeg()) {
type = "A_DTS";
}
}
}
if (lang.getAudioProperties().getAudioDelay() != 0 && params.timeseek == 0) {
timeshift = "timeshift=" + lang.getAudioProperties().getAudioDelay() + "ms, ";
}
pw.println(type + ", \"" + ffAudioPipe[i].getOutputPipe() + "\", " + timeshift + "track=" + (2 + i));
}
}
pw.close();
PipeProcess tsPipe = new PipeProcess(System.currentTimeMillis() + "tsmuxerout.ts");
String[] cmdArray = new String[]{
executable(),
f.getAbsolutePath(),
tsPipe.getInputPipe()
};
cmdArray = finalizeTranscoderArgs(
fileName,
dlna,
media,
params,
cmdArray
);
ProcessWrapperImpl p = new ProcessWrapperImpl(cmdArray, params);
params.maxBufferSize = 100;
params.input_pipes[0] = tsPipe;
params.stdin = null;
ProcessWrapper pipe_process = tsPipe.getPipeProcess();
p.attachProcess(pipe_process);
pipe_process.runInNewThread();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
tsPipe.deleteLater();
ProcessWrapper ff_pipe_process = ffVideoPipe.getPipeProcess();
p.attachProcess(ff_pipe_process);
ff_pipe_process.runInNewThread();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
ffVideoPipe.deleteLater();
p.attachProcess(ffVideo);
ffVideo.runInNewThread();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
if (ffAudioPipe != null && params.aid != null) {
for (int i = 0; i < ffAudioPipe.length; i++) {
ff_pipe_process = ffAudioPipe[i].getPipeProcess();
p.attachProcess(ff_pipe_process);
ff_pipe_process.runInNewThread();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
ffAudioPipe[i].deleteLater();
p.attachProcess(ffAudio[i]);
ffAudio[i].runInNewThread();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
p.runInNewThread();
return p;
}
/**
* Write the resource "/resources/images/fake.jpg" to a physical file on disk.
*
* @return The filename of the file on disk.
*/
private String writeResourceToFile(String resourceName) {
String outputFileName = resourceName.substring(resourceName.lastIndexOf("/") + 1);
try {
outputFileName = configuration.getTempFolder() + "/" + outputFileName;
} catch (IOException e) {
LOGGER.warn("Failure to determine temporary folder.", e);
}
File outputFile = new File(outputFileName);
// Copy the resource file only once
if (!outputFile.exists()) {
final URL resourceUrl = getClass().getClassLoader().getResource(resourceName);
byte[] buffer = new byte[1024];
int byteCount;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = resourceUrl.openStream();
outputStream = new FileOutputStream(outputFileName);
while ((byteCount = inputStream.read(buffer)) >= 0) {
outputStream.write(buffer, 0, byteCount);
}
} catch (final IOException e) {
LOGGER.error("Failure on saving the embedded resource " + resourceName + " to the file " + outputFile.getAbsolutePath(), e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (final IOException e) {
LOGGER.warn("Problem closing an input stream while reading data from the embedded resource " + resourceName, e);
}
}
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
} catch (final IOException e) {
LOGGER.warn("Problem closing the output stream while writing the file " + outputFile.getAbsolutePath(), e);
}
}
}
}
return outputFileName;
}
@Override
public String mimeType() {
return "video/mpeg";
}
@Override
public String name() {
return "tsMuxeR";
}
@Override
public int type() {
return Format.VIDEO;
}
private JCheckBox tsmuxerforcefps;
private JCheckBox muxallaudiotracks;
@Override
public JComponent config() {
// Apply the orientation for the locale
Locale locale = new Locale(configuration.getLanguage());
ComponentOrientation orientation = ComponentOrientation.getOrientation(locale);
String colSpec = FormLayoutUtil.getColSpec(COL_SPEC, orientation);
FormLayout layout = new FormLayout(colSpec, ROW_SPEC);
PanelBuilder builder = new PanelBuilder(layout);
builder.setBorder(Borders.EMPTY_BORDER);
builder.setOpaque(false);
CellConstraints cc = new CellConstraints();
JComponent cmp = builder.addSeparator(Messages.getString("TSMuxerVideo.3"), FormLayoutUtil.flip(cc.xyw(2, 1, 1), colSpec, orientation));
cmp = (JComponent) cmp.getComponent(0);
cmp.setFont(cmp.getFont().deriveFont(Font.BOLD));
tsmuxerforcefps = new JCheckBox(Messages.getString("TSMuxerVideo.2"));
tsmuxerforcefps.setContentAreaFilled(false);
if (configuration.isTsmuxerForceFps()) {
tsmuxerforcefps.setSelected(true);
}
tsmuxerforcefps.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
configuration.setTsmuxerForceFps(e.getStateChange() == ItemEvent.SELECTED);
}
});
builder.add(tsmuxerforcefps, FormLayoutUtil.flip(cc.xy(2, 3), colSpec, orientation));
muxallaudiotracks = new JCheckBox(Messages.getString("TSMuxerVideo.19"));
muxallaudiotracks.setContentAreaFilled(false);
if (configuration.isMuxAllAudioTracks()) {
muxallaudiotracks.setSelected(true);
}
muxallaudiotracks.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
configuration.setMuxAllAudioTracks(e.getStateChange() == ItemEvent.SELECTED);
}
});
builder.add(muxallaudiotracks, FormLayoutUtil.flip(cc.xy(2, 5), colSpec, orientation));
JPanel panel = builder.getPanel();
// Apply the orientation to the panel and all components in it
panel.applyComponentOrientation(orientation);
return panel;
}
@Override
public boolean isInternalSubtitlesSupported() {
return false;
}
@Override
public boolean isExternalSubtitlesSupported() {
return false;
}
@Override
public boolean isPlayerCompatible(RendererConfiguration mediaRenderer) {
return mediaRenderer != null && mediaRenderer.isMuxH264MpegTS();
}
/**
* {@inheritDoc}
*/
@Override
public boolean isCompatible(DLNAResource dlna) {
if (!(
PlayerUtil.isVideo(dlna, Format.Identifier.MKV) ||
PlayerUtil.isVideo(dlna, Format.Identifier.MPG)
)) {
return false;
}
/*
* Notes:
*
* 1) isCompatible is used for two separate tasks: 1) selection
* of #--TRANSCODE--# folder entries (PlayerFactory.getPlayers)
* and 2) assignment of the default player for a resource
* (PlayerFactory.getPlayer)
*
* 2) for resources that aren't in the #--TRANSCODE--# folder, it's understood
* that tsMuxeR will only stream the first audio track and that it won't/can't
* stream subtitles, so no need to check either
*/
if (dlna.isNoName()) { // #--TRANSCODE--# folder entry
DLNAMediaAudio dlnaMediaAudio = dlna.getMediaAudio();
// media is non-null and audioTrackList is non-empty by definition if
// mediaAudio has been set (see FileTranscodeVirtualFolder)
if ((dlnaMediaAudio != null) && !dlnaMediaAudio.toString().equals(dlna.getMedia().getFirstAudioTrack().toString())) {
// PMS only supports playback of the first audio track for tsMuxeR
return false;
}
// XXX why is tsMuxeR compatible if the subtitle language is null?
if ((dlna.getMediaSubtitle() != null) && (dlna.getMediaSubtitle().getLang() != null)) {
return false;
}
}
return true;
}
}