001/******************************************************************************* 002 * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved. 003 * 004 * This file is part of AVPKit. 005 * 006 * AVPKit is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU Lesser General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * AVPKit is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with AVPKit. If not, see <http://www.gnu.org/licenses/>. 018 *******************************************************************************/ 019 020package com.avpkit.mediatool.demos; 021 022import java.io.File; 023 024 025import com.avpkit.mediatool.IMediaReader; 026import com.avpkit.mediatool.IMediaViewer; 027import com.avpkit.mediatool.IMediaWriter; 028import com.avpkit.mediatool.ToolFactory; 029 030import static java.lang.System.out; 031import static java.lang.System.exit; 032 033/** 034 * A very simple media transcoder which uses {@link IMediaReader}, {@link 035 * IMediaWriter} and {@link IMediaViewer}. 036 */ 037 038public class TranscodeAudioAndVideo 039{ 040 /** 041 * Transcodes a media file into a new media file, guessing parameters 042 * and codecs 043 * based on the file names. 044 * @param args 2 strings; an input file and an output file. 045 */ 046 public static void main(String[] args) 047 { 048 if (args.length < 2) 049 { 050 out.println("To perform a simple media transcode. The destination " + 051 "format will be guessed from the file extention."); 052 out.println(""); 053 out.println(" TranscodeAudioAndVideo <source-file> <destination-file>"); 054 out.println(""); 055 out.println( 056 "The destination type will be guess from the supplied file extsion."); 057 exit(0); 058 } 059 060 File source = new File(args[0]); 061 if (!source.exists()) 062 { 063 out.println("Source file does not exist: " + source); 064 exit(0); 065 } 066 067 transcode(args[0], args[1]); 068 } 069 070 /** 071 * Transcode a source url to a destination url. Really. That's 072 * all this does. 073 */ 074 075 public static void transcode(String sourceUrl, String destinationUrl) 076 { 077 out.printf("transcode %s -> %s\n", sourceUrl, destinationUrl); 078 079 // create the media reader, not that no BufferedImages need to be 080 // created because the video is not going to be manipulated 081 082 IMediaReader reader = ToolFactory.makeReader(sourceUrl); 083 084 // add a viewer to the reader, to see progress as the media is 085 // transcoded 086 087 reader.addListener(ToolFactory.makeViewer(true)); 088 089 // create the media writer 090 reader.addListener(ToolFactory.makeWriter(destinationUrl, reader)); 091 092 // read packets from the source file, which dispatch events to the 093 // writer, this will continue until 094 095 while (reader.readPacket() == null) 096 do {} while(false); 097 } 098}