artclarke / humble-video

Humble Video: Demuxing, Decoding, Filtering, Encoding and Muxing of 100's of video and audio formats and Codecs from the JVM
GNU Affero General Public License v3.0
557 stars 115 forks source link

I'm trying to Transcode audio streams, by porting code from xuggler #72

Open radut opened 9 years ago

radut commented 9 years ago

Hi, could you help me with an audio transcode to output stream mp3 @ any bitrate - > mp3 @ 256kbps

my code in xuggler is :

public static void convertToMP3Xuggler( File inputPath, OutputStream os, final int kbps ) { // modify on your convenience

    // create a media reader
    System.out.println( inputPath.getPath( ) );
    final IMediaReader mediaReader = ToolFactory.makeReader( inputPath.getPath( ) );

    // create a media writer
    // IMediaWriter mediaWriter = ToolFactory.makeWriter( XugglerIO.map( XugglerIO.generateUniqueName( os, ".mp3" ), os ), mediaReader );
    IMediaWriter mediaWriter = ToolFactory.makeWriter( XugglerIO.map( os ), mediaReader );
    // manually set the container format (because it can't detect it by filename anymore)
    IContainerFormat containerFormat = IContainerFormat.make( );
    containerFormat.setOutputFormat( "mp3", null, "audio/mpeg3" );
    // containerFormat.setOutputFormat( "mp4", null, "audio/x-aac" );
    mediaWriter.getContainer( ).setFormat( containerFormat );

    // add a writer to the reader, to create the output file
    mediaReader.addListener( mediaWriter );

    // add a IMediaListner to the writer to change bit rate
    mediaWriter.addListener( new MediaListenerAdapter( ) {
        @Override
        public void onAddStream( IAddStreamEvent event ) {

            IStreamCoder streamCoder = event.getSource( ).getContainer( ).getStream( event.getStreamIndex( ) ).getStreamCoder( );
            streamCoder.setFlag( IStreamCoder.Flags.FLAG_QSCALE, false );
            streamCoder.setBitRate( kbps * 1000 );
            streamCoder.setBitRateTolerance( 0 );
        }
    } );

    // read and decode packets from the source file and
    // and dispatch decoded audio and video to the writer
    System.out.println( "start packet" );
    new Thread( ) {
        @Override
        public void run() {

            while ( mediaReader.readPacket( ) == null );
            System.out.println( "end packet" );
        }
    }.start( );
}
metasonic commented 8 years ago

any luck with this?