seamless looping


137 posts   Page 1 of 6   1, 2, 3, 4, 5, 6
by colbyparsons » Sun Jun 10, 2012 4:04 pm
Does anyone have suggestions on how to achieve seamless video looping? I have Raspbmc (XBMC) installed on my Pi, and figured out how to loop a video -- it works, but with a few seconds of black screen before loading again. I make sculpture using video projection, and I need to be able to loop with no hesitation, no black screen, no loading symbol. For a long time I have been looking for a small and inexpensive device that would seamlessly loop a single video, and I am hoping that ultimately the Pi will do that. Now that I have one, I'm looking for solutions and not finding any so far -- though I have seen that at least one other person is looking for a solution to this issue as well.

Unfortunately I am not a programmer, so I am limited as far as what I can do myself to sort this out. My teenage son is though, and would probably be willing to help me if the solution is not too involved. Any ideas or leads would be appreciated!

--Colby
Posts: 22
Joined: Fri Jan 27, 2012 7:42 pm
by robotfunk » Sun Jun 10, 2012 4:17 pm
The codecs that work well with the Pi (H264, DivX) aren't really suited for seamless looping, as they use temporal compression (they discard the pixels that remain the same between frames). For seamless looping it would be better to use a codec such as PNG or M-JPEG, these only compress within the frame, making it much easier to jump to random points in the video. I have no idea if the CPU is capable enough to play these codecs though.
Posts: 45
Joined: Wed May 30, 2012 7:28 pm
by colbyparsons » Sun Jun 10, 2012 4:52 pm
robotfunk wrote:The codecs that work well with the Pi (H264, DivX) aren't really suited for seamless looping, as they use temporal compression (they discard the pixels that remain the same between frames). For seamless looping it would be better to use a codec such as PNG or M-JPEG, these only compress within the frame, making it much easier to jump to random points in the video. I have no idea if the CPU is capable enough to play these codecs though.

I've found some discussions online regarding a solution involving the setup of two video player instances, so that the second one preloads (buffers) before the first finishes, and at the end of the first instance of the video, it begins playing the other instance. Even with the codec issues you described, it seems like that solution might work -- if not perfectly, then at least without going to a black screen.
Posts: 22
Joined: Fri Jan 27, 2012 7:42 pm
by dom » Sun Jun 10, 2012 5:01 pm
Is this video only? And you are looping from beginning?

I would image hello_video could be modified quite easily to seek back to beginning of file when end is reached, and continue submitting frames to the already open codec.
As the start of file is a seek point I don't think there will be any problem with state in the codec.
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 4011
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge
by colbyparsons » Thu Jun 14, 2012 12:01 am
dom wrote:Is this video only? And you are looping from beginning?

I would image hello_video could be modified quite easily to seek back to beginning of file when end is reached, and continue submitting frames to the already open codec.
As the start of file is a seek point I don't think there will be any problem with state in the codec.


That sounds as though it would work, but I'm having trouble locating the variable(s) that control where in the file the program is sending data to the codec.

I've tracked down the line of code where the loop which submits data to the codec breaks, and where I would reset the variable(s), on line 161.

I've tried:
setting data_len to the value it has before quitting.
setting data_len to 0, but this is already done later in the loop and it has no effect.
both of the above in combination with exiting the loop by executing a goto located immediately before the while loop.
As far as other variables:
the variable dest is only used to determine the value of data_len
the variable find_start_codes remains zero the entire time, and in fact I can't find any code that is supposed to change it.
Posts: 22
Joined: Fri Jan 27, 2012 7:42 pm
by dom » Mon Jun 18, 2012 10:52 pm
@colbyparsons

Change:
Code: Select all
         if(!data_len)
            break;

to:
Code: Select all
         if(!data_len)
            fseek(in, 0, SEEK_SET);

and your video will play in an endless loop.
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 4011
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge
by colbyparsons » Thu Jun 21, 2012 3:12 am
Thanks Dom! Outstanding. I love that the solution was so simple. I am now getting a video loop that doesn't have even the slightest hesitation.

Next I need to add looping sound (synchronized, of course), and I need a way to have the Pi be plug-and-play for my video loops. I need to have it set up so that when power is applied, the Pi will turn on and start looping automatically with no need for a login, a password, or any external input whatsoever. Is that going to be possible? And is there a similarly simple solution to making the audio loop? Or will the issue of synchronization make that more complicated?
Posts: 22
Joined: Fri Jan 27, 2012 7:42 pm
by kencampbell » Thu Jun 21, 2012 9:23 pm
Exactly what I want to use Raspberry Pi for as well! Looping video with sound.

FYI, here's a post that mentions auto-login and speeding up boot time:

viewtopic.php?f=53&t=7884

And also a thank you to Dom for the info, works great!

Cheers,
Ken
Posts: 2
Joined: Thu Jun 21, 2012 9:20 pm
by skeeballassault » Thu Jun 28, 2012 5:47 pm
Hey Ken,
I'm actually trying to loop video on boot too, would you mind giving me a rundown on what you did? I'm not sure entirely what you did, or how to put my own videos into hello_video. Could you tell me exactly what you did?

Thanks,
SkeeBall
Posts: 8
Joined: Thu Jun 28, 2012 5:43 pm
by colbyparsons » Thu Jun 28, 2012 11:40 pm
skeeballassault wrote:Hey Ken,
I'm actually trying to loop video on boot too, would you mind giving me a rundown on what you did? I'm not sure entirely what you did, or how to put my own videos into hello_video. Could you tell me exactly what you did?

Thanks,
SkeeBall


SkeeBall,

My son Miles has been working on this for me, and has the Pi loading up automatically on boot, pulling the video from a folder that's easier to find (and plays no matter what the name of the file is, as long as it is the right format, so I can change out the video file without changing the code), and looping with no jump or hesitation whatsoever. The only downsides are:

1.-- We don't have audio working yet, and I suspect that it could be a challenge to get it to loop seamlessly in perfect sync with the video. I think that might not be able to get sorted out until the audio drivers are out of beta.

2.-- For the video resolution and fps I need to play, I have to split .mp4's to get an .h264 file, adding an extra processing step

3. --As the Pi boots up you can see the usual information about what's loading, ultimately I'd like to have a cleaner lead-in, though it's not much of an issue. Also, the way Miles set it up, the video loads in before everything else has completed, so there is sometimes a bit of hesitation here and there within the first 30 seconds or so. Not a big deal for what I need, but maybe could be for some.

4. -- We don't have a way to interrupt the video once it starts other than turning it off. Although I haven't thought of a reason to need that for what I'm doing. Miles was trying to find a way to interrupt the loop with a keypress, but so far nothing he tried has worked.

I plan to follow up with specifics within the next day or two.

--Colby
Posts: 22
Joined: Fri Jan 27, 2012 7:42 pm
by skeeballassault » Mon Jul 02, 2012 2:40 am
Colby,
Thanks for being so thourough in your reply. Audio isn't neccecary, I just inten to play it in the background of films. Thanks a ton for offering to help.
Skeeball
Posts: 8
Joined: Thu Jun 28, 2012 5:43 pm
by skeeballassault » Thu Jul 12, 2012 5:58 pm
Hey Colby,
Not to be a bother, but its been 2 weeks, any chance you could post the instructions?
Thanks,
Skeeball
Posts: 8
Joined: Thu Jun 28, 2012 5:43 pm
by colbyparsons » Sun Jul 15, 2012 4:38 am
Sorry Skeeball! My son had a lot of other stuff going on, so he didn't get to writing out the instructions right away. Keep in mind that he was writing them for me, and I know next to nothing about programming, so that's why he was so explicit about all the details. Let me know if you have any problems. I'm still hoping to get audio added to this process -- I know that was not important for what you are doing, but if anyone else comes across this and can offer a solution, please let me know.
--Colby

How to Seamlessly Loop Video
(image only -- no sound yet)


Changing the Code:

1.Open the file hello_video.c in the directory /opt/vc/src/hello_pi/hello_video/ with the commands:
cd /opt/vc/src/hello_pi/hello_video/
vi video.c

2.Scroll down the file until around line 120, where you should find the code:

Code: Select all
if(!data_len)
break;


3.Change to insert mode by hitting the insert key or CTRL + I
4. Replace the two lines of code from step 2 with these:

Code: Select all
if(!data_len)
fseek(in, 0, SEEK_SET);


5.Hit the insert key or CTRL + I again, and then type :wq to save and quit the file
6.finally, while still in the directory, compile the new code with the command: make


Playing the Video:
1. place a video (must be .h264 or .m4v) in the root of /opt/vc/src/hello_pi/hello_video/ with FTP or SFTP.
2. if you are still in the directory from the last section, skip this step. Otherwise, run this command again:
cd /opt/vc/src/hello_pi/hello_video/
3. run this command to play the video, replacing "video_name.h264" with the name of your video, and its file ending:
./hello_video.bin video_name.h264
4. it will now run seamlessly. You can press CTRL + C at any time to kill the process


Running on Startup:
This section is for those that want to video to immediately start playing when the raspberry pi boots(before login)
1. Create a file called seamless_video.sh from this code:

Code: Select all
#! /bin/sh
# /etc/init.d/raspberry_video
#
echo "\nstart"
cd /opt/vc/src/hello_pi/hello_video/
./hello_video.bin /video/*
echo "done\n"
exit 1


2. place this file in the root of /etc/init.d/
3. Create a folder named "video" in the root of the SD card.
4. Place an .h264 or .m4v video in /video/. It can have any name.
The video will now automatically play when the raspberry pi starts! The first few loops may be a bit slow because it begins playing before everything has loaded. Another thing to note is that you will have to FTP/SFTP to the raspberry pi and move/delete the video or script to prevent it from playing on startup. If someone has an easy solution to stop the video from playing (a way to interrupt it) directly on the pi, it would be greatly appreciated
Posts: 22
Joined: Fri Jan 27, 2012 7:42 pm
by skeeballassault » Mon Jul 16, 2012 3:43 pm
Hey Colby,
First off, thanks a ton for this guide, I owe Miles a major thanks. I'm having a few issues with it, though.
1. The file "hello_video.c" didn't exist, but "video.c" did, so i edited that.

2. The variable "if(!num)" didn't exist either, so I replaced the one Dom suggested earlier.

3.I'm having difficulty creating .h264 files with the various programs I've searched up, the closest being H.264 Encoder.

4. As a side note, when attempt to play a video of what I *think* is an .h264, the Pi freezes up and I have to shut it down.

Any ideas on how to fix these problems? And again, thanks for even coming back to the thread after your problem was solved.
Thanks,
Skeeball
Posts: 8
Joined: Thu Jun 28, 2012 5:43 pm
by colbyparsons » Mon Jul 16, 2012 4:09 pm
No problem Skeeball. I am so reliant on other people's help with this type of thing that I consider it a necessary karmic practice to share any solution I can find. Plus, since I'm still hoping to get the audio issue sorted out, I really don't consider everything solved for me in terms of my overall goal for this project. And even if I do get that worked out using hello_audio, in the longer run, I'm looking for a seamless looping solution that let's me use a wide range of file types and is a bit more user friendly. In any case, I'm glad to help, and so was Miles. And thanks for the heads up on the errors. I'll get Miles to help me correct them.

I posted a solution to the h264 issue here: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=38&t=7547&p=105088#p105088 but to summarize, following up on a lead based on something Dom posted about, I found a free program called "yamb" that will split video files into the audio and video components. I have been creating h.264 encoded mp4 files (first rendering lossless avi in After Effects, then using Media Encoder to create the mp4 -- Adobe keeps making it harder and harder to create non-standard sizes and frame rates!) and then using Yamb to split them. I think I also had success using an h.264 encoded m4v file without splitting it.

--Colby
Posts: 22
Joined: Fri Jan 27, 2012 7:42 pm
by levky » Tue Jul 17, 2012 4:06 am
Hi colby,

thank you so much for the detail instruction on setting this up. I am looking into doing this exact same thing and this means a million to me since i am a total noob at all this.

thanks so much!!
Posts: 5
Joined: Tue Jul 17, 2012 3:58 am
by skeeballassault » Fri Jul 20, 2012 7:15 pm
Colby,
It works! Thanks for the tip about yamb, it works 100% now! Again, thanks for all your guidance.
Posts: 8
Joined: Thu Jun 28, 2012 5:43 pm
by colbyparsons » Fri Jul 20, 2012 7:49 pm
Skeeball,

Good to hear! With Scep's help I was able to get the mistakes in the instructions post fixed, so I think it's correct now.

--Colby
Posts: 22
Joined: Fri Jan 27, 2012 7:42 pm
by roblegge » Tue Aug 28, 2012 12:23 pm
Unfortunately putting the code into the /etc/init.d folder to auto run on start up doesn't work. Can anyone help? The code itself runs ok when fired up manually.

MTIA
Rob
Posts: 12
Joined: Tue Aug 28, 2012 12:20 pm
by roblegge » Tue Aug 28, 2012 12:34 pm
Found that typing
sudo update-rc.d seamless_video.sh defaults
causes it to start almost instantly on the reboot.

R
Posts: 12
Joined: Tue Aug 28, 2012 12:20 pm
by colbyparsons » Wed Aug 29, 2012 1:14 am
roblegge wrote:Unfortunately putting the code into the /etc/init.d folder to auto run on start up doesn't work. Can anyone help? The code itself runs ok when fired up manually.

MTIA
Rob


Yeah, I am finding that too with the latest distro. When my son set this up for me, the latest version of Debian for the Pi was Squeeze, "debian6-13-04-2012" is the name of the file (pretty sure that's the one). That one still works with the looping thank goodness, because I just installed two video pieces at a show which are running on two of my pis.

Recently I spent quite a while trying to get it to work on the latest Raspbian version (2012-08-16-wheezy-raspbian) and I had no success. I was thinking that maybe something changed and it now needs to be placed elsewhere, but I haven't come up with anything. Any ideas?

Also, I wonder if anyone could help me diagnose a problem I am having with WinSCP when I upload new files to the card. If I already have a video on the card (meaning that it has gone automatically into video loop mode when I turned it on), and then I need to update the video file or change it out for another one, the new one gets partway or mostly loaded, and then it disconnects or starts incrementally slowing down until it is almost not transferring at all. I had some success by restarting the pi and doing the transfer right away, but it seems to have been slightly corrupted. I'm wondering there is a video buffering action taking place that gradually squeezes out my file transfer? (seems like the split would keep that from happening) If so, then one solution would be to build in some way to interrupt the loop. My son tried to find a way to do that, but was never successful. I would think that might be fairly simple if we knew how. I would appreciate advice on that from anyone out there who knows!

Finally, I saw one other error in the script I posted on how to loop seamlessly. On line 2, it says:
# /etc/init.d/raspberry_video
but it should say :
# /etc/init.d/seamless_video
because that line is self-referential to the file you are creating (he just forgot that he had originally called it "raspberry_video.sh").

--Colby
Posts: 22
Joined: Fri Jan 27, 2012 7:42 pm
by GeorgeIoak » Thu Aug 30, 2012 1:53 am
With some edits of the scripts I was able to get a video loop running on bootup however on the 2nd pass I can see the desktop but the first time through the video runs full screen. What can I do to always have the video full screen?
Posts: 17
Joined: Thu Aug 30, 2012 1:50 am
by Geal » Mon Sep 03, 2012 2:07 pm
Directory doesn't exist, can someone please post a command directly to editing the Video.c?

Apparently on mine /opt/vc doesn't even exist.
Hate not having a GUI, how do you know the file structure?
Posts: 7
Joined: Mon Sep 03, 2012 2:00 pm
by colbyparsons » Mon Sep 03, 2012 6:46 pm
Geal wrote:Directory doesn't exist, can someone please post a command directly to editing the Video.c?

Apparently on mine /opt/vc doesn't even exist.
Hate not having a GUI, how do you know the file structure?



Geal,

What distro are you using?

Regarding a GUI, what I posted above is a simple solution in part because it is the first one we were able to come up with that actually did what we wanted, and also because it is meant to be as hands-off as possible so that someone who knows nothing at all about how to work with a Pi can plug it in and it will run. It has to be that way so that the videos I make can be installed in galleries. Even if the person running the gallery was fine with navigating a GUI ever day when they turned on the lights, inevitably something would happen that would make the GUI show and viewers would be looking at the GUI instead of the artwork -- like if the power went out and back on, or an intern forgot to get the video going for a while.

If there WAS a GUI, for my purposes it would need to be one that only comes up if you hit control-something. I do hope someday someone will come up with a seamless looping app that allows for a lot of the options I want where settings could be controlled within a GUI. I would want settings for:
-automatic turn on at boot
-seamless loop of one video
-seamless connection from one video to another in a playlist, and looping of the playlist
-options for controlling fade in and out if desired
-ability to easily put videos onto the card via the internet
-ability to loop with audio included
-ability to handle a wide range of video codecs
-ability to interrupt the video to get back to the GUI or prompt
-maybe the ability to do a slide show feature, and even use pdf and powerpoint files that way.

Anyone feel like building something like that? Maybe you wouldn't have a huge audience to use it, but those who need it would be very grateful. It could become the new standard for video artists worldwide, not to mention it's potential use for signage for businesses (usually expensive), including places like conference centers and hotels.

--Colby
Posts: 22
Joined: Fri Jan 27, 2012 7:42 pm
by Geal » Mon Sep 03, 2012 7:10 pm
Soft float debian "wheezy"

Sorry if I look like a total noob, first time using linux. Well out of my depth on this one.
Posts: 7
Joined: Mon Sep 03, 2012 2:00 pm