Showing posts with label mpegts. Show all posts
Showing posts with label mpegts. Show all posts

Sunday, December 08, 2013

lossless encoding from H264 to MPEGTS

For editing purposes, I needed a good way to losslessly encode video from my Canon 5D Mark II's H264 source to a format that allows me to concatenate multiple video files into one large one.  Only a few video formats have the ability to concatenate multiple files together: mainly mpg and mpegts.  FFmpeg is always getting better (thank you, guys!), and has a nice formula for doing this here:
https://trac.ffmpeg.org/wiki/How%20to%20concatenate%20(join,%20merge)%20media%20files

The formula relies on a specification from the H264 standard known as AnnexB.  Here are a couple articles about Annex B, though both are above my head:
http://aviadr1.blogspot.com/2010/05/h264-extradata-partially-explained-for.html
http://wiki.multimedia.cx/index.php?title=H.264

The H.264 spec itself:
http://www.itu.int/rec/T-REC-H.264-201304-I

My goal was to losslessly extract the video and audio from the individual videos I shot using the Canon and concatenate them all into one long-length video.

Here's what the Canon video file container looked like to FFprobe:
ffprobe $VIDEO 2>&1 | grep Stream
    Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuvj420p(pc, smpte170m), 1920x1080, 45694 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 60k tbc (default)
    Stream #0:1(eng): Audio: pcm_s16le (sowt / 0x74776F73), 48000 Hz, stereo, s16, 1536 kb/s (default)

Note the video stream is H264 and the audio stream is waveform PCM.

Extract the Video
I did this with an FFmpeg command line:
ffmpeg -i $VIDEO -c copy -bsf:v h264_mp4toannexb -f mpegts $NEWVIDEO

Note that you'll need the latest and greatest FFmpeg.  Using an older version of FFmpeg from RPM Fusion's repo )FFmpeg 1.0.8), I got a bunch of these errors when I tried to concatenate individual MPEGTS files.
[aac @ 0x6727e0] channel element 1.10 is not allocated
[aac @ 0x6727e0] More than one AAC RDB per ADTS frame is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
[aac @ 0x6727e0] channel element 3.15 is not allocated
[aac @ 0x6727e0] Error decoding AAC frame header.
[aac @ 0x6727e0] Reserved bit set.
[aac @ 0x6727e0] channel element 1.8 is not allocated
[aac @ 0x6727e0] channel element 2.0 is not allocated
[aac @ 0x6727e0] Sample rate index in program config element does not match the sample rate index configured by the container.
[aac @ 0x6727e0] Reserved bit set.
    Last message repeated 1 times
[aac @ 0x6727e0] Number of bands (4) exceeds limit (2).
[aac @ 0x6727e0] SSR not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
[mpegts @ 0x676640] decoding for stream 1 failed
[mpegts @ 0x676640] Could not find codec parameters for stream 1 (Audio: aac ([6][0][0][0] / 0x0006), 8 channels (FL+FR+FC+LFE+BL+BR+FLC+FRC), s16, 548 kb/s): unspecified sample rate
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, mpegts, from 'concat:test.ts|test2.ts':
  Duration: 00:03:14.17, start: 1.400000, bitrate: 131495 kb/s
  Program 1 
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
    Stream #0:0[0x100]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuvj420p, 1920x1088, 29.97 tbr, 90k tbn, 180k tbc
    Stream #0:1[0x101](eng): Audio: aac ([6][0][0][0] / 0x0006), 8 channels (FL+FR+FC+LFE+BL+BR+FLC+FRC), s16, 548 kb/s
[mpegts @ 0x672c20] sample rate not set

Once I downloaded the latest FFmpeg from FFmpeg's git repo

the errors disappeared.  Hooray for updated code!

Extract the Audio
My second task was to yank the PCM audio track from the Canon and save that off to a file for concatenation later on.  I did so using this command to extract only the audio stream (-c:a) from the source video file the Canon outputs:
ffmpeg -i $VIDEO -c:a copy $NEWAUDIO

Concatenate the Individual Video Files
Next, I combined the extracted video streams that were saved to MPEGTS format into one large file.  That command looked like this:
ffmpeg -i "concat:file1.ts|file2.ts|file3.ts" -c copy $FINAL.ts

Concatenate the Individual Audio Files
I did the same for the audio:
ffmpeg -i "concat:file1.wav|file2.wav|file3.wav" -c copy $FINAL.wav

Edit to Taste
Knowing my goal was to have as close as lossless video and audio, I did some audio sweetening in Audacity and reencoded the $FINAL.wav as an MP2 audio file.  This is lossy, but
1) the MPEGTS container prefers MPEG audio codecs.
2) FFmpeg doesn't yet have an encoder for MPEG-4 ALS lossless just yet.

Combine the Long-length Video and Audio Streams
Lastly, I combined both lossless video and lossy audio streams using this command into a:
ffmpeg -i $FINAL.mp2 -i $FINAL.ts -c:a copy -c:v copy -map 0:0 -map 0:1 $COMBINED.ts

The -map feature in FFmpeg is the best thing since sliced bread:
http://ffmpeg.org/ffmpeg.html#Advanced-options

You can take an audio or video stream from one file and combine it with an audio or video stream from another file to save into a new file.  This assumes the container format supports those codecs.  Sweet!

Sync Issue Averted
I noticed that if I combined the individual audio and video streams into files that were greater than an hour and a half long, FFmpeg would introduce audio-visual sync issues towards the end of that long duration video.  My solution was to combine the audio and video streams into smaller transport stream files first and then combine those smaller, complete files into one big file.  This way, I had no a/v sync issue.

Anyway, that's my latest adventure into the world of lossless video editing using my Canon and FFmpeg.

Enjoy.
CM

Monday, January 14, 2008

MediaGate MG350HD v1.1.7 firmware

For my MediaGate MG350HD wired and wireless media player:
http://www.mediagateusa.com/html/mg350.html

I upgraded to the latest MediaGate firmware version 1.1.7. The previous version I was using was 1.1.1beta. With that version, I had been experiencing poor playback of HDV MPEG-TS videos. The video would stutter and the audio would drop out. I downloaded the upgrade from here:
http://www.mediagateusa.com/html/download/download.html

MediaGate firmware upgrades are always a pain in the ass because when the new firmware is written to the device, the configuration of the device gets reset and you need to re-enter all your configuration information. Most painful is entering the wireless security information. A long WEP key password is particularly painful because you have to enter the 20+ alphanumeric key using the remote. And, the video output settings are similarly reset, so instead of viewing the menus in HD, I have to use the separate composite connection and then reset the output type and resolution to DVI, 1080P. Ugh.

On the brighter side, once I got my wired and wireless network and display settings re-input, I was very pleased to find out that the video freezes and audio drop outs for the HDV MPEG-TS videos had been eliminated! Thanks MediaGate, for fixing those problems!

In addition, the new 1.1.7 firmware has an FTP server, so I no longer have to install the kludgy NDAS drivers.

Setup of an FTP Server with the MediaGate is easy:
  1. Start up your 1.1.7 Mediagate
  2. Press "Setup" on your remote
  3. Navigate to the "FTP Server" section of the setup
  4. Enable the FTP server
  5. If you wish to change the username and password, do so here
  6. Save your settings
  7. Verify the MediaGate is on your network. Use "ping [IP address of the MediaGate]" either wired or wirelessly) to make sure the device is on the network
  8. Test FTP access by logging in from another machine on your network. From the command line, this would entail a command such as "ftp [IP address of the MediaGate]".
  9. If you get a connection via the above command, enter your username and password. You should be good to go!
So I can now upload files directly to the MediaGate via good old FTP! Nice job, MediaGate!

One suggestion for the MediaGate people is to create a firmware upgrade that does not wipe out my configuration information. That would be greatly appreciated.
:)

Sunday, May 27, 2007

dar: a solution to archiving video

Like myself, you may have gigs and gigs of video sitting on your hard drive, taking up space that should be used for live projects or new media. And you've filled up your 500GB drive, so that you are constantly having to do piecemeal copies of older material to DVD+R. I have the same problem. But as I get older, I try to be a little wiser and actually solve my problems instead of living with them.

I will make the distinction that you should be using DVD+R for your archives. I have found +Rs to be more reliable for data archival that -Rs.

Here's the problem with doing a straight copy of media files to DVD+R. Look at the directory listing below:
[root@computer ~]# ll /mnt/videos/20050721/
total 20871112
-rwxr-xr-x 1 root root 1469679848 Oct 18 2006 1.m2t-rwxr-xr-x 1 root root 58916 Oct 21 2006 20050721b.xml-rwxr-xr-x 1 root root 3877924864 Oct 22 2006 20050721dvd.mpg-rwxr-xr-x 1 root root 1130843124 Oct 29 2006 20050721.m2t
-rwxr-xr-x 1 root root 45197 Oct 20 2006 20050721.xml
-rwxr-xr-x 1 root root 70478 Oct 21 2006 20060721c.xml
-rwxr-xr-x 1 root root 70431 Oct 24 2006 20060721d.xml
-rwxr-xr-x 1 root root 71543 Oct 24 2006 20060721e.xml
-rwxr-xr-x 1 root root 8549 Oct 25 2006 20060721small.xml
-rwxr-xr-x 1 root root 2065588524 Oct 18 2006 2.m2t
-rwxr-xr-x 1 root root 1755983168 Oct 18 2006 3.m2t
-rwxr-xr-x 1 root root 902360332 Oct 18 2006 4.m2t
-rwxr-xr-x 1 root root 1491412084 Oct 18 2006 5.m2t
-rwxr-xr-x 1 root root 2865367596 Oct 18 2006 6.m2t
-rwxr-xr-x 1 root root 5461152404 Oct 18 2006 7.m2t


Here we see the typical mess of Cinelerra project files, source material (HDV MPEGTS files), and final renders (MPEGs). Now, totaling up the space used for these file, you get about 21GB. Ugh. And given the odd file sizes, you'd end up using about 7 DVDs just to backup what you've got. This is because HDV files are huge, ranging from 1.5GB to 5GB and DVDs only hold about 4.38GB usable space on each. So you're forced to do a statistical combinations balancing act in order to fit as many files on a DVD in the most efficient manner possible. We who live in the land of video production are all living and breathing this headache. What a pain in the ass. But what is the alternative?

The alternative is to find a program that can compress and archive files over multiple DVDs, grouping these files of different sizes and compacting them together. Most importantly, the software should then evenly divide the compressed file archive across multiple DVDs in the most efficient and space conscious manner possible.

Well, lucky for us, the Linux Gods have brought down "dar" from the heavens. Dar (disk archive), available at http://dar.linux.free.fr/, is a command line backup and restore tool that can compress files using the bz2 algorithm, put files into a single archive and divide that archive into manageable chunks destined for backup media of one type or another. For the file listing you see above, dar was able to take it and turn it into this:
[root@computer ~]# ll /mnt/videos/2007-05-27_data.*
-rw-r--r-- 1 root root 4194304000 May 27 2007-05-27_data.1.dar
-rw-r--r-- 1 root root 4194304000 May 27 2007-05-27_data.2.dar
-rw-r--r-- 1 root root 4194304000 May 27 2007-05-27_data.3.dar
-rw-r--r-- 1 root root 4194304000 May 27 2007-05-27_data.4.dar
-rw-r--r-- 1 root root 3912021642 May 27 2007-05-27_data.5.dar


Nice! Easily digestible chunks for a single layer DVD to handle!

Now, the compression that dar acheived was not very much. Total file size went from 2.135GB to about 2.06GB. This is because the MPEGTS files are already compressed, so I'm not going to gain much from bz2. My 3.2Ghz, 2GB, PC3200, RAID0 (stripe set of two IDE drives), Dell 400SC took about three hours and twenty minutes to compress that 21GB. So it's not fast.

Before you get too excited, here are some known limitations of dar:
http://dar.linux.free.fr/doc/Limitations.html

I made sure to give dar a full system test using the steps below.

1) archive the above directory of files
TIME: about three hours and twenty minutes on the system described above.
dar -m 256 -v -y -s 4000M -D -R /mnt/videos/20050721/ -c `date -I`_data
Adding file to archive: /mnt/videos/20050721/20050721e.xml
Adding file to archive: /mnt/videos/20050721/addSecond.sh
..

Update 2008/12/22
If you have 120 minute, 4.7GB DVD+Rs, you can up the number of bytes in each dar to 4400MB or 4,613,734,400 or (4400 x 1024 x 1024):
dar -m 256 -v -y -s 4400M -D -R /mnt/videos/20050721/ -c `date -I`_data

Note: you made need the latest and greatest version of dvd+rw-tools for this large filesize burning to work! I tested this on Fedora 10 and I was able to store and retrieve a 25GB dar archive using this procedure.

Note that you will need to use the "-allow-limited-size" switch to growisofs when you burn these larger than normal files to dvd:
growisofs -Z /dev/dvd -R -J -allow-limited-size filename.dar
end update

In short, the switches I used mean:

-m 256   = don't compress files less than 256 bytes
-v       = verbose output showing what is being archived
-y       = activate bz2 compression
-s 4000M = create archives 4000MB in size.  4000MB is 1024x1024x4000 bytes or 4,194,304,000 bytes.
    By the way, 4GB is actually 2 to the 32 power or 4,294,967,296 bytes.
-D       = store directories excluded by the -P option or absent from the command line path list as empty directories
-R       = specify the root directory for saving or restoring files
-c       = create the archive with the following name, using the current date

Here's the output of that command:
--------------------------------------------
17 inode(s) saved
with 0 hard link(s) recorded
0 inode(s) changed at the moment of the backup
0 inode(s) not saved (no file change)
0 inode(s) failed to save (filesystem error)
0 files(s) ignored (excluded by filters)
0 files(s) recorded as deleted from reference backup
--------------------------------------------
Total number of file considered: 17
--------------------------------------------


The command line switches I used above are well summarized in this HowTo:
http://dar.linux.free.fr/doc/mini-howto/index.html

Also, for you man page readers, here's the nitty gritty:
http://www.die.net/doc/linux/man/man1/dar.1.html

2) validate that the archive is does not contain errors
TIME: about an hour and a half.
dar -t <archive name>

Here is the output of that command:
--------------------------------------------
17 file(s) treated
0 file(s) with error
0 file(s) ignored (excluded by filters)
--------------------------------------------
Total number of file considered: 17
--------------------------------------------

Also, it is helpful to list out the contents of the created dar in order to verify it matches the files you want archived. Here is sample output from another archive I created:

[root@computer ~]# dar -l 20081016_data
[data ][ EA  ][compr] | permission | user  | group | size  |          date                 |    filename
----------------------+------------+-------+-------+-------+-------------------------------+------------
[Saved]       [  90%]   -rw-r--r--   root       root    46335   Tue Oct 21 22:09:02 2008        20081016e.xml
[Saved]       [  46%]   -rwxr-xr-x   root       root    990     Sat Oct 18 16:56:45 2008        addSecond.sh
[Saved]       [   8%]   -rw-r--r--   root       root    5663820164      Sat Oct 18 09:25:36 2008        20081016_6.m2t
[Saved]       [   2%]   -rw-r--r--   root       root    26411454        Sun Oct 26 16:29:52 2008        test.mov
[Saved]       [  91%]   -rw-r--r--   root       root    55587   Mon Oct 27 08:23:02 2008        20081016i.xml
[Saved]       [  79%]   -rw-r--r--   root       root    13680   Sat Oct 18 15:41:23 2008        20081016a.xml
[Saved]       [  47%]   -rwxr-xr-x   root       root    1408    Sat Oct 18 17:50:51 2008        2songlist.sh
[Saved]       [  51%]   -rw-r--r--   root       root    1688    Wed Oct 22 08:19:37 2008        vodcastNew.xml
[Saved]       [  47%]   -rwxr-xr-x   root       root    2411    Sat Oct 18 19:13:01 2008        1encode.sh.bak
[Saved]       [   8%]   -rw-r--r--   root       root    1143234956      Sat Oct 18 09:11:36 2008        20081016_4.m2t
[Saved]       [   5%]   -rw-r--r--   root       root    3805975877      Mon Oct 27 04:49:50 2008        StormPigs20081016.m2v
[Saved]       [     ]   -rwxr-xr-x   root       root    146     Sat Oct 18 17:56:25 2008        5ftp.sh
[Saved]       [  10%]   -rw-r--r--   root       root    804122436       Sat Oct 18 09:02:08 2008        20081016_1.m2t
[Saved]       [   8%]   -rw-r--r--   root       root    2091780308      Sat Oct 18 09:05:45 2008        20081016_2.m2t
[Saved]       [  88%]   -rw-r--r--   root       root    30648   Tue Oct 21 21:07:01 2008        20081016c.xml
[Saved]       [  89%]   -rw-r--r--   root       root    40105   Tue Oct 21 21:32:46 2008        20081016d.xml
[Saved]       [  79%]   -rw-r--r--   root       root    12197   Sat Oct 18 15:13:20 2008        20081016.xml
..

3) write each output file from dar to DVD
TIME: with a 18x burner running at 16x speed to DVD+R, this takes about an hour.

First, check your media:
dvd+rw-mediainfo /dev/dvd

Then burn your archive to disk:
growisofs -Z /dev/dvd -R -J /root/2007-05-27_data.1.dar
..


If you intend to do a lot of archiving, I suggest you purchase a recent model DVD+R recorder. When I first tested dar this past weekend, I had a mess of problems reading the archive files I had burned successfully to DVD. I figured my DVD was three years old and it was time for an upgrade, so I bought the internal version of this drive, the HP DVD940E External 18x Super Multi DVD Writer for $60 with a $30 rebate from Office Depot. The thing performs like a champ!

4) copy the archive from the DVDs to disk
TIME: with an 18x burner, this takes about twenty minutes.
mount /dev/cdrom /mnt/cdrom
cp /mnt/cdrom/* /mnt/videos/


5) validate that the archive files off the DVD do not contain errors
TIME: about an hour and a half.
dar -t <archive name>

While validating my archives off DVD, I encountered one problem:
[root@computer ~]# dar -t /mnt/videos/2007-05-27_data
ERR /6.m2t : compressed data CRC error
--------------------------------------------
17 file(s) treated
1 file(s) with error
0 file(s) ignored (excluded by filters)
--------------------------------------------
Total number of file considered: 17
--------------------------------------------

Bad news. It looks like the data written to one of the DVDs is corrupt. Since I had the originals files and they tested out correct, I re-wrote the archive to new DVDs and did not encounter this problem again. By the way, the test of my 20GB archives took about an hour.

Here is what a successful validation looks like:
[root@computer ~]# dar -t 20081016_data
--------------------------------------------
17 inode(s) treated
0 inode(s) with error
0 inode(s) ignored (excluded by filters)
--------------------------------------------
Total number of inode considered: 17
--------------------------------------------


6) if no errors, restore original files and verify file sizes
TIME: about three hours.
This step is optional, if you've already run "dar -t" to verify the integrity of the archive coming off the DVD. Here is the output:
dar -x 2007-05-27_data
--------------------------------------------
17 file(s) restored
0 file(s) not restored (not saved in archive)
0 file(s) ignored (excluded by filters)
0 file(s) less recent than the one on filesystem
0 file(s) failed to restore (filesystem error)
0 file(s) deleted
--------------------------------------------
Total number of file considered: 17
--------------------------------------------


There was some slowness copying the archives back from DVD (which took about two hours at 4x speed), but that's just the speed of the DVD player. Aside from that 4GB limit, dar live up to its reputation! So I'm pretty happy.

Review
1) archive your files
TIME: about three hours and twenty minutes on the system described above.
dar -m 256 -v -y -s 4000M -D -R /mnt/videos/20050721/ -c `date -I`_data

2) validate that the archive is does not contain errors
TIME: about an hour and a half.
dar -t <archive name>

3) write each output file from dar to DVD
TIME: with a 18x burner running at 16x speed to DVD+R, this takes about an hour.
growisofs -Z /dev/dvd -R -J /root/2007-05-27_data.1.dar

4) copy the archive from the DVDs to disk
TIME: with an 18x burner, this takes about twenty minutes.
cp /mnt/cdrom/* /mnt/videos/

5) validate that the archive files off the DVD do not contain errors
TIME: about an hour and a half.
dar -t <archive name>

OPTIONAL:
6) if no errors, restore original files and verify file sizes
TIME: about three hours.
dar -x 2007-05-27_data

Summary
If you wish to use dar and want to keep your valuable video data in tact for years to come, I strongly suggest you run through steps 1-5 each time you make an archive! Of course, just the basic steps take a total of eight hours for 20GB of data. The optional step brings that total to eleven hours of your time spent.

Of course, you don't have to archive EVERYTHING. Only archive the source videos and maybe the primary intermediates. For example, I archive all my MPEG-TS files from my cam, plus the MPEG2 video and MP3 audio rendered from my project. I DON'T archive the finals: DVD format, iTunes format and MPEG program streams, as I can always reproduce those from the primary intermediates that are rendered from the project.

In the end, you have to ask yourself "How much do I value the work that I've done?"
Going through these steps everytime you make an archive may seem like a pain, but the pain will be worse if your data goes away! You could opt to store your media on a hard drive, but if that hard drive gets near a speaker or large magnet, your data could be lost. If you are going to archive this data for years, it makes more sense to do it on optical formats that are not susceptible to damage by magnetism.

If you do decide to go the dar route and follow these steps, you'll have the peace of mind that your archives are error free.

Hopefully, dar might fit into your backup and recovery schemes. There are a number of other softwares to do something similar. Partimage on the http://www.sysresccd.org comes to mind, though that is used for entire partitions. Also Duplicity is available, but that's strength is in encryption and network backups. To its strength, dar is a proven solution and is very well documented:
http://dar.linux.free.fr/doc/

As I have time, I will post a bit more technical information about the commands used, but the best idea is to research the documentation at the link above, as well as do a simple "dar -h" at the command line for a listing of all the available features.

Update 1/4/2014
The Extraction Process Redux
I've been restoring dar archives from DVDs.  Today, I pulled out a couple five DVD dar archives that I originally created four years ago.  Each DVD took about six minutes to copy over to my hard drive.  I'm happy to say that dar restored the individual video files that I specified without any problems.  Here's a sample command:

dar -x 20090430_data -g 20090430.m2v

However, dar did spit out this message:
File ownership will not be restored as dar is not run as root. to avoid this message use -O option [return = YES | Esc = NO]
Continuing...

Error met while opening the last slice: This is an old archive, it can only be opened starting by the first slice. Trying to open the archive using the first slice...

Even with this message, the archived files restored without error.

The commands above mean:
-x = extract
-g = subdirectory to include in the operation

Also, another good switch is -O, to avoid the "root ownership" message seen above.  Be careful of the placement of -O..it has to be the first parameter.  Like so:

dar -O -x 20090430_data -g 20090430.m2v

After giving the -O parameter in the above command, all you should see is the "Error met while opening the last slice" message.

Update 10/1/2008
The Extraction Process
I pulled out a 6 DVD dar archive that I originally created more than a year ago and I'm happy to say that dar restored the files without any problems. Specifically, I needed to pull one MPEG video from a dar archive of about 25 files. The dar command to extract one specific file was relatively simple:
dar -x -I *.mpg

-x = extract
-I = include following filespec in operation


So my command ended up looking like this:
dar -x /mule/20060831 -I *.mpg

One thing I noticed is that depending on the archive, wildcards (like *.mpg) may work, but not all the time. In which case, you should remove the wildcard from the include specification and just use the exact syntax; eg:
dar -x /mule/20060831 -I file.mpg

That's it!

Have a good day!
The Video Mule

5/30/07 update - After using dar for the past couple of days and releasing about 50GB, I have to say that I am really starting to like this new process. It is a consistent, repeatable and efficient approach to archiving my material that I can kick off before bedtime.

10/1/08 update - Dargui is a nice, simple graphical front end to dar. For some reason, though, the filter did not work properly, so I reverted to command line. Perhaps someone else will have better luck.


References
http://dargui.sourceforge.net/
http://dar.linux.free.fr/doc/mini-howto/dar-differential-backup-mini-howto.en.html

Saturday, February 10, 2007

scripted rendering!

I am very happy to say that I've been able to script a couple pieces of the rendering process. Once I've edited my HDV masterpiece, I export out an .m2v file and an mp3, layer 2 file, according to my previous post:
http://crazedmuleproductions.blogspot.com/2006/11/cam-compatible-hdv-edit-chain-part-ii.html

I then use the following script to:
1) combine the .m2v and .m2a as a program stream (using mplex)
2) convert the program stream to a transport stream, MPEG-TS file (using VLC)
3) convert the MPEG-TS to a DVD compatible mpg (using ffmpeg)
4) lastly, convert the DVD to an iTunes compatible format (again, using ffmpeg)

Obviously, their is inherent quality loss in each step. I tried to eliminate one conversion step by rendering to the iTunes format direct from the HDV source, but for some reason, ffmpeg gave me errors. So, I saved time and just converted from the DVD source. I will revisit this problem in a later post.

I can say that scripting VLC kicks ass! No more GUI!! Yay!

Here is the script. Don't forget to "chmod a+x " in order to run it.

Note that when run, the script will ask for a filename. That filename must be common to the .m2v and .m2a files. For example, start with two files called test.m2v and test.m2a. The script will prompt you for the filename, at which point you will enter "test" and hit enter. After that, the script will chug away and render each of the files to the filename test., where format will vary by the destination type (.ps/.m2t/.mpg/.mov).

Good luck!

#!/bin/bash -v
#
# This script converts HDV content
echo "Type in the name of the m2a/m2v files for mplex'ing"
read NAME
echo "Mplex'ing $NAME"
mplex -f 3 -b 2000 ${NAME}.m2a ${NAME}.m2v -o ${NAME}.ps

echo "VLC'ing $NAME"
vlc $NAME.ps --sout '#duplicate{dst=std{access=file,mux=ts,dst="'$NAME'.m2t"}}' vlc:quit

echo "FFMPEG convert to DVD"
ffmpeg -i ${NAME}.m2t -target dvd ${NAME}.mpg

echo "FFMPEG convert to MOV"
ffmpeg -i ${NAME}.m2t -f mov -vcodec mpeg4 -qscale 7 -s 320x180 -r 29.97 -aspect 16:9 -acodec aac -ac 2 -ab 128 ${NAME}.mov
echo "Done"

Friday, February 09, 2007

backing up your HDV content with dual-layer DVD

I finally made the jump to dual DVDs. I successfully burned a dual layer dvd using growisofs! I am very excited! This means I can get that content OFF my hard drives..FINALLY! However, there is a 4.3GB file size limitation, so keep those MPEGTS's small. Or, if you have files you need to backup that are larger than 4.3GB, go ahead and split them using AVIDEMUX.

First, get and install the dvd+rw-tools from here:
http://fy.chalmers.se/~appro/linux/DVD+RW/tools/

The install requires you to:
1) untar/gunzip the source files
2) run "make"
3) run "make install"

After that, the tools should be installed. Once the tools are installed, load up a dual-layer disc and check your media:
dvd+rw-mediainfo /dev/hda

You'll get output similar to this:
[gagazote@computer hvirtual]# dvd+rw-mediainfo /dev/hda
INQUIRY: [_NEC ][DVD_RW ND-3500AG][2.16]
GET [CURRENT] CONFIGURATION:
Mounted Media: 2Bh, DVD+R Double Layer
Media ID: MKM/001
Current Write Speed: 6.1x1385=8467KB/s
Write Speed #0: 6.1x1385=8467KB/s
Write Speed #1: 5.1x1385=7056KB/s
Write Speed #2: 4.1x1385=5645KB/s
Write Speed #3: 3.1x1385=4234KB/s
Write Speed #4: 2.0x1385=2822KB/s
Write Speed #5: 1.0x1385=1411KB/s
GET [CURRENT] PERFORMANCE:
Write Performance: 4.0x1385=5540KB/s@[0 -> 4173824]
Speed Descriptor#0: 00/4173824 R@5.0x1385=6925KB/s W@4.0x1385=5540KB/s
Speed Descriptor#1: 00/4173824 R@5.0x1385=6925KB/s W@2.4x1385=3324KB/s
DVD+R DOUBLE LAYER BOUNDARY INFORMATION:
L0 Data Zone Capacity: 2086912*2KB, can still be set
READ DISC INFORMATION:
Disc status: blank
Number of Sessions: 1
State of Last Session: empty
Number of Tracks: 1
READ TRACK INFORMATION[#1]:
Track State: blank
Track Start Address: 0*2KB
Next Writable Address: 0*2KB
Free Blocks: 4173824*2KB
Track Size: 4173824*2KB
ROM Compatibility LBA: 262144
READ CAPACITY: 1*2048=2048


Then, burn your files:
growisofs -Z /dev/hda -R -J /mnt/videos/*

This command creates an ISO 9660 image of the directory "/mnt/videos" and writes it to a dual-layer DVD.
-Z erases any previous contents
-R include rockridge extensions
-J include joliet extentions

Sunday, February 04, 2007

AVIDEMUX is an excellent little program..

Rough cuts video of any format and saves to any format rapidly. I used it to chop up a 7GB MPEGTS HDV file and it took one minute to render out a 2.7GB! This is much better than using Cinelerra to reduce it via a re-render via Quicktime. Sweet! I don't see any loss in the image, but that will need more investigation. Still, initial impressions are very good!

Friday, November 10, 2006

cam compatible HDV edit chain, part II (short)

I posted a more detailed version of this two posts ago (http://crazedmuleproductions.blogspot.com/2006_10_01_archive.html), but I thought I'd reiterate it in a shorter format:
1) from Cinelerra, export video using YUV4MPEG using mpeg2enc (.m2v) compression scheme with special params:
mpeg2enc --verbose 0 --aspect 3 --format 3 --frame-rate 4 --video-bitrate 18300 --nonvideo-bitrate 384 --interlace-mode 0 --force-b-b-p --video-buffer 448 --video-norm n --keep-hf --no-constraints --sequence-header-every-gop --min-gop-size 6 --max-gop-size 6 -o %
2) from Cinelerra, export audio using mpeg audio, layer II (384Kbps)
3) multiplex audio and video streams to MPEGPS (Program Stream) format using mplex:
mplex -f 3 -b 2000 video.m2v audio.m2a -o output.ps
4) convert .ps to .ts, MPEGTS (Transport Stream) format using VLC
5) export to cam, consider using 10sec blank leader

Update 5/17/2009
Note that if you bring the video into Cinelerra and the thumbnail is green, this means that the first frame of the video is not a keyframe. This is a problem with VLC. You can avoid this problem by using this ffmpeg command in place of Step 4 above:
ffmpeg -y -i OUTPUT.ps -acodec copy -f mpegts -qscale 1 OUTPUT.m2t
*** end update ***

Update 3/14/2008
Once the video is converted, you may then output to the camera using test-mpeg2 from libiec61883:
/2006/10/success-and-failure-in-land-of-dvhdv.html

Also, take note of a possible 2GB limit in libiec61883 uploading your content to the cam:
/2006/10/libiec61883-now-exports-files-greater.html
*** end update ***

Update 11/17/06
Last night, I found that doing performing other operations on your system like surfing the web or playing audio while running mplex or VLC convert may cause problems with the video as it uploads to the camera. So don't do anything while you convert your videos.

In my case, I was 28 minutes into an hour long video uploading to the cam when the import stopped. I no longer had uploading video displayed on the LCD screen of the cam. I only saw blue, the upload seemed to continue, but I got no final output. The solution was to remux the original streams using mplex and rebuild the MPEGTS using VLC. The important point being that one of these two processes is very sensitive to fluctuations in CPU or hard drive performance. So leave your PC alone while these processes are active.
*** end update ***

That's all she wrote for today. Enjoy!
the mule

Sunday, October 29, 2006

rendering cam-compatible HDV MPEGTS files

I have been successful in exporting HDV back out to my cam. I haven't posted detailed output from the individual steps before, so here it is:

General Steps
1) export video from Cinelerra as mpeg2 using mpeg2enc
2) export audio from Cinelerra as mp3
3) mux in mplex
4) open MPEGPS in VLC and convert to TS.

Detail
1) export video from Cinelerra as mpeg2 using mpeg2enc:
mpeg2enc --verbose 0 --aspect 3 --format 3 --frame-rate 4 --video-bitrate 18300 --nonvideo-bitrate 384 --interlace-mode 0 --force-b-b-p --video-buffer 448 --video-norm n --keep-hf --no-constraints --sequence-header-every-gop --min-gop-size 6 --max-gop-size 6 -o /mnt/videos/20050721/20050721.m2v

2) export audio from Cinelerra as mp3:
--------------------------------------------
Input File : 'stdin' 48.0 kHz
Output File: '/mnt/videos/20050721/test.mp3'
384 kbps MPEG-1 Layer II j-stereo Psy model 1
[De-emph:Off Copyright:No Original:No CRC:Off]
[Padding:Normal Byte-swap:Off Chanswap:Off DAB:Off]
ATH adjustment 0.000000
--------------------------------------------
encode_init: using tablenum 0 with sblimit 27
Hit end of audio data
Avg slots/frame = 1152.000; b/smp = 8.00; bitrate = 384.000 kbps


3) mux in mplex
[root@computer 20050721]# mplex -f 3 -b 2000 -o test.ps test.mp3 20050721.m2v
INFO: [mplex] mplex version 1.8.0 (2.2.4 $Date: 2005/08/28 17:50:54 $)
INFO: [mplex] File test.mp3 looks like an MPEG Audio stream.
INFO: [mplex] File 20050721.m2v looks like an MPEG Video stream.
INFO: [mplex] Found 1 audio streams and 1 video streams
INFO: [mplex] Selecting generic MPEG2 output profile
INFO: [mplex] Multiplexing video program stream!
INFO: [mplex] Scanning for header info: Audio stream c0 (test.mp3)
INFO: [mplex] MPEG AUDIO STREAM: c0
INFO: [mplex] Audio version : 1.0
INFO: [mplex] Layer : 2
INFO: [mplex] CRC checksums : no
INFO: [mplex] Bit rate : 49152 bytes/sec (384 kbit/sec)
INFO: [mplex] Frequency : 48000 Hz
INFO: [mplex] Mode : 0 stereo
INFO: [mplex] Mode extension : 0
INFO: [mplex] Copyright bit : 0 no copyright
INFO: [mplex] Original/Copy : 0 copy
INFO: [mplex] Emphasis : 0 none
INFO: [mplex] Scanning for header info: Video stream e0 (20050721.m2v)
INFO: [mplex] VIDEO STREAM: e0
INFO: [mplex] Frame width : 1280
INFO: [mplex] Frame height : 720
INFO: [mplex] Aspect ratio : 16:9 display
INFO: [mplex] Picture rate : 29.970 frames/sec
INFO: [mplex] Bit rate : 18300000 bits/sec
INFO: [mplex] Vbv buffer size : 229376 bytes
INFO: [mplex] CSPF : 0
INFO: [mplex] SYSTEMS/PROGRAM stream:
INFO: [mplex] rough-guess multiplexed stream data rate : 19077648
INFO: [mplex] Setting best-guess data rate.
INFO: [mplex] Run-in Sectors = 752 Video delay = 58123 Audio delay = 61126
INFO: [mplex] New sequence commences...
INFO: [mplex] Audio c0: buf= 0 frame=000000 sector=00000000
INFO: [mplex] Video e0: buf= 0 frame=000000 sector=00000000
INFO: [mplex] Scanned to end AU 39469
INFO: [mplex] STREAM e0 completed @ frame 39469.
INFO: [mplex] STREAM c0 completed @ frame 54874.
INFO: [mplex] Multiplex completion at SCR=118584775.
INFO: [mplex] Audio c0: buf= 2304 frame=054874 sector=00031342
INFO: [mplex] Video e0: buf= 0 frame=054843 sector=00509538
INFO: [mplex] AUDIO_STATISTICS: c0
INFO: [mplex] Audio stream length 63216000 bytes.
INFO: [mplex] Syncwords : 54875
INFO: [mplex] Frames : 54875 padded
INFO: [mplex] Frames : 0 unpadded
INFO: [mplex] BUFFERING min 18 Buf max 1169
INFO: [mplex] VIDEO_STATISTICS: e0
INFO: [mplex] Video Stream length: 1031417371 bytes
INFO: [mplex] Sequence headers: 6579
INFO: [mplex] Sequence ends : 1
INFO: [mplex] No. Pictures : 39470
INFO: [mplex] No. Groups : 6579
INFO: [mplex] No. I Frames : 6579 avg. size 58215 bytes
INFO: [mplex] No. P Frames : 32891 avg. size 19714 bytes
INFO: [mplex] No. B Frames : 0 avg. size 0 bytes
INFO: [mplex] Average bit-rate : 6264800 bits/sec
INFO: [mplex] Peak bit-rate : 9203600 bits/sec
INFO: [mplex] BUFFERING min 943996 Buf max 1944874
INFO: [mplex] MUX STATUS: no under-runs detected.
[root@computer 20050721]#



4) open MPEGPS in VLC and convert to MPEGTS.

Update 3/14/2008
Once the video is converted, you may then output to the camera using test-mpeg2 from libiec61883:

/2006/10/success-and-failure-in-land-of-dvhdv.html

Also, take note of a possible 2GB limit in libiec61883 uploading your content to the cam:
/2006/10/libiec61883-now-exports-files-greater.html

Saturday, October 28, 2006

libiec61883 now exports files greater than 2GB to cam!

As you may know, I've been successful in rendering HDV MPEGTS that works being output back to my cam. However, I stumbled upon the fact that libiec61883 cannot output files greater than 2GB. Well, I rolled back my operating system to an earlier image, rebuild libiec61883 with the -D_FILE_OFFSET_BITS=64 DEPS param in examples/Makefile and now I can send data greater than 2GB using the command
[scf@localhost ~]# test-mpeg2 -t 0 file.m2t
where 0 is the physical id of the cam as specified by gscanbus

I have found that using the incorrect physical id will result in the following error message:
[scf@localhost ~]# test-mpeg2 -t 1 file.m2t libiec61883 warning: No plugs exist on either node; using default broadcast channel 63.

However, the camera will still display the incoming video. If the physical id is correctly specified, you should see no errors and the "Starting to transmit.." message only:
[scf@localhost ~]# test-mpeg2 -t 0 file.m2t
Starting to transmit

I have no idea why this fixed the problem, because I used the same steps as before. Just glad it works.

Thursday, October 19, 2006

HDV MPEG2 transport stream file sizes/render rates

The data rate of the 720P MPEG2-TS files output from my cam is about 108.95MB/min or 1.82MB/s. Here is a table of video length-to-size conversions.

duration size

12m 1.32GB
15m 1.65GB
18m35s 2.07GB
19m12s 2.12GB
20m01s 2.18GB
34m 3.70GB
Exporting 720P HDV from Cinelerra takes two processes:
1) render the video
2) render the audio

Here are some rendering times using mpeg2enc and mpeg layer 2 audio compression:

duration mpeg2enc render rate
63m 310m 4.92min per min of video

duration mp2 render rate
63m 6m 0.09min per min of audio

Mplex takes about 7 minutes to mux about an hour of audio and video.

Monday, October 16, 2006

hard work is paying off / HDV workflow

OK! I've got the latest video up on iTunes and now I just need to edit the XML for it. Editing the damn XML always takes too long. And of course, I want to get the track list right with the correct track times and witty comments. Ufff. OK! So the track list is done and now to send the boys the email for the latest video! Hurrah!

More for my benefit than anyone elses, here is my HDV workflow using Cinelerra:
1) import video
2) edit video
3) for multiple segments, make sure you set "align to frames" and audio and video segments end at the exact same time.
4) for HD output, render using mpeg2enc settings
5) for DVD output, render using ffmpeg -target DVD from Cinelerra, something like this:
ffmpeg -f yuv4mpegpipe -i - -y -i /mnt/videos/20050721/20050721.wav -target dvd %
6) for iPod output, render using ffmpeg -target DVD, then output to mpeg4 video/aac audio mov container:
ffmpeg -i inputdvd.mpg -f mov -vcodec mpeg4 -qscale 7 -s 320x180 -r 29.97 -aspect 16:9 -acodec aac -ac 2 -ab 128 output.mov
7) import .mov to XP iTunes
8) upload .mov to website
9) edit RSS XML
10) final test in iTunes

Wednesday, October 11, 2006

exporting HD content to the cam: IT IS ALIIIIVE!! ALIVE!!

After a month of testing and experimentation with many inter-OS softwares, exporting HD content to my camera is working! YAHOO!! The greatest part is..I am Microsoft FREE!

Most of my work was based off of this original thread:
http://www.dvinfo.net/conf/archive/index.php/t-19007.html

But a number of steps (ffmpeg/DVHSCap) did not work for me.

Here are the steps I used to get HD content from Cinelerra back to my cam in full resolution, HD 720P format:

1) install the latest 2.6.18 kernel according to specs on http://www.linux1394.org
2) you'll need the latest libiec61883 and libraw1394 libraries from the same place . I needed to manually tweak the ./configure procedure for libiec61883 with the following environment variable:
PKG_CONFIG_PATH=/usr/local/lib/pkconfig
Another caveat to this step is that I needed to remove libraw1394 first, which was a nightmare because of all its dependencies. I created a yum install script to reinstall everything after this was complete.
3) you'll need to make a change to tsbuffer.c and recompile libiec61883 (don't have it now..email me if you need it).
4) you'll need to recompile mpeg2enc to allow for bitrates larger than 14000. This is referenced in the thread from way back when:
http://www.dvinfo.net/conf/archive/index.php/t-19007.html
--------------------
A kludgy hack allowed me to get bit rates higher than 15Mbs in mpeg2enc

I removed the following lines from conform.cc
if (bit_rate> 1.0e6 * maxval->bit_rate)
mjpeg_error_exit1("Bit rate is greater than permitted in specified Level");
---------------------
5) here are the mpeg2enc switches that were successful for me:
mpeg2enc --verbose 0 --aspect 3 --format 3 --frame-rate 4 --video-bitrate 18300 --nonvideo-bitrate 384 --interlace-mode 0 --force-b-b-p --video-buffer 448 --video-norm n --keep-hf --no-constraints --sequence-header-every-gop --min-gop-size 6 --max-gop-size 6 -o mpeg2.mpv
6) mplex params:
mplex -f 3 -b 2000 -o programstream.ps audio.mp2 mpeg2.mpv
7) Use VLC to convert the file to a transport stream. By the way, the VLC is a hellish snake pit of install dependencies. Go to http://www.videolan.org to download the latest source.

Here are the instructions for VLC from Paul St. Denis' original steps (http://www.celt.sunysb.edu/paul/mpeg2enc_HDTV.zip):
------------------
Start VLC, from the "file" menu select "open file", click on the "Browse..." button and find "programstream.ps", check on the "advanced output" check box and then click on the "Settings.." button. Make sure the "file" radio button is picked click on the "Browse..." button save the file on your desktop as "transport.ts".

Make sure that the "Encapsulation Method" is MPEG TS, leave everything else blank. Click OK, which brings you back to the other screen click OK again.
------------------

Yes, it's a lot of work. But to be Microsoft free is a great feeling.

Update: as of this writing (10/17), I'm only able to upload less than 2GB of video. For my 720P video cam, this is about 19.7 minutes of video. I am working with the linux1394 community to resolve this.

Wednesday, October 04, 2006

trying to get HDV content back out to my cam..

Lest ye think I have been sitting on my very small, small laurels, I want all the open source, JVC HD10U fans out there know that I am still working the problem. However, I have almost given up as a result of my many trials and tribulations over the past five days with MPEGTS file formats and the JVC cam.

My journey started here:
http://www.dvinfo.net/conf/archive/index.php/t-19007.html

I replicated all of the steps; however, the last step I cannot reproduce because I don't have an Apple Mac. I've tried exporting the .ts file to the cam using DVHSTool, CapDVHS and the utility that comes with the cam, but all of them give me errors. I've tested against many different versions of outputted files with little success. My last hope is that I can get a virtual Mac going using Xen, download DVHSCap to it and then try DVHSCap on the file.

say a prayer..cross your fingers..