A few helpful hints

Some phone calls last week have highlighted that one or two helpful hints are scattered around various articles so I have wrapped a few up into one!

Many video files are exported from surveillance systems unwrapped. This may be fine if they are also supplied with a dedicated player, but can cause some difficulty if they are on their own. If the files are an understandable stream type, conforming to a video industry standard, then it makes the work of managing them much easier. The examples below use a file type where the date/time information is hard burnt into the video at the time of creation.

I am going to look at a number of functions.

Quick Playback, Quick Transcode to Video DVD, I frame Extract and then finally creating a self playing video.

Example; two files on a disk……. file1.dav and file2.dav (Very popular extension holding either mpeg4 or H264 streams). There are many different players designed, and rebranded, that often get packaged with files like the ones mentioned here. Some of them also have a ‘convert to avi’ function. For today though we are not going to play around with those. Be aware though that sometimes a player designed specifically for an export type will give you some hidden functionality so if it’s a new file type, it’s worth fully examining all playback options.

The first job then is to check playback.

Open up a command window inside the ffmpeg bin folder. Full details on using command tools and a few hints here

Type ffplay and then drag one of your files in. Hit enter

Capture

Hopefully it will play in a basic window, although it may not be playing at the correct speed. If it doesn’t play first time, you may need to force the input by adding -f h264 or -f m4v before you drag your file into the command. This forces it to be read in either of the stated formats. The command would look something like this:

ffplay -f h264 C:/folder/video/file1.dav

If you have got some playback you may then need to create a report on what your file is.

Instead of ffplay, this time we are going to use ffprobe.

Type ffprobe -show_format -show_streams -count_frames -pretty and then drag your file in then type space > space and then drag the file again. Change the extension from what you had to .txt

Your command should look something like this:

ffprobe -show_format -show_streams -count_frames -pretty C:/folder/video/file1.dav > C:/folder/video/file1.txt

When you hit enter, a txt file will be created alongside your .dav file looking a bit like this:

[STREAM]
index=0
codec_name=h264
codec_long_name=H.264/AVC/MPEG-4 AVC/MPEG-4 part 10
profile=Constrained Baseline
codec_type=video
codec_time_base=1/50
codec_tag_string=[0][0][0][0]
codec_tag=0x0000
width=352
height=288
has_b_frames=1
sample_aspect_ratio=0:1
display_aspect_ratio=0:1
pix_fmt=yuv420p
level=20
timecode=N/A
id=N/A
r_frame_rate=50/2
avg_frame_rate=25/1
time_base=1/1200000
start_pts=N/A
start_time=N/A
duration_ts=N/A
duration=N/A
bit_rate=N/A
nb_frames=N/A
nb_read_frames=74904
nb_read_packets=N/A
[/STREAM]
[FORMAT]
filename=C:\folder\video\file1.txt
nb_streams=1
nb_programs=0
format_name=h264
format_long_name=raw H.264 video
start_time=N/A
duration=N/A
size=179.208008 Mibyte
bit_rate=N/A
probe_score=51
[/FORMAT]

I could do that for all files if required. Once the first one is done, just hit the up arrow to go back to the last command and change the file names to fit each new file.

OK, now we know we can play and understand our files. It’s now time to use ffmpeg.

If we want to wrap them individually into an .avi container we use:

ffmpeg -i C:/folder/video/file1.dav -vcodec copy -fflags genpts -f avi C:/folder/video/file1.avi

If we had a large amount of files and wanted to rewrap them all individually, then creating a batch preset in something like anotherGUI is a lifesaver.

We will then end up with the h264 video stream, indexed and placed inside an avi container. It will now open with any player that can understand the h264 format. If your file required the -f h264 when using the ffplay command, you will need to use this again here. Remember it goes before the -i

We had two .dav files to start with. From previewing them in ffplay I saw that they were 1 stream, split into 2 files. This was also suggested by the naming convention.

We could now join the two .dav files into one .avi file. I have written a few posts on the concatenating of files (use the search bar at the top for concat). For this example though, we need to place both h264 streams into one DVD compliant MPEG2.

Disclaimer: I am not going to get into the reasons why you may just want a DVD copy and I am not going to get into the loss in quality  / image integrity argument! Lets just assume you need the footage on DVD!

Script workflow would then be something like this….

Use ffmpeg to join all files, forcing them to be read as h264, and then encode directly to high quality video DVD compatible mpeg2 file.

ffmpeg -f h264 -i “concat:C:/folder/video/file1.dav|C:/folder/video/file2.dav” -s 720×576 -aspect 4:3 -c:v mpeg2video -b:v  6000k -minrate 4000k -maxrate 6000k -bufsize 2000k -dc 9 -flags +ilme+ildct -alternate_scan 1 -top 1  C:/folder/video/allfiles.mpg

Within 10 mins we have gone from having a split raw h264 file, to a standard 1hr 20min MPEG2, ready for authoring to Video DVD. It does not require further transcoding as its already compliant.

I could have increased the bitrate a fair amount but upon a visual review, the quality was acceptable. Lets take a quick look at all the components in the ffmpeg command.

-f h264 = Force input to be read as a h264 format

-i “concat:C:/folder/video/file1.dav|C:/folder/video/file2.dav” = Concatenate the following list of files and use the joined up file as the input file.

-s 720×576 = Video Size PAL Resolution

-aspect 4:3 = Set Aspect Ratio Flag

-c:v mpeg2video = Video Codec to be used for encoding (To see a list of possible codecs use ffmpeg -formats)

-b:v  6000k -minrate 4000k -maxrate 6000k = Sets a variable bitrate between 4000-6000k

-bufsize 2000k = Encoding buffer size

-dc 9 = The Intra DC Precision. MPEG 2 can use the values of 8,9,10 or 11. The higher the value the more precise quantization is achieved.

-flags +ilme+ildct -alternate_scan 1 -top 1 = Interlace the output using Top Field First

Lastly, the output has the .mpg file extension.

After producing the MPEG2 file, its over to the DVD authoring program of choice to create the Video DVD. The whole process, from start to finish takes less than half an hour. Not bad considering its 1hr 20min of footage.

Whilst we are using ffmpeg, lets look back at I frame only extraction. There are many reasons why you may want to just grab the I frames from a video stream, but one of the most popular is to then use a selection of them to produce a composite and remove the noise produced during the compression.

So, to extract all the I frames from our video:

ffmpeg -f h264 -i C:/folder/video/file1.dav -vf select=’eq(pict_type\,I)’ -vsync drop -f image2 -pix_fmt rgb24 C:/folder/video/framesfolder/frame%05d.tiff

This then only encodes the I frames and puts them into the image format of TIFF.

Now that we have our folder of I frames, all or a group can be used to create the composite. Its over to Photoshop for that bit.

Single I frame at Top compared to the frame averaged image using many. 'Zoomed' in to highlight noise reduction.

Single I frame at Top compared to the frame averaged image using many. ‘Zoomed’ in to highlight noise reduction.

Lastly then, what if you wanted to provide easy playback for the original video files but didn’t need or want to encode to Video DVD.

A handy utility, that can deal very nicely with h264 / Mpeg4 etc is MakeInstantPlayer

Rewrapping the two h264 .dav files into one avi file with no transcoding takes less than a minute. As a reminder, the command would be:

ffmpeg -f h264 -i “concat:C:/folder/video/file1.dav|C:/folder/video/file2.dav” -vcodec copy -fflags genpts -f avi C:/folder/video/allfiles.avi 

Its not really necessary to use the avi container for this, you could use another that may be more suitable for holding h264 streams. Anyway, now that we have are new full file, we can drop it into the GUI.

The GUI for Make Instant Player

The GUI for Make Instant Player

The nice thing about this is that you can add your own splash screen and icon.

The defaults not that bad though..

Snap013

When this is selected the executable will unwrap the enclosed light version of Mplayer and then automatically start playing your video.

Snap012

If you wanted to use these often, and require them to be on disk, you could also create an autorun.inf. This file will be read by the Windows Autorun functionality and then open the directed .exe file. See the instructions HERE. If you always named the player ‘FilePlayer.exe’ you could save this autorun and just drop it into your burn to disk folder when required!

That’s it for now, I hope some of the details here come in handy.

Advertisements
By Spreadys Posted in EEPIP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s