Move encode file on unpublish [FFMPEG]

Hello guys,

I have a live-record application that receives streams from a web SWF widget and a mobile application.

After creating a SWF that pushes H264 encoded video to wowza everything was going smooth until the mobile developers demanded that I convert the recorded videos from VP6 to H264 with a mp4 container.

Having used ffmpeg in several projects before I created the following command to convert flv to h264 mp4 :

ffmpeg -i src.flv -acodec libfaac -ab 128k -ar 44100 -ac 1 -vcodec h264  dest.mp4

I have this onUnpublish callback :

class StreamListener implements IMediaStreamActionNotify2 {
public void onUnPublish(IMediaStream stream, String streamName,	boolean isRecord, boolean isAppend) {
			stream.stopPublishing();
if(stream.getPublishVideoCodecId()==2){
				File f =new File("/usr/local/WowzaMediaServer/content/"+streamName+".flv");
				getLogger().info("file size "+f.length());
				ffmpegFLV(streamName); // 
			}
			
	private void ffmpegFLV(String streamName){
		String command = "ffmpeg -i /usr/local/WowzaMediaServer/content/"+streamName+".flv -vf \"crop=((in_w/2)*2):((in_h/2)*2)\" -acodec libfaac -ab 128k -ar 44100 -ac 1 -vcodec h264  /usr/local/WowzaMediaServer/content/"+streamName+".mp4";
		exec(command,true);
	}

The Exec function

	public static Process exec(String command, boolean wait) {
		Process p;
		try {
			p = Runtime.getRuntime().exec(command);
			
			java.io.InputStream stdin = p.getInputStream();
            InputStreamReader isr = new InputStreamReader(stdin);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            System.out.println("<OUTPUT>");
            while ( (line = br.readLine()) != null)
                System.out.println(line);
            System.out.println("</OUTPUT>");
		} catch (IOException e) {
			return null;
		}
		if (wait) {
			try {
				p.waitFor();
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
		}
		try {
			p.getInputStream().close();
			p.getOutputStream().close();
			p.getErrorStream().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}

The following works fine:

The same ffmpeg command in the shell after disconnect.

Is the execution of the ffmpeg command somehow interrupted, the destination file created as an empty file with a size of 0.

Should i move this to a better place where wowza would be done in saving the data ?

Additional info:

At the same code i have succeeded in moving the file into another directory which confuses me because the ffmpeg command is basically working and i was able to move the file at this point but whenever i try to convert the video the ffmpeg creates an empty file.

Thanks for the help.

It might be too soon to do that in IMediaStreamActionNotify2.onUnPublish. Try this interface instead.

https://www.wowza.com/docs/how-to-use-imediawriteractionnotify-to-programmatically-move-and-rename-recordings-of-live-streams

Also, you can publish h.264 video and Speex audio from a Flash application. Take a look at the Flash example here:

https://www.wowza.com/docs/how-to-convert-flash-player-11-output-from-h-264-speex-audio-to-h-264-aac-audio-using-wowza-transcoder

Richard

You can write h.264 and Speex to a .mp4 container for Flash RTMP and Flash HTTP playback, and works in some iOS devices too (in my test with IPhone 4gs iOS 6.1). So you might not need the Transcoder. In any case it would be better to start with h.264/Speex instead of VP6/NellyMoser when you transcode with FFmpeg

Richard

also, to record to .mp4 container, add the “mp4:” prefix to the stream name that you are publishing in Flash. If you publish “mp4:myStream” it will be recorded to myStream.mp4

Richard

Thank you Richard for the quick reply I will try the IMediaWriterActionNotify and get back to you ASAP.

Concerning the transcoder Add-on I was able to successfully achieve what I wanted but since the license is billed per connection it is very hard for us to commit to such a license :frowning:

Thanks:D

In the SWF embedded in the web browsers I use “flash.media.H264” to publish h.264/Speex video to a .mp4 container using the “mp4:” prefix to the stream name and it has been working like charm for now, but in the case of the mobile developers; android to be more specific the produced stream is VP6/NellyMoser :frowning:

So i am stuck with finding a solution for this problem on the server’s side.

On this subject and by your experience is it possible to publish a “h.264/Speex” stream on an android device and is there any possible way that you can help us with this matter ?

Thanks again for all the support :slight_smile:

I have tried the “mp4:” prefix in the case of the mobile i remember getting the following error:

MediaWriterH264: Unsupported video format for MP4 container: CODEC_VIDEO_SPARK